Reputation: 55
I tried to call from a lua script the c method my_sin. I'm using lua 5.2.2 and wanted to test to use luaL_newlib instead of lua_register. Unfortunately, the lua script doesn't find mysin.
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <cmath>
static int my_sin (lua_State *L) {
lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
return 1;
}
static const luaL_Reg my_lib[] = {
{"mysin", my_sin},
{NULL, NULL}
};
int my_open(lua_State *L) {
luaL_newlib(L, my_lib);
return 1;
}
int main() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
my_open(L);
luaL_dostring(L, "print(mysin(2))");
lua_close(L);
return 0;
}
Upvotes: 4
Views: 5157
Reputation: 72312
luaL_newlib creates a new table and populates it from a list of functions. Your function mysin is inside this table and is not a global function. If you want it to be a global function, use lua_register.
Upvotes: 3