Page 1 of 1

Can I mark table as userdata?

Posted: Thu Jun 04, 2015 10:50 am
by deathbeam
Hey everyone. I am working on port of Lua VM to Java. This question is not entirely Love2D related, but I do not know where to ask and developers here are skilled with Lua C API. So is there any way to mark table as userdata? Something like:

Code: Select all

lua_newtable( L )
lua_markasuserdata ( L , -1 ) -- pseudo code

Re: Can I mark table as userdata?

Posted: Thu Jun 04, 2015 11:35 am
by Robin
I don't think so, because userdata is basically* a pointer to C-stuff that Lua code can't directly access. So if it's a table, Lua code can access it so it doesn't need to be the opaque type userdata.

*Oversimplified because I don't know the full story myself.

By the way, why do you need such a function, if you're implementing it in Java?

Re: Can I mark table as userdata?

Posted: Thu Jun 04, 2015 2:58 pm
by ejmr
Robin is correct.

You can have userdata behave like a table in your Lua code by defining pointers to functions to act as table functions/methods, and by defining a metatable to affect that userdata. Chapter 29 of "Programming in Lua 3rd Ed" covers this, as I'm sure the older editions do as well, but I don't know the chapter numbers off the top of my head. The C API functions luaL_setfuncs() and luaL_newmetatable() are good places to start.

Hope that helps!

Re: Can I mark table as userdata?

Posted: Thu Jun 04, 2015 4:37 pm
by I~=Spam
As a side note you could create a C library that creates an empty userdata and attaches a metatable to it... (But it is probably not a good idea because it breaks some conventions.)

Re: Can I mark table as userdata?

Posted: Thu Jun 04, 2015 6:55 pm
by deathbeam
I want to do this because as you said, userdata are pointers to C objects. I can convert Java to C objject, but in Java there is no way to define multiple return function because of all that converting to C. So I need to make table with multiple return function (what is possible with my implememtation) and then somehow mark it as userdata and when needed, return it back as Java object. Now when I am thinking about it, even that part to return Lua table as Java object is impossible. Fuck...

Re: Can I mark table as userdata?

Posted: Fri Jun 05, 2015 2:16 am
by I~=Spam
deathbeam wrote:I want to do this because as you said, userdata are pointers to C objects. I can convert Java to C objject, but in Java there is no way to define multiple return function because of all that converting to C. So I need to make table with multiple return function (what is possible with my implememtation) and then somehow mark it as userdata and when needed, return it back as Java object. Now when I am thinking about it, even that part to return Lua table as Java object is impossible. Fuck...
Hmmm sorry I am not entirely sure what you are saying (I know close to nothing about how java interacts with c...) But for passing values to java I recommend you either parse a table that is passed to a function (if possible) or you create a native function that takes all values passed to it and converts it to some sort of data structure. I hope that helps. :)

Re: Can I mark table as userdata?

Posted: Fri Jun 05, 2015 10:01 am
by deathbeam
I figured it out. I added new check to Java object -> C object converter where it checks if return value from Java object method is instance of specified array class and if yes it handles it as multiple return, loops throught it and returns that array length.

Re: Can I mark table as userdata?

Posted: Fri Jun 05, 2015 7:23 pm
by I~=Spam
Good! :) Sorry I couldn't be much (if any) help.