[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to load lua extensions using C API?
- From: Miles Bader <miles@...>
- Date: Thu, 08 Aug 2013 18:09:16 +0900
Newton Kim <babynewton73@gmail.com> writes:
> What C API should I call to let the lua engine load my extension?
> Thank you in advance.
You can execute other bits of Lua code from the C api too... so you
_could_ just include a small constant string in your C program,
containing Lua "init" code that does all the module loading and other
setup you want, and run that code snippet before loading the main
script:
static const char init_lua_code[] = "...some lua code...";
luaL_loadstring (L, init_lua_code);
// optionally push arguments the lua code uses
lua_call (L, 0/*number of args*/, 0);
Alternatively, it's easy to just call the Lua require function:
lua_getglobal (L, "require"); // function
lua_pushstring (L, "mymodulename"); // arg 0
lua_call (L, 1, 1); // call require; returns 1 val
// If it's a 5.1/5.2-style module, it will return the module table, so
// store that in a global variable:
lua_setglobal (L, "mymodulevar"); // maybe same name as above
-miles
--
On a bad day, I see brewers still talking as though all beer were
consumed in the pub, by the pint -- by insatiably thirsty Yorkshire
steelworkers who have in reality long ago sought work as striptease
artists. [Michael Jackson]