WxLua and require

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

WxLua and require

Mildred Ki'Lya
Hi,

I wanted to program GUIs in lua so I installed wxLua. But I don't
understand how to use it from a normal lua interpreter.
the statement require("wx") makes an error since require can't find any
wxLua library. Apparently, I have to use the wxLua program.

So, does anyone know how to require wxLua from a normal lua
interpreter ... or how to make a .so that can perform that ?
Thanks.

Mildred

--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Hakki Dogusan
Hi,

Mildred yazmış:

> Hi,
>
> I wanted to program GUIs in lua so I installed wxLua. But I don't
> understand how to use it from a normal lua interpreter.
> the statement require("wx") makes an error since require can't find any
> wxLua library. Apparently, I have to use the wxLua program.
>
> So, does anyone know how to require wxLua from a normal lua
> interpreter ... or how to make a .so that can perform that ?
> Thanks.
>
> Mildred
>

I think You can't use wxLua as -standard- Lua binary module, yet.

But it is possible to use it as a dll.
(With the samples for 'using wx as plugin')
I did some experiment. Usage is like this:


[test.lua]
dofile("./samples/wxluasudoku.wx.lua")
wx.wxGetBaseApp():SetExitOnFrameDelete(true)
wx.wxGetBaseApp():MainLoop()


test.lua is executed by a loader program:

[test.cpp]
#include <windows.h>
typedef int (__stdcall *TRunScript)(const char* script);
TRunScript RunScript;

int main()
{
     HINSTANCE handle = LoadLibrary("wxlua_ds.dll");
     if (!handle) return -33;
     RunScript = (TRunScript)GetProcAddress(handle, "RunScript");

     int rc = RunScript("test.lua");

     return rc;
}


Of course, You can use Lua binary modules in test.lua.

If you are interested I can make it available for download.


--
Regards,
Hakki Dogusan
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

John Labenski
On 4/27/06, Hakki Dogusan <[hidden email]> wrote:
> > I wanted to program GUIs in lua so I installed wxLua. But I don't
> > understand how to use it from a normal lua interpreter.
> > the statement require("wx") makes an error since require can't find any
> > wxLua library. Apparently, I have to use the wxLua program.

Currently yes. wxLua is a rather large system and may or may not be
suited to loading as a module. You can also use the wxLuaFreeze
program by downloading and compiling one of the snapshots on the
download page or using the CVS version of wxLua. The wxLuaFreeze
program works as a replacement for the lua executable if you haven't
attached a program and can run any standard lua script, therefore it's
not clear if there are any advantages in making a module out of wxLua.

> > So, does anyone know how to require wxLua from a normal lua
> > interpreter ... or how to make a .so that can perform that ?
> > Thanks.

I am waiting for some docs about module and require. It seems that all
that exist are some emails with the specifications.

Other people have asked for this capability as well, but for me it's
not high on my todo list. I'd rather get the bindings updated to
wxWidgets 2.6.3 and write some docs. However, I welcome work on this
and a dicussion on [hidden email] might be a good
place to flesh out what is needed to be added/changed and get feedback
from others, like Hakki, who are already trying it out so work isn't
duplicated.

> I think You can't use wxLua as -standard- Lua binary module, yet.
>
> But it is possible to use it as a dll.
> (With the samples for 'using wx as plugin')
> I did some experiment. Usage is like this:

This looks promising... I think wxWidgets has it's own dll (library)
loading class, maybe that could be used so that this might even work
cross-platform? I'm afraid that I have no experience with dlls or
loading libraries. If this can be worked we can add a C++ function in
the base wxLua library and a method for lua to call it to load the dll
from lua itself. In this case, maybe it'll work using a stock lua
executable.

Note: Coroutines will NOT work using the stock lua executable since we
have to track the lua_State and associate our data structures in the
wxLuaState class (it's ref data) with it. This is all done in C by
adding functions to lua, see lstate.c and search for wxLua and the
functions luaX_getnewthreadhandler which should probably be given
names like wxLua_lua_getnewthreadhandler to make it more clear what
they're for. I'll do that later today. We might also be able to push
the wxLuaStateRefData into the references table? Is that copied
between coroutine lua_States? I dunno. See also the top of
modules/wxlua/src/wxlstate.cpp for what happens when a coroutine is
created.

Regards,
    John Labenski
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Joshua Jensen
In reply to this post by Mildred Ki'Lya
Mildred wrote:
> I wanted to program GUIs in lua so I installed wxLua. But I don't
> understand how to use it from a normal lua interpreter.
> the statement require("wx") makes an error since require can't find any
> wxLua library. Apparently, I have to use the wxLua program.
>
> So, does anyone know how to require wxLua from a normal lua
> interpreter ... or how to make a .so that can perform that ?
> Thanks.
>  
LuaPlus allows loading of wxLua through 'require'.  You could check out
the code at svn://svn.luaplus.org/root/LuaPlus/Dev/Src/Modules/wxLua for
more information...

Josh
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Diego Nehab-3
In reply to this post by John Labenski
Hi,

> can run any standard lua script, therefore it's not clear
> if there are any advantages in making a module out of
> wxLua.

It simplifies distribution for everyone.

> I am waiting for some docs about module and require. It
> seems that all that exist are some emails with the
> specifications.

There is an official description about it in the manual

     http://www.lua.org/manual/5.1/manual.html#5.3

Also, although I didn't see the Blue PiL, I am sure Roberto
talks about it in the book, doesn't him?

And, of course, we are all here to help you!

Having wxLua and any other widget toolkits working with as
smoothly as possible is important, so I am sure everyone
will be as helpful as possible.

Regards,
Diego.
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Keith Howe
On 4/28/06, Diego Nehab <[hidden email]> wrote:

> > I am waiting for some docs about module and require. It
> > seems that all that exist are some emails with the
> > specifications.
>
> There is an official description about it in the manual
>
>      http://www.lua.org/manual/5.1/manual.html#5.3
>
> Also, although I didn't see the Blue PiL, I am sure Roberto
> talks about it in the book, doesn't him?

Yes, Chapter 15 appears to be devoted to module, require, and
packaging in general. I haven't gotten that far yet though, so I can't
comment to its level of detail or coverage...

- K.Howe
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

John Labenski
In reply to this post by Diego Nehab-3
On 4/28/06, Diego Nehab <[hidden email]> wrote:
> > I am waiting for some docs about module and require. It
> > seems that all that exist are some emails with the
> > specifications.
>
> There is an official description about it in the manual
>
>      http://www.lua.org/manual/5.1/manual.html#5.3

Is there a *simple* C library that shows an implementaion of the C
side of this. I've looked at some of the ones on LuaForge, but they
use a compat51 script which I'd rather avoid and just do it the 5.1
way. Of course maybe I'm not understanding what compat51 is really
doing...

> And, of course, we are all here to help you!
>
> Having wxLua and any other widget toolkits working with as
> smoothly as possible is important, so I am sure everyone
> will be as helpful as possible.

I agree, thanks in advance for your help.

Regards,
    John Labenski
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Diego Nehab-3
Hi,

> Is there a *simple* C library that shows an implementaion of the C
> side of this. I've looked at some of the ones on LuaForge, but they
> use a compat51 script which I'd rather avoid and just do it the 5.1
> way. Of course maybe I'm not understanding what compat51 is really
> doing...

I am setting one up for you just now.

     http://www.cs.princeton.edu/~diego/ran2.tar.gz

Let me know when you get it and I will remove the file.

Don't worry about compat-5.1. This is what Lua 5.0 people
have to use to have access to the Lua 5.1 package system.
The interface is exactly what you would find in Lua 5.1. Of
course, if you are using Lua 5.1, you already have that.

[]s,
Diego.
Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

John Labenski
On 5/9/06, Diego Nehab <[hidden email]> wrote:

> > Is there a *simple* C library that shows an implementaion of the C
> > side of this. I've looked at some of the ones on LuaForge, but they
> > use a compat51 script which I'd rather avoid and just do it the 5.1
> > way. Of course maybe I'm not understanding what compat51 is really
> > doing...
>
> I am setting one up for you just now.
>      http://www.cs.princeton.edu/~diego/ran2.tar.gz
>
> Let me know when you get it and I will remove the file.

Thanks alot, that is a very nice example! I thought it was really that
simple, but I wasn't sure.

All you need is this single function right?

int luaopen_ran2(lua_State *L) {
   do whatever you want here
   return 1;
}

Must you always return 1? wxLua doesn't use the luaL_openlib(L,
"ran2", ran2_func, 1) method of creating the lib since it's a little
more complicated and we have to worry about setting up metatables for
tracking objects. I suppose that I could just push the global "wx"
table we create onto the stack after wxLua does it's thing and nobody
would be any the wiser right?

Just to be sure, if I understand the code in lua/src/loadlib.c, Lua's
require takes "require"XXX"" and looks for the C function
"luaopen_XXX" in the library and runs it. You have to name the
"-shared" library XXX.so for lua to find and you also need "-fpic" for
the position-independent code in the library. This last bit would have
certainly caused me a headache as I wouldn't have thought to do that.

> Don't worry about compat-5.1. This is what Lua 5.0 people
> have to use to have access to the Lua 5.1 package system.
> The interface is exactly what you would find in Lua 5.1. Of
> course, if you are using Lua 5.1, you already have that.

I wasn't really sure if it was that the C code was written for 5.1 and
compat51 tricked lua50 to work properly or if it was the other way
around.

Thanks again, I'll give it a shot later this week, when the
SourceForge CVS is back up :(
   John Labenski
Reply | Threaded
Open this post in threaded view
|

RE: WxLua and require

Daniel Collins
> int luaopen_ran2(lua_State *L) {
>    do whatever you want here
>    return 1;
> }
>
> Must you always return 1?

According to the API docs for lua_Cfunction, the return value must be
the number of results that the function is returning. These results must
be on the top of the stack. All other values in the stack will be
discarded.

I believe most library loading functions return 1 because they have a
single result on the stack: the table containing all the library
functions. But it doesn't have to be this way. If you have a look at
luaopen_base in lbaselib.c you will see it returns 2, because there are
two table results in this case: the globals table and the coroutine
table. At least that's how it looks to me.

All this is fresh in my mind because I am writing a lot of library
registration functions right now :)

- DC

Reply | Threaded
Open this post in threaded view
|

Re: WxLua and require

Diego Nehab-3
In reply to this post by John Labenski
Hi,

> Must you always return 1? wxLua doesn't use the
> luaL_openlib(L, "ran2", ran2_func, 1) method of creating
> the lib since it's a little more complicated and we have
> to worry about setting up metatables for tracking objects.
> I suppose that I could just push the global "wx" table we
> create onto the stack after wxLua does it's thing and
> nobody would be any the wiser right?

You can still setup a bunch of stuff before (or after)
calling luaL_openlib. Unless you have a good reason not to
use it, I would suggest you do. Makes your code easier for
people to understand later. And saves you the trouble of
writing the loop yourself. :) But yes, you are free not to
use it, create your own namespace table and return it.

About the return, users will want to write a line like this:

     local wx = require"wx"

The call to require() returns what or your function returns,
what it finds in package.loaded["wx"] (if your function
returns something, require will store it in package.loaded
too), or it returns true otherwise.

Both luaL_openlib and module create a namespace and store it
in package.loaded. So you don't actually have to return
anything if you use them. But since the table was there,
I decided to return it.

> Just to be sure, if I understand the code in
> lua/src/loadlib.c, Lua's require takes "require"XXX"" and
> looks for the C function "luaopen_XXX" in the library and
> runs it. You have to name the "-shared" library XXX.so for
> lua to find and you also need "-fpic" for the
> position-independent code in the library. This last bit
> would have certainly caused me a headache as I wouldn't
> have thought to do that.

It's in the Wiki. Instructions to build libraries in pretty
much every type of machine.

> Thanks again, I'll give it a shot later this week, when
> the SourceForge CVS is back up :(

It will be great to start using it.

[]s,
Diego.