lua-users home
lua-l archive

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



Well, it's my library :) and I'm puzzled as to why not finding the email.. Anyways, you found me. :)

You obviously haven't read the manual.html (I uploaded it now to http://luaforge.net/docman/view.php/21/19/manual.html so it can be viewed online). Also, the 'sample.c' is now there, it contains simple working sample code.

To make things clear, 'gluax' is the name of the interfacing code (couple files), LuaX is the name of the distribution utilizing it. Now, read on..


13.7.2004 kello 09:49, Jim Jones kirjoitti:

I am currently using the framework Glua-X to bind my C function to the Lua interpreter.  I apologize if you can't answer questions regarding someone else's library, but I can't seem to find a contact address for the author.
 
I am utilizing Lua 4.0.

Okay, this means you've got an old old version of gluax. The latest also _should_ work with 4.0 but not out-of-the-box and I'm not testing them against 4.0 any more.

Why aren't you using 5.x if I may ask?

 
Here is the issue.  Say I have the following function :
  int foo (int x,int *y);
 
'y' will always return a pointer to a newly allocated value.  Say I have that function binded as such :

Do you mean "int foo(int x, int** y)" since to 'return' a pointer you need a double reference. Just checking..

 
/////////////////////////////////////////////////////////////
GLUA_FUNC(FOO)
{
  int x  = glua_getInteger(1);
 
  int *y;
  int r = foo(x,y);

Now, here you are feeding nothing (contents of 'y') to the func.
I believe you mean "= foo(x,&y)", right?

 
  glua_pushUserdata( y );  // Here is how I want to pass it back
  // glua_pushUserdata( y, glua_newtag("y")); // This is the syntax Glua-X wants

Nope. You should:
	- declare the tag variable somewhere up front ("int foo_tag")
- use it in your push/getUserdata functions (to tell which kind of data you pass)
	- define it (to Lua) at the GLUA_DECL section at the end.

See 'sample.c' for actual code:

	static int coord_tag;  // tag for Coord userdata
	...
	GLUA_FUNC(GetCoord)
	{
		struct s_Coord *cp;
    	cp= (struct s_Coord*)glua_getUserdata(1, coord_tag);

	    glua_pushNumber(cp->x);     // Return 2 separate items to Lua
    	glua_pushNumber(cp->y);
	}
	GLUA_END
	...
	GLUA_DECL( my_tbl )
		{
		...
        // Userdata types (=tags):
        glua_tag( "Coord", &coord_tag ),
        glua_tagmethod( coord_tag, "gc", _CoordCleanup ),
		}
	GLUA_DECL_END

}
GLUA_END
 
GLUA_FUNC(PRINTFOO)
{
  int *x  = glua_getUserdata(1);  // Here is the syntax I expected

You _can_ do something like that by 'glua_getUserdata(1,0)', which basically says "give me any userdata, no matter what type". In practise, if you have several different userdata types declared, you'd like to have type safety (I do!).

  // int *x  = glua_getUserdata(1,tag);  // Here is how Glua-X wants the syntax
 
  printf(*x);
}
GLUA_END
/////////////////////////////////////////////////////////////
 
I don't understand why I have to associate the pointer with a tag.  If I call the FOO function from Lua like this
 
  zzz = FOO(1)  
  PRINTFOO(zzz)
 
isn't zzz my tag? 

'zzz' is your userdata, the pointer. Which kind of data it carries is told by the tag (which is, basically, a unique integer ID). Perhaps a look at the Lua reference manual would help here? :)

I want the return value to be assigned receiving userdata object.  I am confused.
 
Any help is GREATLY appreciated.
 
Jim

-ak