Environments

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Environments

Mark Meijer-2
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

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Mildred Ki'Lya
Hi,

You may want to try VEnv from the Kepler Project:

http://www.keplerproject.org/venv/

Else, I think it is a wxLua issue ... But I don't know.

If this doesn't work, you can still use Rings to create another Lua
state (and this runs on plain lua). There you are sure there is a
strict separation ... but it may require the wxLua module to be able
to be initialized twice.

http://www.keplerproject.org/rings/

Try it out and tell us if it worked, I would like to know too.

Mildred

-- 
Mildred Ki'lya
E-Mail:	mildred593(at)online.fr

Site:	<http://mildred632.free.fr/>
XMPP:	<[hidden email]> (GoogleTalk, Jabber)

GPG:	197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Mark Meijer-2
Hi,

Thanks for your reply. Actually I had tried venv already when I
started this project, but for a different reason. Part of the
experiment is to be able to run a whole bunch of scripts concurrently,
and those scripts are assumed to be "hostile" so the plan was to run
those each in their own environment too (this is why I'd like the
editor.. how cool is that, if users can write their scripts from
within the same app, with syntax highlighting and code folding and the
whole shebang... of course it can always be run separately but that's
not quite *as* cool :P).

Anyway, the problem I encountered then, is the same I got just now
when I tried it again. As soon as I call venv(MyFunction), everything
halts. I have to hit ctrl-c a couple of times just to exit Lua. I've
tried it with and without strict.lua, and with and without any other
modules/libraries (such as wxLua), it simply won't budge.

I've used venv before. As in, a couple of years ago (think it was venv
1.0, now I have venv 1.1 though), and it seemed to work then. But this
time, when it didn't work, I never looked further into it. I figured I
would just use setfenv... heh... Btw I use a precompiled binary of Lua
5.1.2, in case that matters.

Anyway, I look at the venv script and see a lot of code, and I know
it's ultimately based on setfenv. But it's not really clear to me what
the advantage is supposed to be over the couple lines of code in my
previous post?

Haven't tried Rings yet. Might check it out, looks cool. Will keep you informed.


Cheers,
Mark


On 31/01/2008, Mildred <[hidden email]> wrote:
> Hi,
>
> You may want to try VEnv from the Kepler Project:
>
> http://www.keplerproject.org/venv/
>
> Else, I think it is a wxLua issue ... But I don't know.
>
> If this doesn't work, you can still use Rings to create another Lua
> state (and this runs on plain lua). There you are sure there is a
> strict separation ... but it may require the wxLua module to be able
> to be initialized twice.
>
> http://www.keplerproject.org/rings/
>
> Try it out and tell us if it worked, I would like to know too.
>
> Mildred
>
> --
> Mildred Ki'lya
> E-Mail: mildred593(at)online.fr
>
> Site:   <http://mildred632.free.fr/>
> XMPP:   <[hidden email]> (GoogleTalk, Jabber)
>
> GPG:    197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
>

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Hakki Dogusan
In reply to this post by Mark Meijer-2
Hi,

Mark Meijer yazmÄÅ:
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.


I used wxLua's editor in my CairoPad application. I encapsulated editor as a Lua module (file is paneeditor.lua). Maybe you'll find some ideas there; http://www.dynaset.org/dogusanh


[snip]

Cheers,
Mark




--
Regards,
Hakki Dogusan

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Mark Meijer-2
On 31/01/2008, Hakki Dogusan <[hidden email]> wrote:
> I used wxLua's editor in my CairoPad application. I encapsulated editor
> as a Lua module (file is paneeditor.lua). Maybe you'll find some ideas
> there; http://www.dynaset.org/dogusanh

Cool man, thanks, I'll look into it!

By the way I've tried Rings now. Nothing appears to happen. I made a
little wrapper file that does:

require("wx"); -- Load wxLua
dofile("editor.wx.lua"); -- Start the editor
wx.wxGetApp():MainLoop(); -- Message pump

That thing works when started from the command line with lua5.1.exe,
but not when run with Rings, as follows:

require("rings");
local lEditorEnv = rings.new();
lEditorEnv:dostring("dofile('editorwrapper.lua')");

Doesn't seem to do anything, or at least not in my app, and I think
that makes sense... My app has its own message pump, how can it have
another one for the editor? I also tried it without the message pump
in editorwrapper.lua, actually I tried that first because I simply
forgot it at first... but nothing happened then too.

So I don't think this is going to work with Rings. Was worth a shot
though. And rings is worth having in any case :)

I'm open to suggestions :) Meanwhile, I'll check out Hakki's CairoPad
when I can.

Cheers,
Mark

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Mark Meijer-2
On 31/01/2008, Mark Meijer <[hidden email]> wrote:
> On 31/01/2008, Hakki Dogusan <[hidden email]> wrote:
> > I used wxLua's editor in my CairoPad application. I encapsulated editor
> > as a Lua module (file is paneeditor.lua). Maybe you'll find some ideas
> > there; http://www.dynaset.org/dogusanh

Ah I see, you took the thorough approach of beating the editor's
source code into shape :-) Looks like that's the way to go.

Cheers,
Mark

Reply | Threaded
Open this post in threaded view
|

Re: Environments

John Labenski
In reply to this post by Mark Meijer-2
On Jan 31, 2008 12:58 PM, Mark Meijer <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Mark Meijer-2
Hi,

> 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?

No I don't believe I'm getting any of the errors generated from
wxLuaEventCallback::OnEvent. I'm not exactly sure where things get
screwed up, I think it's not the callbacks themselves but other stuff
they reference. I get a whole bunch (as in *a lot*) of errors about
trying to call or index nil values, each one in a fresh new popup
messagebox. That's where I tend to hit ctrl-c a couple of times. But
the errors all seem to originate from within callbacks, i.e. from the
Lua code.

I figured it should be possible with environments, because if I'm not
mistaken, making a module() out of that editor would also setfenv() it
implicitly, and that is known to work (Hakki Dogusan did it for his
CairoPad). I haven't had the time yet to look further into it, though.
But now I'm confused by what follows.


> 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)

Maybe I misunderstand. But if a callback function is created in some
non-global environment X, and then wxLua sets the environment for that
function to LUA_GLOBALSINDEX every time it is called, wouldn't that
mean it *can't* possibly find any other stuff that resides in X? It
looks like wxLua forces callbacks to run in the global environment,
regardless of what environment they were created in. Why is that? Or
is there something about all this that I'm getting all wrong?


Regards,
Mark

Reply | Threaded
Open this post in threaded view
|

Re: Environments

Ericson Carlos
Old thread, but I ran into this 'problem' recently. Wanted to embed the editor.wx.wlua into my app so I could write test code with a nice editor without restarting the app. It couldn't run as a module due to the event-handling/environment issue so at first I modified the code to avoid naming conflicts and run it in the same environment. But I really wanted to run it as a module so I modified wxLuaEventCallback.

The changes are minimal. Save the environment alongside the function in ::Connect and load it in ::OnEvent, instead of always loading the global environment.

I could post my changes to wxLua if anyone is interested.
Reply | Threaded
Open this post in threaded view
|

Re: Environments

John Labenski


On Jun 25, 2015 5:55 AM, "Ericson Carlos" <[hidden email]> wrote:
>
> Old thread, but I ran into this 'problem' recently. Wanted to embed the
> editor.wx.wlua into my app so I could write test code with a nice editor
> without restarting the app. It couldn't run as a module due to the
> event-handling/environment issue so at first I modified the code to avoid
> naming conflicts and run it in the same environment. But I really wanted to
> run it as a module so I modified wxLuaEventCallback.
>
> The changes are minimal. Save the environment alongside the function in
> ::Connect and load it in ::OnEvent, instead of always loading the global
> environment.
>
> I could post my changes to wxLua if anyone is interested.
>

Yes please, I thought the callbacks worked in a module already. Though maybe it was not implemented the way you describe to avoid saving the env for each callback. You might want to look at the wxlua mailing list in 2012 or so for Lua code that will work for you. I can't test now, but I think you just need to prefix the module name to the function name for the callback func param.

Regards,
    John

Reply | Threaded
Open this post in threaded view
|

Re: Environments

aryajur
Here is the link to the discussion I had in 2012 for this:


Regards,
Milind


On Thu, Jun 25, 2015 at 4:00 PM, John Labenski <[hidden email]> wrote:


On Jun 25, 2015 5:55 AM, "Ericson Carlos" <[hidden email]> wrote:
>
> Old thread, but I ran into this 'problem' recently. Wanted to embed the
> editor.wx.wlua into my app so I could write test code with a nice editor
> without restarting the app. It couldn't run as a module due to the
> event-handling/environment issue so at first I modified the code to avoid
> naming conflicts and run it in the same environment. But I really wanted to
> run it as a module so I modified wxLuaEventCallback.
>
> The changes are minimal. Save the environment alongside the function in
> ::Connect and load it in ::OnEvent, instead of always loading the global
> environment.
>
> I could post my changes to wxLua if anyone is interested.
>

Yes please, I thought the callbacks worked in a module already. Though maybe it was not implemented the way you describe to avoid saving the env for each callback. You might want to look at the wxlua mailing list in 2012 or so for Lua code that will work for you. I can't test now, but I think you just need to prefix the module name to the function name for the callback func param.

Regards,
    John


Reply | Threaded
Open this post in threaded view
|

Re: Environments

Ericson Carlos
On Fri, Jun 26, 2015 at 7:15 AM, Milind Gupta <[hidden email]> wrote:
Here is the link to the discussion I had in 2012 for this:


Regards,
Milind


​Here is diff patch for the ​wxLua-2.8.12.3-src tree:
https://drive.google.com/file/d/0B19rfLak7LN-OGZMZnV1dEhOeFU/view?usp=sharing

​It also includes small changes to wxlua.cpp, wxldserv.cpp and wxlsock.cpp to get debugging working when using wxLua from a dll, such as in the samples editor.wx.lua.

​I did consider wrapping every call to :Connect but it was an interesting problem for me to see if it could work otherwise.