我正在尝试实现一个简单的C++函数,它检查Lua脚本的语法.为此,我使用Lua的编译器函数luaL_loadbufferx()并在之后检查其返回值.
最近,我遇到了一个问题,因为我认为应该被标记为无效的代码未被检测到,而是稍后脚本在运行时(例如,在lua_pcall())中失败.
示例Lua代码(可在官方Lua演示中测试):
function myfunc()
return "everyone"
end
-- Examples of unexpected behaviour:
-- The following lines pass the compile time check without errors.
print("Hello " .. myfunc() "!") -- Runtime error: attempt to call a string value
print("Hello " .. myfunc() {1,2,3}) -- Runtime error: attempt to call a string value
-- Other examples:
-- The following lines contain examples of invalid syntax, which IS detected by compiler.
print("Hello " myfunc() .. "!") -- Compile error: ')' expected near 'myfunc'
print("Hello " .. myfunc() 5) -- Compile error: ')' expected near '5'
print("Hello " .. myfunc() .. ) -- Compile error: unexpected symbol near ')'
Run Code Online (Sandbox Code Playgroud)
显然,目标是在编译时捕获所有语法错误.所以我的问题是:
luaL_loadbufferx()在这个特定的例子中有问题?lua_pcall().注意:我使用的是Lua版本5.3.4(此处为手册).
非常感谢您的帮助.
| 归档时间: |
|
| 查看次数: |
341 次 |
| 最近记录: |