Advertisement

Lua/C++: Keeping the index of a function/table after calling lua_getglobal so next time not needed

Started by March 18, 2011 05:34 PM
11 comments, last by kdmiller3 13 years, 8 months ago
Say I call a Lua function from C++ like this

lua_getglobal(L, "TableName");
lua_getfield(L, -1, "FunctionName");
lua_pushinteger(L, 123); // parameter
lua_pcall(L, 1, 0, 0);



I'm guessing that Lua will hash the names and search that value in a table to get their indices. How would I go for saving this index values so next time I just push those indices instead of calling lua_getglobal? That could save some time.
[size="2"]I like the Walrus best.
You can use a reference to speed things up a bit.

Save the function into the Lua registry with luaL_ref:

int r = luaL_ref(L, LUA_REGISTRYINDEX);


Pull the reference back onto the Lua stack with lua_rawgeti:

lua_rawgeti(L, LUA_REGISTRYINDEX, r);


Release the reference with luaL_unref:

luaL_unref(L, LUA_REGISTRYINDEX, r);

Advertisement

You can use a reference to speed things up a bit.

Save the function into the Lua registry with luaL_ref:

int r = luaL_ref(L, LUA_REGISTRYINDEX);


Pull the reference back onto the Lua stack with lua_rawgeti:

lua_rawgeti(L, LUA_REGISTRYINDEX, r);


Release the reference with luaL_unref:

luaL_unref(L, LUA_REGISTRYINDEX, r);




Hi! So how would I go with it? I'm trying this but it says I'm trying to call a nill value:



lua_getglobal(L, "TableName"); // Get the table instance
r_obj = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, -1, "FunctionName"); // Get the function
r_func = luaL_ref(L, LUA_REGISTRYINDEX);


lua_rawgeti(L, LUA_REGISTRYINDEX, r_obj);
lua_rawgeti(L, LUA_REGISTRYINDEX, r_func);
int s;
if( (s=lua_pcall(L,0,0,0)) != 0)
report_errors(L, s);



Clearly I am doing something wrong...
[size="2"]I like the Walrus best.

Clearly I am doing something wrong...




Clearly you are and I am a little confused after you said this only yesterday

Why can't you do this with SWIG?
[quote name='owl']SWIG automagically generates all the required c/c++ code needed to share whatever you need with Lua scripts

So instead I will just say RTFM.

[quote name='owl' timestamp='1300474722' post='4787627']
Clearly I am doing something wrong...




Clearly you are and I am a little confused after you said this only yesterday

Why can't you do this with SWIG it?
[quote name='owl']SWIG automagically generates all the required c/c++ code needed to share whatever you need with Lua scripts

So instead I will just say <a href="http://www.lua.org/manual/5.1/manual.html#lua_pcall">RTFM</a>.
[/quote]

I do need to call Lua functions from c++. I have a special helper class for that that I instance in the Lua script and my engine never sees. This helper class holds any values I need to pass to the Lua function which I access directly from the script. Your rant in my other thread was about passing parameters from C++ not about calling Lua functions.

Besides, what's your purpose on this site? Being a bitch? You have been anything but helpfull. So instead I will just say GTFOOH.
[size="2"]I like the Walrus best.
[color=#CCCCCC][size=2]I do need to call Lua functions from c++. I have a special helper class for that that I instance in the Lua script and my engine never sees. This helper class holds any values I need to pass to the Lua function which I access directly from the script. Your rant in my other thread was about passing parameters from C++ not about calling Lua functions.

Besides, what's your purpose on this site? Being a bitch? You have been anything but helpfull. So instead I will just say GTFOOH.[/quote]
[color="#CCCCCC"]But the other thread was calling a function?
[color="#CCCCCC"]Anyway I am not a bitch but thanks for your concern, any more problems you have I will be sure to avoid.
[color="#CCCCCC"]"[color=#CCCCCC][size=2]You have been anything but helpfull" [color=#CCCCCC][size=2]Have you read the link to the manual I posted yet?
Advertisement

[color="#CCCCCC"] [color="#CCCCCC"]Have you read the link to the manual I posted yet?


Yes, several times before you posted it. Now, could you explain how that link answers the question I made?

No wait, that would be actually helpful...
[size="2"]I like the Walrus best.

[quote name='dmail' timestamp='1300480382' post='4787674']
[color="#CCCCCC"]Have you read the link to the manual I posted yet?


Yes, several times before you posted it. Now, could you explain how that link answers the question I made?

No wait, that would be actually helpful...
[/quote]

lua_pcall needs a function and there is not one on the stack which is why you get the error about trying to call nil. The reason is because luaL_ref returns a reference and also pops the value of the stack. If you wanted to kept a copy like that, you could call lua_pushvalue to duplicate the entry on the stack or pull using luaL_ref then push back using lua_rawgeti.



Eventually you may not even want to store the table to which the function belongs and just store the function itself, I am not sure if this was the intention when pushing them before the pcall as you do not signal any parmeters to the function.

or pull using luaL_ref then push back using lua_rawgeti.


Isn't that exactly what I'm trying to do here?



lua_getglobal(L, "TableName"); // Get the table instance
r_obj = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, -1, "FunctionName"); // Get the function
r_func = luaL_ref(L, LUA_REGISTRYINDEX);


lua_rawgeti(L, LUA_REGISTRYINDEX, r_obj);
lua_rawgeti(L, LUA_REGISTRYINDEX, r_func);
int s;
if( (s=lua_pcall(L,0,0,0)) != 0)
report_errors(L, s);
[size="2"]I like the Walrus best.

[quote name='dmail' timestamp='1300483573' post='4787699']
or pull using luaL_ref then push back using lua_rawgeti.


Isn't that exactly what I'm trying to do here?



lua_getglobal(L, "TableName"); // Get the table instance
r_obj = luaL_ref(L, LUA_REGISTRYINDEX);//pulls the table of stack and creates a reference
lua_getfield(L, -1, "FunctionName"); // Get the function : Doesnt IIRC would push nil
r_func = luaL_ref(L, LUA_REGISTRYINDEX); //gets a reference to nil


lua_rawgeti(L, LUA_REGISTRYINDEX, r_obj);//push the table
lua_rawgeti(L, LUA_REGISTRYINDEX, r_func);//push nil
int s;
if( (s=lua_pcall(L,0,0,0)) != 0)//call nil
report_errors(L, s);

[/quote]




lua_getglobal(L, "TableName");//table
lua_getfield(L, -1, "FunctionName");//table func
r_func = luaL_ref(L, LUA_REGISTRYINDEX); //table
lua_pop(L,1);


..
lua_rawgeti(L, LUA_REGISTRYINDEX, r_func);//func
int s;
if( (s=lua_pcall(L,0,0,0)) != 0)
report_errors(L, s);


This topic is closed to new replies.

Advertisement