Lua Return Table From C

Converts a ptr value to a Lua table, with the keys and values set to the fields of the specified structure type. Essentially equivalent to cinv.ptrtoarray(ptr, type, 1)1. Ptrtocallback(ptr) Converts a ptr value to a value suitable for passing as a Ccallback. Useful for C functions which return function pointers. I need to return a table of strings from the Luaj (Java, but C code would be fine) API. How do I do this? I figured out that I need to create an LTable, but I'm not sure how to add data to it: public LTable getFunctionsForObject(String type) LTable tbl = new LTable; re. First of all, our handles will be tables with raw C references stored in them as userdata. They’ll also have a bool named isValid which will help us test if the handle is still valid. We’ll also have a global table of references in Lua, so that you can easily get handle from any place without calling C. Lua is very loose about the number of arguments and return value, but C/C is less so - hence we need to be explicit when calling lua from C/C. Extract the return values, using the functions: int luatointeger(luastate.L, int stackLocation) Grab an integer value off the lua stack. The first return value is at index -1, the second return.

Return a multidimensional table from C function to lua
Push_MultiDimArray.c
/*
Return an array in this structure
http://stackoverflow.com/questions/20173954/how-to-return-a-multidimensional-table-from-c-function-to-lua
In Lua array values usually starts at 1.
{
[1] = { ['field1'] = '1', ['field2'] = '2' , ['field3'] = '3' },
[2] = { ['field1'] = '10' , ['field2'] = '20', ['field3'] = '30' }
}
*/
/* Pushes multidimentional table on top of Lua VM stack. */
intl_push_multidim_table(lua_State *l)
{
/* Creates parent table of size 2 array elements: */
lua_createtable(L, 2, 0);
/* Puts key of the first child table on-top of Lua VM stack: */
lua_pushnumber(L, 1);
/*Creates first child table of size 3 non-array elements: */
lua_createtable(L, 0, 3);
/* Fills the first child table: */
lua_pushnumber(L, 1);
lua_setfield(L, -2, 'field1');
lua_pushnumber(L, 2);
/* setfield() pops the value from Lua VM stack. */
lua_setfield(L, -2, 'field2');
lua_pushnumber(L, 3);
lua_setfield(L, -2, 'field3');
/* Remember, child table is on-top of the stack.
* lua_settable() pops key, value pair from Lua VM stack. */
lua_settable(L, -3);
/* Pushes they key value for the second child table: */
lua_pushnumber(L, 2);
/*Creates second child table of size 3 non-array elements: */
lua_createtable(L, 0, 3);
/* Fills the second child table: */
lua_pushnumber(L, 10);
lua_setfield(L, -2, 'field1');
lua_pushnumber(L, 20);
lua_setfield(L, -2, 'field2');
lua_pushnumber(L, 30);
lua_setfield(L, -2, 'field3');
/* Remember, child table is still on-top of the stack.
* lua_settable pops the key, value pair from Lua VM stack
* And puts child table into the parent. */
lua_settable(L, -3);
/* Returns number of output tables:
* (1 multidimentional) */
return1;
}

commented Feb 2, 2018

Lua Return Table From C

Lua Return Table From Costco

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment