lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I may just be dense, but I don't understand what you are asking.
Are you saying that you want to do the following?...

------
FuncTable = {}
FuncTable[1] = TestF1
FuncTable[2] = TestF2

FuncTable[1]()
FuncTable[2]()
-------

Or perhaps are you confused as to how to use the C API to pick up a function
from the stack and move it to another location on the stack (in preparation
for adding it to a table, calling it, passing it as a parameter to another
function, etc.)?

If that is the case then the API functions you need are lua_pushvalue,
lua_replace and/or lua_insert.  lua_pushvalue pushes a copy of a value at a
specified stack location onto the top of the stack (be it number, function,
string, userdata, whatever).  lua_replace moves a value from the top of the
stack to the specified stack location (overwriting the previous contents of
that location).  lua_insert works like lua_replace except that it moves the
contents of the specified location and all higher locations up one level
rather than overwriting any of the stack contents.

-------
If I'm way off on both of these interpretations then I sugest you clarify
your question so that you can get the response you want.


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Antero Vipunen
Sent: Wednesday, November 05, 2003 8:53 AM
To: Lua List
Subject: Getting Lua functions from stack


Hello All,

Is there a way to get from  and push to stack lua functions?

I mean smth like lua_tofunction, lua_pushfunction.

I want to do the following:

-- Test.c
LuaFunc *table[100];
.....
static int AddToTable(lua_State *L)
{
....
 table[lua_tonumber(L,1)] = lua_tofunction(L,2);
....
}

static int CallFromTable(lua_State *L)
{
...
  lua_pushfunction(table[lua_tonumber(L,1)]);
  lua_call(L,0,0);
...
}
....
lua_State *L = lua_open();
....
lua_dofile(L,"test.lua");
....

-- test.lua

function TestF1()
 print("This Is F1!");
end

function TestF2()
 print("This Is F2!");
end


AddToTable(1,TestF1);
AddToTable(2,TestF2);

CallFromTable(2);
CallFromTable(1);

-- Output

This is T2!
This is T1!






--
Best regards,
  Antero Vipunen