lua-users home
lua-l archive

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


Hi all (again :)

Question about Lua environments. I'm working on a little private
project/experiment that uses wxLua for its GUI. Now, wxLua comes with
a pretty cool Lua script that is a full-fledged editor for, well, Lua
scripts. And obviously it uses wxLua for its GUI too.

I'd like to be able to launch that editor from within my own app, but
unfortunately the editor script was really not written with that in
mind. It assumes it's alone in the universe, basically, and so makes
royal use of the global environment, shall we say.

Instead of rummaging through the script in an attempt to change all
that, I thought I could simply run it with a different Lua environment
(not a separate Lua instance, my own app is written in Lua too and
runs on a plain precompiled Lua binary). To this effect, I tried what
amounts to the following code:

local StartEditor = loadfile("editor.wx.lua");
local lEditorEnv = {};
setmetatable(lEditorEnv, {__index = _G});
setfenv(StartEditor, lEditorEnv);
StartEditor();


The problem with this is that, it being an event-based GUI app, most
of the editor runs in "callbacks" that are initiated by the GUI
toolkit (wxLua). And wxLua exists in the global environment (since I'm
using it from my main app as well). This appears to be causing
trouble, because suddenly it can't find its own functions and
variables anymore which are defined in the new environment.

I thought that calling a function in one environment, from another
environment, would happen transparently without a problem. But I don't
have a lot of experience with Lua environments so I don't really know
how this should work.

Does anyone have some insights as to where my reasoning failed, or
suggestions as to how I might get it to work?

Cheers,
Mark