lua-users home
lua-l archive

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


On Jan 31, 2008 12:58 PM, Mark Meijer <meijer78@gmail.com> wrote:
> 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.

The Lua callback functions that wxLua uses for wxEvent handling are
refed in the LUA_REGISTRYINDEX. Using them should not depend on them
existing in the Lua global table.

See here, do you get any of these error messages or something else?
Note that wxLua also calls setfenv() using the LUA_GLOBALSINDEX so
that you can use local vars in your event handler.

http://wxlua.cvs.sourceforge.net/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp?view=markup
void wxLuaEventCallback::OnEvent(wxEvent *event)


Hope this helps,
    John