constructors in tolua++

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

constructors in tolua++

Peter Kuemmel
Hello,

I've done a very simple binding of two c++ classes to lua
with tolua++1.0.5 and get a error with a constructor which
not uses the new command:


--header file tv.h:
struct A {  A(){}; };
struct B {  B(){}; };


--pkg file tv.pkg:
$#include "tv.h"
struct A { A(){}; };
struct B { B(){}; };


--main.cpp:
extern "C" {
#include "lualib.h"
#include "lauxlib.h" }
int main (void){
    int  tolua_tv_open (lua_State*);
    lua_State* L = lua_open();
    luaopen_base(L);
    tolua_tv_open(L);
    lua_dofile(L,"tv.lua");
    lua_close(L);
    return 0;
}


--lua code tv.lua:
v = B.new  print("B.new done")
v = A.new  print("A.new done")
v = B()    print("B() done")
v = A()    print("A() done")


And this is the ouput of the program:

B.new done
A.new done
B() done
tv.lua:4: error in function 'new'.
     argument #1 is 'class A'; 'B' expected.


Is it my fault or is it a bug of tolua++?


Best regards,
Peter

Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Fabian Peña
try

v = B:new()
..
..


Peter Kümmel wrote:

Hello,

I've done a very simple binding of two c++ classes to lua
with tolua++1.0.5 and get a error with a constructor which
not uses the new command:


--header file tv.h:
struct A {  A(){}; };
struct B {  B(){}; };


--pkg file tv.pkg:
$#include "tv.h"
struct A { A(){}; };
struct B { B(){}; };


--main.cpp:
extern "C" {
#include "lualib.h"
#include "lauxlib.h" }
int main (void){
    int  tolua_tv_open (lua_State*);
    lua_State* L = lua_open();
    luaopen_base(L);
    tolua_tv_open(L);
    lua_dofile(L,"tv.lua");
    lua_close(L);
    return 0;
}


--lua code tv.lua:
v = B.new  print("B.new done")
v = A.new  print("A.new done")
v = B()    print("B() done")
v = A()    print("A() done")


And this is the ouput of the program:

B.new done
A.new done
B() done
tv.lua:4: error in function 'new'.
     argument #1 is 'class A'; 'B' expected.


Is it my fault or is it a bug of tolua++?


Best regards,
Peter



Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Peter Kuemmel
Fabian Peña wrote:
try

v = B:new()
..
..


Thanks, I also found that 'B.new' is not correct.

But 'v = A()' is correct lua code (or I'm wrong?),
it triggers the evatuation of the table field '__call'.

So I think there is a bug in the tolua function

TOLUA_API void tolua_set_call_event (tolta_map.c)

which sets the __call field.

Peter


Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Peter Kuemmel
In reply to this post by Peter Kuemmel
More on a bug in tolua++.

With above example code there should be two metatables,
one for A and one for B, each with a different __call
entry, but there is only ONE table for both classes!

You can test it with this code, copied into the
tolua_tv_open function:


lua_getglobal   (tolua_S, "A");    // tA
lua_getmetatable(tolua_S,  -1);    // tA mtA

lua_getglobal   (tolua_S, "B");    // tA mtA tB
lua_getmetatable(tolua_S,  -1);    // tA mtA tB mtB

int eq = lua_rawequal(tolua_S, -1, -3);


Peter




Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Ariel Manzur-2
Hi..

Yeah, there's a bug on set_call_event, I just don't know where it is :( I 
haven't had much time to investigate it, but I figured since new_local() 
still works, it's not such a big deal.. I guess leaving that out of the 
manual was a bad idea :(

Anyway, this looks interesting, I'll check it out as soo as I can.

thanks..

Ariel.

On Fri, May 06, 2005 at 07:42:14PM +0200, Peter Kümmel wrote:
> More on a bug in tolua++.
> 
> With above example code there should be two metatables,
> one for A and one for B, each with a different __call
> entry, but there is only ONE table for both classes!
> 
> You can test it with this code, copied into the
> tolua_tv_open function:
> 
> 
> lua_getglobal   (tolua_S, "A");    // tA
> lua_getmetatable(tolua_S,  -1);    // tA mtA
> 
> lua_getglobal   (tolua_S, "B");    // tA mtA tB
> lua_getmetatable(tolua_S,  -1);    // tA mtA tB mtB
> 
> int eq = lua_rawequal(tolua_S, -1, -3);
> 
> 
> Peter
> 
> 

Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Peter Kuemmel
Ariel Manzur wrote:
Hi..

Yeah, there's a bug on set_call_event, I just don't know where it is :
I've checked the set_call_event, and it looks correct, you can't do it
better. I've tested it with lua_getglobal(L, type ), without using the
prepared stack.

( I haven't had much time to investigate it, but I figured
> since new_local() still works, it's not such a big deal..
The metatables are also identical if you comment out the
set_call_event function. I suppose something is wrong
with tolua_cclass or functions called from there, especially
the inherit stuff.
So it's maybe more worse you thought.

I guess leaving that out of the manual was a bad idea :(
This was really no good idea, it costs me several hours.
But now I know a bit tolua internals.

Anyway, this looks interesting, I'll check it out as soo as I can.

thanks..

Ariel.

Is there a reason to use char* instead const char* arguments in some
functions? It's very annoying when your calling application
uses std::string.

Peter

P.S. I've successfully build lua_qt with the GPLed windows version of
qt (kde-cygwin). If you like I send you the (very small) changes.

Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Peter Kuemmel
In reply to this post by Ariel Manzur-2
here's a fix tolua_map.c:

static void mapinheritance (lua_State* L, const char* name, const char* base)
{
	/* set metatable inheritance */
	luaL_getmetatable(L,name);

	if (base && *base)
		luaL_getmetatable(L,base);
	else {

		if (lua_getmetatable(L, -1)) { /* already has a mt, we don't overwrite it */
			lua_pop(L, 2);
			return;
		};
here ->		//luaL_getmetatable(L,"tolua_commonclass");
here ->		luaL_getmetatable(L,name);	
	};

	set_ubox(L);

	lua_setmetatable(L,-2);
	lua_pop(L,1);
}


What is the function of tolua_commonclass, do you really need it?

And why needs a metatable a metatable?


Peter


Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Ariel Manzur-2
In reply to this post by Peter Kuemmel
On Sat, May 07, 2005 at 10:36:42AM +0200, Peter Kümmel wrote:
[...]
> Peter
> 
> P.S. I've successfully build lua_qt with the GPLed windows version of
> qt (kde-cygwin). If you like I send you the (very small) changes.

yeah, I'd love to see that.. I haven't tried building lua_qt outside
debian.. Is this the version that uses cygwin's X server, or the one with
the windows native backend?

thanks..

Ariel.

Reply | Threaded
Open this post in threaded view
|

Re: constructors in tolua++

Peter Kuemmel
Ariel Manzur wrote:
On Sat, May 07, 2005 at 10:36:42AM +0200, Peter Kümmel wrote:
[...]

Peter

P.S. I've successfully build lua_qt with the GPLed windows version of
qt (kde-cygwin). If you like I send you the (very small) changes.


yeah, I'd love to see that.. I haven't tried building lua_qt outside
debian.. Is this the version that uses cygwin's X server, or the one with
the windows native backend?

thanks..

Ariel.

It's the native windows version!

I've managed it to compile with mingw, and get errors with msvc8.

Attached are building files to compile out of the box.

First get qt from:
http://kde-cygwin.sourceforge.net/qt3-win32/index.php

use the daily cvs snapshot:
http://webdev.cegit.de/snapshots/kde-cygwin/qt

build qt until the library is in the lib directory.
use for mingw a "static debug" configuration. It
takes about half an hour. (when building a dll
mingw need 0.5-1 hour just for linking!)

The build-all.bat will build lua, tolua, tolua++
and lua_qt, it uses qmake. If you don't want to
overwrite your local stuff just unpack all clean
distribution files in some new folder.

First set the environment variables: paths with setdirs_c.bat
and the qt environment variables with set-mingw.bat.

then call build-all.bat

compilation breaks  when compiling lua_qt.cpp.
Eventually there are some Mac declarations because
of a non optimal hook file. Mingw also don't
likes a conversion, which indicates a bug in
tolua++.

you must also adjust the qt-pathes in
lua_qt.pkg. This is the biggest problem of
the installation. I don't know if it's
possible to use QTDIR in this file.

Cheers,
Peter





Attachment: lua-qmake.rar
Description: application/xxx