Lua中包含函数的loadstring()

假设有这段备忘录文本:

Memo.Lines.Text = 'print('Hello World')
local test = Memo.Lines.Text
loadstring(test) ()

--结果: Hello World

如果像这样:

Memo.Lines.Text = [[
function playTest()
 local a = 10
 local b = 20
 local c = a + b
 print('This is a test of a math calculation: ')
 print(a..' + '..b..' is '..c)
end
]]

local test = Memo.Lines.Text
loadstring(test) ()
playTest()

-- 结果: error attempt to call a nil value 'playTest'

如何从字符串/作为备忘录文本返回函数?

点赞