Water's Home

Just another Life Style

0%

Calling a C Function From Lua

Prepare So

#include #include #include #include static int my_sum(lua_State *L){
int d1 = luaL_checknumber(L, 1);
int d2 = luaL_checknumber(L, 2);
lua_pushnumber(L, d1+d2);
return 1;
}

static int my_info(lua_State *L){
lua_pushstring(L, “qinuu”);
return 1;
}

static const struct luaL_Reg test_lib[] = {
{“my_sum” , my_sum},
{“my_info” , my_info},
{NULL, NULL}
};

int luaopen_test_lib(lua_State *L){
//luaL_newlib(L, test_lib); // 5.2
luaL_register(L, “test_lib”,test_lib); // lua 5.1
return 1;
}

Test

local my_lib = require “test_lib”

print(type(test_lib))

print(test_lib.my_sum(23,17))

print(test_lib.my_info())