## Can I mark table as userdata?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
deathbeam
Prole
Posts: 39
Joined: Mon Mar 30, 2015 5:57 pm
Location: Slovakia
Contact:

### Can I mark table as userdata?

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


Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Can I mark table as userdata?

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?

ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

### Re: Can I mark table as userdata?

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!
ejmr :: Programming and Game-Dev Blog, GitHub

I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

### Re: Can I mark table as userdata?

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.)
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610

deathbeam
Prole
Posts: 39
Joined: Mon Mar 30, 2015 5:57 pm
Location: Slovakia
Contact:

### Re: Can I mark table as userdata?

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

I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

### Re: Can I mark table as userdata?

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.
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610

deathbeam
Prole
Posts: 39
Joined: Mon Mar 30, 2015 5:57 pm
Location: Slovakia
Contact:

### Re: Can I mark table as userdata?

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.

I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

### Re: Can I mark table as userdata?

Good! Sorry I couldn't be much (if any) help.
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610

### Who is online

Users browsing this forum: Google [Bot], joaolemos and 3 guests