[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: So close! Now: userdata mt help!
- From: Ando Sonenblick <ando@...>
- Date: Wed, 30 Jul 2003 13:00:19 -0700
Huge thanks to everyone on the list!
I've got 95% of what I need to have lua fully embedded in our app! So far
so good and I'm down to one last thing to implement!
Here is what I'm trying to do (correct me if it's not even possible);
Description in lua terms:
I want to create a variable "foo", whose type is of userdata. Then I want to
set foo's metatable to something like
{
__index = {
DoSomething = function(self, x) <guts> end
}
}
So that I can then call:
foo:DoSomething(100)
First, is this possible to do with a userdata value type?
If not, stop me right here and let me know! !!!!!!!!
If it is possible, can anyone give me some help on how to do this from my c
code?
Here is what I've been trying but execution of foo:DoSomething(100) returns
an error. I'm trying to create foo by pushing it onto the stack (and later
calling a function).
First I create the meta table by having lua interpret the following text,
assigning the to a global variable (I've confirmed this works):
FooMetaTable =
{
__index = {
DoSomething = function(self, x) <guts> end
}
}
I then lua_load (and call to register main) this (also confirmed as
working):
"function main(object)
object:DoSomething(100)
end"
And then to create foo I:
lua_pushlightuserdata(itsLuaState, 0x400); -- 0x400 is placeholder data
top = lua_gettop(itsLuaState);
lua_pushstring(itsLuaState, "FooMetaTable");
lua_gettable(itsLuaState, LUA_GLOBALSINDEX);
if (lua_istable(itsLuaState, -1) == true)
lua_setmetatable(itsLuaState, top - 1);
Then I call the function, assuming that the userdata is on the stack and
will be passed to main:
lua_pushstring(itsLuaState, "main");
lua_gettable(itsLuaState, LUA_GLOBALSINDEX);
if (lua_isfunction(itsLuaState, -1) == true)
lua_pcall
Is this correct? I have a feeling I'm not passing the correct index to set
metatable. I've tried passing just top and top - 1 but to no avail.
I'm 100% confident my loading and calling of code is correct as it works
perfectly for everything else I've been doing. It's just the
userdata/metatable stuff that is bogus...
Anyone have any suggestions for me?
As usual, thanks!
Ando
-----------------
SpriTec Software
www.spritec.com