lua-users home
lua-l archive

Re: How to call a function not by its name in lua script?

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


Graham Wakefield wrote:
_G["print"]("hello to you too")
On Dec 12, 2006, at 12:46 AM, Austin Xu Wang wrote:
$ lua
Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
array={print}
array[1]("Hello World!")
Hello World!
I think the question was not "how to call a function indirectly", but "how to call the n-th function of the, say, iolib array?"
The answer is: you can't. Not in the Lua side, at least.
If you want to optimize your code, make the function local - Lua will use a local by it's index, avoiding a table lookup.
Example:
local print = print -- or '_G.print' or 'mypackage.print'
function main()
 print("hello wolrd");
end -- "end function" ?
main()
Inspecting the output of luac -l, you will see:
> cat > test.lua
function main()
 print("hello world")
end
> luac50 -l test.lua
main <test.lua:0> (3 instructions, 12 bytes at 00325910)
0 params, 2 stacks, 0 upvalues, 0 locals, 1 constant, 1 function
 1 [3] CLOSURE 0 0 ; 00325A70
 2 [1] SETGLOBAL 0 0 ; main
 3 [3] RETURN 0 1 0
function <test.lua:1> (4 instructions, 16 bytes at 00325A70)
0 params, 2 stacks, 0 upvalues, 0 locals, 2 constants, 0 functions
 1 [2] GETGLOBAL 0 0 ; print
 2 [2] LOADK 1 1 ; "hello world"
 3 [2] CALL 0 2 1
 4 [3] RETURN 0 1 0
And the version with locals:
> cat > test-local.lua
local print = print
function main()
 print("hello world")
end
> luac50 -l test-local.lua
main <test-local.lua:0> (5 instructions, 20 bytes at 00325918)
0 params, 2 stacks, 0 upvalues, 1 local, 2 constants, 1 function
 1 [1] GETGLOBAL 0 0 ; print
 2 [4] CLOSURE 1 0 ; 00325BA8
 3 [4] MOVE 0 0 0
 4 [2] SETGLOBAL 1 1 ; main
 5 [4] RETURN 0 1 0
function <test-local.lua:2> (4 instructions, 16 bytes at 00325BA8)
0 params, 2 stacks, 1 upvalue, 0 locals, 1 constant, 0 functions
 1 [3] GETUPVAL 0 0 0 ; print
 2 [3] LOADK 1 0 ; "hello world"
 3 [3] CALL 0 2 1
 4 [4] RETURN 0 1 0
Note that in the second version, instead of querying the global table with GETGLOBAL, the function 'main' called GETUPVAL, which retrieve an upvalue by it's index, which is very fast.
--rb
On 12/12/06, BrillyWu <brillywu@gmail.com> wrote:
In lua baselib.c,it give us a lot of base function,such as:
static const struct luaL_reg iolib[] = {
 {LUA_ERRORMESSAGE, errorfb},
 {"clock", io_clock},
 {"date", io_date},
 {"debug", io_debug},
 {"execute", io_execute},
 {"exit", io_exit},
 {"getenv", io_getenv},
 {"remove", io_remove},
 {"rename", io_rename},
 {"setlocale", setloc},
 {"tmpname", io_tmpname}
};
static const struct luaL_reg iolibtag[] = {
 {"appendto", io_appendto},
 {"closefile", io_close},
 {"flush", io_flush},
 {"openfile", io_open},
 {"read", io_read},
 {"readfrom", io_readfrom},
 {"seek", io_seek},
 {"write", io_write},
 {"writeto", io_writeto}
};
and in my project,I definded a lot of function of myself.and register to
lua.
in Lua script,I can use this function:
test.lua
function main()
 print("hello wolrd");
end function
main();
-----------------------------
I use the function by its funciton name:"print",How can I call print
function,not by its function name,but use the index of the function in the
array?

AltStyle によって変換されたページ (->オリジナル) /