Forum

> > Off Topic > Sharing a Lua State's global environment
Forums overviewOff Topic overviewLog in to reply

English Sharing a Lua State's global environment

2 replies
To the start Previous 1 Next To the start

old Sharing a Lua State's global environment

Starkkz
Moderator Off Offline

Quote
Hello, a little ago I managed to get full control of a Lua state using LuaJIT's ffi interface, but using the C functions from the Lua API directly into the main "thread" makes the program crash (it's because it causes stack errors).

So I decided to use lua "threads" which are supposed to be Lua coroutines in C, they don't crash when I use the C api from there. The problem is that the lua threads are bound to their main lua state, and loading/unloading modules becomes a problem.

I need to create a separate Lua state (via luaL_newstate) and link it to the running Lua state, so it'd be able to have a different registry/reference table.

I did change the lua thread's running environment from

Long ass code I'm using for the C API >


The code I'm using to change the state's environment
1
2
3
4
local TempThread = lua.lua_newlocalthread()
State = lua.luaL_newstate()
State.l_gt = TempThread.l_gt
State.env = TempThread.env

The error I'm getting:
PANIC: unprotected error in call to Lua API (attempt to index a nil value)

Basically I didn't change the state's running environment in the correct way (please don't argue that I'm not using the standard Lua API for this, I already tried and it's useless) (and don't ask me why I'm doing this, it's for my own purposes).

Any help?

old Re: Sharing a Lua State's global environment

MikuAuahDark
User Off Offline

Quote
user Starkkz has written
1
2
3
4
local TempThread = lua.lua_newlocalthread()
State = lua.luaL_newstate()
State.l_gt = TempThread.l_gt
State.env = TempThread.env


Did you try to execute the code line-by-line? I can't execute the first line in my Lua 5.1.4 custom build with FFI library (throws expected cdata for arg #1)

old Re: Sharing a Lua State's global environment

Starkkz
Moderator Off Offline

Quote
I realized that it makes no sense either to share the same environment object in two lua states, they both are running different garbage collectors.

Isn't there a way to have two lua states that are sharing the global environment but not sharing important stuff as the registry/reference tables?
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview