Discussion:
loadstring to create a function with parameters
Terisquas Brothers
2006-12-17 13:18:16 UTC
Permalink
I have this code:

function funcXY(param) print (param); end;


This function is used in a coroutine which receives
some parameters. Sometimes I have to restart a
coroutine, however the file has probably been modified
and reloaded, so I can't store the function funcXY "as
is", so I have to its name "funcXY"

my idea would be doing something like

funcName="funcXY";
loadstring (funcName.."()"); --<-- this would be the
function to be called, with a parameter

however loadstring expands this to:

function () funcXY(); end;

where no parameters can be passed to funcXY

Is there any way to convert the string funcName to a
function which accepts a single parameter?

TIA
Kak





___________________________________________________________
Try the all-new Yahoo! Mail. "The New Version is radically easier to use" – The Wall Street Journal
http://uk.docs.yahoo.com/nowyoucan.html
Asko Kauppi
2006-12-17 13:52:02 UTC
Permalink
If you need to get return values via loadstring, how about:

loadstring( "return "..funcName.."()" ) ()

..But I fail to see why you couldn't simply:

funcXY()

Functions don't have names in Lua; they are just values in variables.
So you can toss them around as you would any value. Hope this helps?

-asko
Post by Terisquas Brothers
function funcXY(param) print (param); end;
This function is used in a coroutine which receives
some parameters. Sometimes I have to restart a
coroutine, however the file has probably been modified
and reloaded, so I can't store the function funcXY "as
is", so I have to its name "funcXY"
my idea would be doing something like
funcName="funcXY";
loadstring (funcName.."()"); --<-- this would be the
function to be called, with a parameter
function () funcXY(); end;
where no parameters can be passed to funcXY
Is there any way to convert the string funcName to a
function which accepts a single parameter?
TIA
Kak
___________________________________________________________
Try the all-new Yahoo! Mail. "The New Version is radically easier
to use" – The Wall Street Journal
http://uk.docs.yahoo.com/nowyoucan.html
Javier Guerra
2006-12-17 14:10:08 UTC
Permalink
Post by Terisquas Brothers
Is there any way to convert the string funcName to a
function which accepts a single parameter?
a global function is just an anonymous function stored as a named field in the
environment variable, usually _G

therefore:

funcXY()

is the same as:

_G["funcXY"] ()

in fact, i guess both compile to the same bytecode. (maybe using a constant
reference for the environment, instead of _G)

so, even if you have reloaded some code, executing the first form should fetch
the 'new' global function from the environment
--
Javier
Rici Lake
2006-12-17 16:09:04 UTC
Permalink
Post by Javier Guerra
Post by Terisquas Brothers
Is there any way to convert the string funcName to a
function which accepts a single parameter?
a global function is just an anonymous function stored as a named field in the
environment variable, usually _G
funcXY()
_G["funcXY"] ()
in fact, i guess both compile to the same bytecode. (maybe using a constant
reference for the environment, instead of _G)
_G is not magic, it's just another global. It might not even refer to
the current function environment.

In 5.1, chunks are compiled as vararg functions, not as functions of no
arguments. So a chunk can acquire its arguments easily:

-- foo.lua
local a, b, c = ...


If you want to do that with earlier versions of Lua, you can use the
"double call" mechanism:

chunk = loadstring("return function(a, b, c) " .. chunkstring .. "
end")()

Note the extra () to do the second call; you might want to replace that
with a pcall:

chunk = pcall(loadstring("return function(a, b, c) "
.. chunkstring .. " end"))
Terisquas Brothers
2006-12-22 14:23:15 UTC
Permalink
Post by Rici Lake
In 5.1, chunks are compiled as vararg functions, not
as functions of no
arguments. So a chunk can acquire its arguments
-- foo.lua
local a, b, c = ...
thanks! It works wonderfully! :)



____________________________________________________

Yahoo! Photos is now offering a quality print service from just 7p a photo. http://uk.photos.yahoo.com
Loading...