fin-language/fin

support function pointers

Closed this issue · 0 comments

for now, delegates must be defined inside a fin class.

//fin
public class FuncPtrEx2 : FinObj
{
    public delegate i32 FuncPtr(i32 a, i32 b);

    public FuncPtr func = add;

    public FuncPtrEx2()
    {
        // empty constructor required for finlang right now
    }

    public static i32 add(i32 a, i32 b)
    {
        return a + b;
    }

    public static i32 sub(i32 a, i32 b)
    {
        return a - b;
    }

    public void use_sub()
    {
        func = sub;
    }

    public void set(FuncPtr f)
    {
        func = f;
    }

    public i32 Run(i32 a, i32 b)
    {
        return func(a, b);
    }
}
//...h

#pragma once

#include <stdint.h>


typedef int32_t (*hal_FuncPtrEx2_FuncPtr)(int32_t a, int32_t b);

typedef struct hal_FuncPtrEx2 hal_FuncPtrEx2;
struct hal_FuncPtrEx2
{

    hal_FuncPtrEx2_FuncPtr func ;
};


void hal_FuncPtrEx2_ctor(hal_FuncPtrEx2 * self);

int32_t hal_FuncPtrEx2_add(int32_t a, int32_t b);

int32_t hal_FuncPtrEx2_sub(int32_t a, int32_t b);

void hal_FuncPtrEx2_use_sub(hal_FuncPtrEx2 * self);

void hal_FuncPtrEx2_set(hal_FuncPtrEx2 * self, hal_FuncPtrEx2_FuncPtr f);

int32_t hal_FuncPtrEx2_Run(hal_FuncPtrEx2 * self, int32_t a, int32_t b);
//... c

void hal_FuncPtrEx2_ctor(hal_FuncPtrEx2 * self)
{
    memset(self, 0, sizeof(*self));
    self->func = hal_FuncPtrEx2_add;
    // empty constructor required for finlang right now
}

int32_t hal_FuncPtrEx2_add(int32_t a, int32_t b)
{
    return a + b;
}

int32_t hal_FuncPtrEx2_sub(int32_t a, int32_t b)
{
    return a - b;
}

void hal_FuncPtrEx2_use_sub(hal_FuncPtrEx2 * self)
{
    self->func = hal_FuncPtrEx2_sub;
}

void hal_FuncPtrEx2_set(hal_FuncPtrEx2 * self, hal_FuncPtrEx2_FuncPtr f)
{
    self->func = f;
}

int32_t hal_FuncPtrEx2_Run(hal_FuncPtrEx2 * self, int32_t a, int32_t b)
{
    return self->func(a, b);
}