Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411579 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 12:46:36 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[LUA] How do I make a c function that takes a lua table as an argument?
Pages: [1]
Print
Author Topic: [LUA] How do I make a c function that takes a lua table as an argument?  (Read 4053 times)
Theodolite
Level 0
*


View Profile
« on: July 13, 2009, 04:52:41 PM »

n lua you can do this:function() to pass this as the first argument to function(), so I'm wondering, if I create a lua table in C/whatever like this

Code:
lua_newtable(ls)

lua_pushstring(ls,"fart")
lua_pushcfunction(ls,cat_fart)
lua_settable(ls,-3)

lua_pushstring(ls,"x")
lua_pushinteger(ls,415)
lua_settable(ls,-3)

lua_setglobal(ls,"cat")

In lua I'll be able to access cat.x , but if I wanted to do for example, cat:fart() , how do I need to write my cat_fart c function so that, when it's called with the cat table as the first argument, it can access whatever it is in that table? Like (pseudocode)

Code:
static int cat_fart(lua_state l)
{
  // take the table passed an argument    <- how do I do this
  // change the x value to something else <- and this?
};
Logged
Overkill
Level 3
***


Andrew G. Crowell


View Profile WWW
« Reply #1 on: July 13, 2009, 06:02:18 PM »

Well, chances are if you're binding a C object to Lua, you want to use userdata, not tables. But I can explain either.

Tables aren't going to handle well for any dynamic objects, like things you malloc/new into existance, because Lua doesn't magically know to free your stuff when it's done.


Anyway, that said, to get a table from Lua in a C function, it's already on the stack. You just need to use positive stack indexes to your table functions. Index 1 is the first argument, index 2 is the second argument, and so on. If you wanna push it to the top of the stack and copy the reference to it, use lua_pushvalue.


Userdata are like tables, but they have few extra things at their disposal to make them more flexible. One thing is a __gc metamethod, so that when the userdata is no longer is used, it's automatically garbage collected.

As far as the C API is concerned, using Lua tables vs userdata is pretty much identical, save for the fact that userdata have pointers associated with them. (use luaL_checkuserdata to get good typechecking in your userdata's methods)

You usually make a wrapper userdata that has a pointer to the C/C++ object you want to use. And, you use lua_newuserdata to create the wrapper structure, which Lua manages internally. Then you allocate the object you want to keep a hold of, and put a reference in your wrapper struct. Then you set a __gc metamethod (and make a C function to go with it) so that when the object escapes scope, it'll correctly delete/free your wrapper struct's reference.

Hopefully some of that made sense, I've been replying rather fast. If you need clarification, just yell back.
Logged

Theodolite
Level 0
*


View Profile
« Reply #2 on: July 13, 2009, 07:12:36 PM »

Quote
Anyway, that said, to get a table from Lua in a C function, it's already on the stack. You just need to use positive stack indexes to your table functions. Index 1 is the first argument, index 2 is the second argument, and so on. If you wanna push it to the top of the stack and copy the reference to it, use lua_pushvalue.

What I was having trouble with was exactly that but I found out a way to do what I wanted without userdata. Thanks anyway.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic