C++ 编译lua代码,存储字节码然后加载执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8936369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile lua code, store bytecode then load and execute it
提问by WoLfulus
I'm trying to compile a lua script that calls some exported functions, save the resulting bytecode to a file and then load this bytecode and execute it, but I haven't found any example on how to do this. Is there any example available on how to do this? How can I do this?
我正在尝试编译一个调用一些导出函数的 lua 脚本,将生成的字节码保存到一个文件中,然后加载这个字节码并执行它,但我还没有找到任何关于如何执行此操作的示例。有没有关于如何做到这一点的例子?我怎样才能做到这一点?
Edit: I'm using Lua + Luabind (C++)
编辑:我正在使用 Lua + Luabind (C++)
回答by Nicol Bolas
This is all very simple.
这一切都非常简单。
First, you load the Lua script withoutexecuting it. It does not matter if you have connected the Lua state with your exported functions; all you're doing is compiling the script file.
首先,加载 Lua 脚本而不执行它。是否已将 Lua 状态与导出的函数连接起来并不重要;您所做的就是编译脚本文件。
You could use luaL_loadfile
, which uses C-standard library functions to read a file from disk and load it into the lua_State
. Alternatively, you can load the file yourself into a string and use luaL_loadstring
to load it into the lua_State
.
您可以使用luaL_loadfile
,它使用 C 标准库函数从磁盘读取文件并将其加载到lua_State
. 或者,您可以自己将文件加载到字符串中,然后使用luaL_loadstring
将其加载到lua_State
.
Both of these functions will emit return values and compiler errors as per the documentation for lua_load
.
这两个函数都会根据lua_load
.
If the compilation was successful, the lua_State
now has the compiled Lua chunk as a Lua function at the top of the stack. To get the compiled binary, you must use the lua_dump
function. It's rather complicated as it uses a callback interface to pass you data. See the documentation for details.
如果编译成功,则lua_State
现在将编译后的 Lua 块作为 Lua 函数放在堆栈顶部。要获得编译后的二进制文件,您必须使用该lua_dump
函数。它相当复杂,因为它使用回调接口来传递数据。有关详细信息,请参阅文档。
After that process, you have the compiled Lua byte code. Shove that into a file of your choice. Just remember: write it as binary, not with text translation.
在这个过程之后,你就有了编译好的 Lua 字节码。将其推入您选择的文件中。请记住:将其写为binary,而不是文本翻译。
When it comes time to load the byte code, all you need to do is... exactly what you did before. Well, almost. Lua has heuristics to detect that a "string" it is given is a Lua source string or byte code. So yes, you can load byte code with luaL_loadfile
just like before.
当需要加载字节码时,您需要做的就是……完全按照您之前所做的。嗯,差不多。Lua 具有启发式方法来检测给定的“字符串”是 Lua 源字符串或字节码。所以是的,您可以luaL_loadfile
像以前一样加载字节码。
The difference is that you can't use luaL_loadstring
with byte code. That function expects a NULL-terminated string, which is bad. Byte code can have embedded NULL characters in it, which would screw everything up. So if you want to do the file IO yourself (because you're using a special filesystem or something), you have to use lua_load
directly. Which also uses a callback interface like lua_dump
. So read up on how to use it.
不同之处在于您不能使用luaL_loadstring
字节码。该函数需要一个以 NULL 结尾的字符串,这很糟糕。字节码可以在其中嵌入 NULL 字符,这会搞砸一切。所以如果你想自己做文件IO(因为你使用的是特殊的文件系统什么的),你必须lua_load
直接使用。它还使用回调接口,如lua_dump
. 所以请仔细阅读如何使用它。