lua-users home
lua-l archive

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



On 13-May-05, at 7:51 AM, Nick wrote:

Simple solution:

function myloadstring(str, name)
local f, err = loadstring("return function (arg) " .. str .. " end", name or str)
  if f then return f() else return f, err end
end

> class = { x = 10, y = 20 }
> f = myloadstring(' print("X:", arg.x, " Y:", arg.y) ')
> f(class)
X:      10       Y:     20
> = myloadstring(' print("X:", arg.x, " Y:", arg.y ')
nil [string " print("X:", arg.x, " Y:", arg.y "]:1: `)' expected near `end'
> = myloadstring(' print("X:", arg.x, " Y:", arg.y ', "Frobnicator")
nil     [string "Frobnicator"]:1: `)' expected near `end'

Of course, you could just redefine loadstring to do that:

do
  local old_loadstring = loadstring

  function myloadstring(str, name)
local f, err = old_loadstring("return function (arg) " .. str .. " end", name or str)
    if f then return f() else return f, err end
  end
end


Is there a way to pass arguments to loadstring in Lua 5.0.2 (i know that in 5.1
it is possible).
Here is my code I would like to run:

class = {}
class.x=10
class.y=20

f=loadstring( ' print("X:",arg.x," Y:",arg.y ) ')
f(class)

Best Regards
Nick