lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Richard Hundt wrote:
> local lua = ffi.load('./src/libluajit.so')

No need for this. The public entry points of the Lua/C API of
LuaJIT are already part of the default ffi.C namespace.

> With a little C helper function you could return the current
> lua_State without the need to first do luaL_newstate. Now to figure
> out how to pass data back and forth :)

The current lua_State is NOT safe to use! You'll need to specially
mark any C function which accesses or calls back into the same
state. I'll add this mark whenever I get around to implementing
callbacks. Also, Lua callback functions will probably be run in a
separate one-shot coroutine.

BTW: Using a different lua_State and passing data around is not
that difficult:

local ffi = require("ffi")

ffi.cdef[[
typedef struct lua_State lua_State;
typedef double lua_Number;
lua_State *luaL_newstate(void);
void luaL_openlibs(lua_State *L);
void lua_close(lua_State *L);
int luaL_loadstring(lua_State *L, const char *s);
int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc);
lua_Number lua_tonumber(lua_State *L, int idx);
]]

local C = ffi.C

local L = C.luaL_newstate()
local s = "local x = 0; for i=1,100 do x=x+i end; return x"
C.luaL_openlibs(L)
assert(C.luaL_loadstring(L, s) == 0)
assert(C.lua_pcall(L, 0, 1, 0) == 0)
assert(C.lua_tonumber(L, -1) == 5050)
C.lua_close(L)

--Mike