<i id='gOp3z'><tr id='gOp3z'><dt id='gOp3z'><q id='gOp3z'><span id='gOp3z'><b id='gOp3z'><form id='gOp3z'><ins id='gOp3z'></ins><ul id='gOp3z'></ul><sub id='gOp3z'></sub></form><legend id='gOp3z'></legend><bdo id='gOp3z'><pre id='gOp3z'><center id='gOp3z'></center></pre></bdo></b><th id='gOp3z'></th></span></q></dt></tr></i><div id='gOp3z'><tfoot id='gOp3z'></tfoot><dl id='gOp3z'><fieldset id='gOp3z'></fieldset></dl></div>
    1. <tfoot id='gOp3z'></tfoot>

        <bdo id='gOp3z'></bdo><ul id='gOp3z'></ul>
      <legend id='gOp3z'><style id='gOp3z'><dir id='gOp3z'><q id='gOp3z'></q></dir></style></legend>

      <small id='gOp3z'></small><noframes id='gOp3z'>

        编译lua代码,存储字节码然后加载执行

        Compile lua code, store bytecode then load and execute it(编译lua代码,存储字节码然后加载执行)
            <tbody id='WRCrq'></tbody>

              <i id='WRCrq'><tr id='WRCrq'><dt id='WRCrq'><q id='WRCrq'><span id='WRCrq'><b id='WRCrq'><form id='WRCrq'><ins id='WRCrq'></ins><ul id='WRCrq'></ul><sub id='WRCrq'></sub></form><legend id='WRCrq'></legend><bdo id='WRCrq'><pre id='WRCrq'><center id='WRCrq'></center></pre></bdo></b><th id='WRCrq'></th></span></q></dt></tr></i><div id='WRCrq'><tfoot id='WRCrq'></tfoot><dl id='WRCrq'><fieldset id='WRCrq'></fieldset></dl></div>

            1. <tfoot id='WRCrq'></tfoot>
                • <bdo id='WRCrq'></bdo><ul id='WRCrq'></ul>
                  <legend id='WRCrq'><style id='WRCrq'><dir id='WRCrq'><q id='WRCrq'></q></dir></style></legend>
                • <small id='WRCrq'></small><noframes id='WRCrq'>

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

                  问题描述

                  我正在尝试编译一个调用一些导出函数的 lua 脚本,将生成的字节码保存到一个文件中,然后加载这个字节码并执行它,但我还没有找到任何关于如何执行此操作的示例.有没有关于如何做到这一点的例子?我该怎么做?

                  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 + Luabind (C++)

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

                  推荐答案

                  这一切都很简单.

                  首先,您加载 Lua 脚本, 不执行它.是否已将 Lua 状态与导出的函数连接起来并不重要;您所做的就是编译脚本文件.

                  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.

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

                  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.

                  这两个函数都会根据 文档发出返回值和编译器错误对于 lua_load.

                  Both of these functions will emit return values and compiler errors as per the documentation for lua_load.

                  如果编译成功,lua_State 现在将编译后的 Lua 块作为 Lua 函数放在栈顶.要获得编译后的二进制文件,您必须使用 lua_dump 函数.它相当复杂,因为它使用回调接口来传递数据.请参阅文档了解详情.

                  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 字节码.将其推入您选择的文件中.请记住:将其写为二进制,而不是文本翻译.

                  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 具有启发式方法来检测字符串"是否存在.它是一个 Lua 源字符串或字节码.所以是的,您可以像以前一样使用 luaL_loadfile 加载字节码.

                  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.

                  区别在于您不能将 luaL_loadstring 与字节码一起使用.该函数需要一个以 NULL 结尾的字符串,这很糟糕.字节码可以在其中嵌入 NULL 字符,这会搞砸一切.所以如果你想自己做文件IO(因为你使用的是特殊的文件系统什么的),你必须直接使用lua_load(或luaL_loadbuffer).它还使用了一个回调接口,比如 lua_dump.所以阅读如何使用它.

                  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 (or luaL_loadbuffer). Which also uses a callback interface like lua_dump. So read up on how to use it.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                      <tbody id='JyAqY'></tbody>

                      <small id='JyAqY'></small><noframes id='JyAqY'>

                        <bdo id='JyAqY'></bdo><ul id='JyAqY'></ul>
                        <i id='JyAqY'><tr id='JyAqY'><dt id='JyAqY'><q id='JyAqY'><span id='JyAqY'><b id='JyAqY'><form id='JyAqY'><ins id='JyAqY'></ins><ul id='JyAqY'></ul><sub id='JyAqY'></sub></form><legend id='JyAqY'></legend><bdo id='JyAqY'><pre id='JyAqY'><center id='JyAqY'></center></pre></bdo></b><th id='JyAqY'></th></span></q></dt></tr></i><div id='JyAqY'><tfoot id='JyAqY'></tfoot><dl id='JyAqY'><fieldset id='JyAqY'></fieldset></dl></div>

                          <legend id='JyAqY'><style id='JyAqY'><dir id='JyAqY'><q id='JyAqY'></q></dir></style></legend>
                        • <tfoot id='JyAqY'></tfoot>