Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a closure with Lua

Tags:

c

lua

Instead of using the lua_CFunction signature for writing methods to be called from Lua, I'd like to use my own function signature that simplifies the export process.

void foo(call_t *call)
{
    int a;
    char *b;
    char *c;
    table_t *d;

    /* reading arguments */
    a = read_integer(call);
    b = read_string(call);

    /* do something... */

    /* writing arguments */
    write_string(call, c);
    write_table(call, d);
}

/* export to Lua */
export("foo", foo);

So far, all I can think of doing is having a single lua_CFunction that calls the wrapper function from a table. However, I don't know how to associate a Lua function with a C function and table index so as to effectively make the Lua function a closure. Something like this:

lua_register_with_data(state, "foo", base_function, FOO_INDEX);

How can I make this happen?

like image 585
jmegaffin Avatar asked Oct 01 '22 12:10

jmegaffin


1 Answers

I figured it out after all. I guess this proves how useful rubber duck debugging is.

I just registered the base function along with the actual function index as an upvalue.

function_t table[FUNCTION_COUNT];

/* lookup function using upvalue */
int base_function(lua_State *state)
{
    int index;
    call_t call;

    call.state = state;
    call.argument_index = 1;
    call.return_count = 0;

    index = lua_tointeger(state, lua_upvalueindex(1));
    table[index](&call);

    /* return_count is incremented by write_* functions */
    return(call.return_count);

}

/* register function as closure */
table[FOO_INDEX] = foo;
lua_pushinteger(state, FOO_INDEX);
lua_pushcclosure(state, base_function, 1);
lua_setglobal(state, "foo");
like image 140
jmegaffin Avatar answered Oct 26 '22 13:10

jmegaffin