lua-users home
lua-l archive

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


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

Sure, Lua functions are just objects.

> I mean smth like lua_tofunction, lua_pushfunction.

However, Lua objects live in the Lua world :)

The easiest way to do it is to just use Lua:

-- test.lua

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

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

Table = {}
Table[1] = TestF1
Table[2] = TestF2

Table[1]()
Table[2]()

-- Output

This is T2!
This is T1!

-- Perhaps you find Table[1]() ugly. In that case:

Table = setmetatable({}, {__call =
  function(t, i)
    if t[i] then return t[i]()
     else error("Undefined table function at index "..i)
    end
  end
})

Table[1] = TestF1
Table[2] = TestF2
Table(1) -- !
Table(2)

-- Output as above.