Re: How to create another lua_State in pthread?

  • From: Mike Pall <mike-1301@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 24 Jan 2013 20:33:41 +0100

Hiroaki Nakamura wrote:
> I tried to typedef lua_State at

Under no circumstances should you need to access the internal
fields of a state.

Use an opaque struct, just like in lua.h. Here's a simple example
on how to create another state with the FFI:

local ffi = require("ffi")

ffi.cdef[[
typedef struct lua_State lua_State;
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);
]]

local C = ffi.C

local L = C.luaL_newstate()
assert(L ~= nil)
C.luaL_openlibs(L)
assert(C.luaL_loadstring(L, 'print("Hello from another Lua state!")') == 0)
assert(C.lua_pcall(L, 0, 1, 0) == 0)
C.lua_close(L)

--Mike

Other related posts: