Lua 获取对使用';执行的字符串中的参数的访问权限;加载';

Lua 获取对使用';执行的字符串中的参数的访问权限;加载';,lua,Lua,我想知道“load”函数是否有办法从局部变量而不是全局变量中获取变量值 假设我有一个像'trade.version==2'这样的字符串,我想在一个以trade为参数的函数中使用'load'函数执行它 function doTest( trade, test ) -- inside the string 'test', I would like that any reference to 'trade' -- refer to the function parameter instead

我想知道“load”函数是否有办法从局部变量而不是全局变量中获取变量值

假设我有一个像'trade.version==2'这样的字符串,我想在一个以trade为参数的函数中使用'load'函数执行它

function doTest( trade, test )
  -- inside the string 'test', I would like that any reference to 'trade'
  -- refer to the function parameter instead of a global variable
  if ( assert(load("return "..test))() ) then
    -- do something
  end
end

a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )

[string "return trade.version == 2"]:1: attempt to index global 'trade' (a nil value)
stack traceback:
    [string "return trade.version == 2"]:1: in main chunk
    stdin:2: in function 'doTest'
    stdin:1: in main chunk
    [C]: in ?
作为一种解决方法,我定义了一个全局变量,它工作得很好。 但我想避免这个全局变量


非常感谢您

您计划使用什么Lua版本?太好了!非常感谢你
function doTest( trade, test )
  -- inside the string 'test', I would like that any reference to 'trade'
  -- refer to the function parameter instead of a global variable
  if assert(load("local trade = ...; return "..test))(trade) then
    -- do something
    print('Yes, version equals to 2')
  end
end

a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )