1

I have functions process and matrix. The following code works

process(matrix({{2,4,6},{8,10,12},{14,16,20}}))

However the following doesn't work.

n='matrix({{2,4,6},{8,10,12},{14,16,20}})'
process(n)

It throws some error. The reason is obvious that process takes n as string rather than the output of the function matrix. So the basic difficulty involved here is about evaluating string from variable n and then give it as argument to the function process. Here loadstring function is of no use as matrix is local function and can't be referred from loadstring. Is there any work around for this? I hope that I have clearly stated the problem here. It is about evaluating (or unloading) string and then passing it as argument to another function. Any help will be appreciated. Thanks.

1 Answer 1

3

as matrix is local function

Lua takes local declarations seriously. If a variable is declared local, it can only be accessed by code which is statically within the local scope of that variable. Strings which you later turn into code are not statically in the local scope and therefore cannot access local variables.

Now, with Lua 5.2+, you can provide load with a second parameter, a table which represents the global environment against which that Lua chunk will be built. If that table contains a matrix value, then the loaded string can access it. For Lua 5.1, you'd have to use setfenv on the function returned to load to accomplish a similar effect. The Lua 5.2+ method would look like this:

local env = {matrix = matrix}
local func = load("return matrix({{2,4,6},{8,10,12},{14,16,20}})", nil, "t", env)
process(func())

Note the following:

  • You must create an explicit table which is the global environment. There's nothing you can pass that says "make my locals available"; you have to put every local you'd like to access there. Which is, generally speaking, why you pass these things as parameters or just make them globals.

  • You explicitly need the "return " there if you want to get the results of the call to matrix.

  • You have to call the function. Functions are values in Lua, so you can freely pass them around. But if you want to pass the results of a function to another function, you have to actually call it.

Sign up to request clarification or add additional context in comments.

5 Comments

How to do it with setfenv? For the current answer, I don't get where to put string n.
@Maths89: I don't understand what you mean by "where to put string n". I copied your string exactly, with the necessary addition of "return ". That's where you put n.
local func = load("return''..n, nil, "t", env) doesn't work
@Maths89: I don't see the space at the end of "return", the way it is in my example.
Yes It worked. Thanks for your great help. Could you signify the meaning of nil and "t" in load function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.