编译lua代码,存储字节码,然后加载并执行它 [英] Compile lua code, store bytecode then load and execute it

查看:2780
本文介绍了编译lua代码,存储字节码,然后加载并执行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译一个lua脚本,调用一些导出的函数,将生成的字节码保存到一个文件,然后加载这个字节码并执行它,但我还没有找到任何示例如何做到这一点。有什么例子可以做到这一点吗?我如何做到这一点?



编辑:我使用Lua + Luabind(C ++)

解决方案

这很简单。



首先,加载Lua脚本如果你已经连接Lua状态和你的导出函数没关系;



您可以使用 luaL_loadfile ,它使用C标准库函数从磁盘读取文件并将其加载到 lua_State 中。或者,您可以将文件自己加载到字符串中,并使用 luaL_loadstring 将其加载到 lua_State 中。 >

这两个函数都会根据 lua_load 的文档。



如果编译成功, lua_State 现在已将编译的Lua块作为堆栈顶部的Lua函数。要获得编译的二进制文件,必须使用 lua_dump 函数。这是相当复杂,因为它使用回调接口传递您的数据。有关详情,请参见文档



之后,就有了编译后的Lua字节码。把它放到你选择的文件中。只要记住:把它写成 binary ,而不是文本翻译。



当加载字节代码时,所有你需要做的是... 正是您在之前做的。好吧,差不多。 Lua有启发式方法来检测它给出的字符串是一个Lua源字符串或字节码。所以是的,你可以加载字节码与 luaL_loadfile 就像以前一样。



不同的是,你不能用字节码使用 luaL_loadstring 。该函数需要一个NULL结尾的字符串,这是坏的。字节代码可以有嵌入的NULL字符,它会拧紧一切。因此,如果你想自己做文件IO(因为你使用一个特殊的文件系统或其他),你必须直接使用 lua_load 。它也使用一个回调接口,如 lua_dump 。因此请阅读如何使用


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?

Edit: I'm using Lua + Luabind (C++)

解决方案

This is all very simple.

First, you load the Lua script without executing 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.

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.

Both of these functions will emit return values and compiler errors as per the documentation for 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.

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.

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.

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.

这篇关于编译lua代码,存储字节码,然后加载并执行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆