将所有内容保存在单个 lua 字节码块中? [英] Keeping everything in a single lua bytecode chunk?

查看:31
本文介绍了将所有内容保存在单个 lua 字节码块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 lua 与字节码块一起嵌入到用 C 编写的项目中.现在,当我通过添加 .lua 文件扩展我的 lua 代码库时,有没有办法将这些代码保存在单个字节码块中?

I've embedded lua together with a bytecode chunk into a project written in C. Now when I extend my lua code base by adding .lua files, is there a way to keep this code in a single bytecode chunk?

(我知道如何加载多个字节码块.但是让它加载单个块然后忘记粘合代码似乎很舒服.)

(I know how to load multiple bytecode chunks. But making it load a single chunk and then forgetting about the glue code would just seem comfortable.)

我尝试使用文本包含,但在 Lua 中似乎没有这个关键字."Require" 和 "dofile" 在运行时查看文件,因此运行 "lua -b ..." 后生成的字节码不会包含这些文件的代码.

I tried to use textual inclusion, but it seems there's no keyword for this in Lua. "Require" and "dofile" look at the files at run time, so the resulting bytecode after running "lua -b ..." won't include the code of those files.

而且也没有办法组合字节码文件,是吗?我的意思是,在创建字节码文件时,require"命令会将所有这些文件的代码添加到一个字节码文件中.

And there's no way for combining bytecode files either, is there? I mean so that, when creating a bytecode file, the "require" command would add the code of all those files into one bytecode file.

PS:Michal Kottman 的回答适用于 Lua,这正是我所要求的.我认为 Lua 和 LuaJIT 的工作方式相同.他们没有.要将多个 .lua 文件组合成一个 LuaJIT 字节码文件,应该一个

PS: Michal Kottman's answer works for Lua, which is what I asked for. I thought Lua and LuaJIT would work the same way. They don't. To combine multiple .lua files to one LuaJIT bytecode file, should one

  • 使用LuaJIT -b"(似乎不起作用)
  • 使用 LuaJIT 源编译 Lua 的 luac.c
  • 用 lua 命令模拟 luac.c(没有 C API)?

推荐答案

您可以使用 luac 将多个文件合并为一个文件.运行时,源文件中的所有块都按照它们添加到编译文件的顺序执行:

You can combine multiple files into a single file using luac. When run, all the chunks from the source files are executed in the order they were added to the compiled file:

$ echo "x=1"         > l1.lua
$ echo "y=2"         > l2.lua
$ echo "print(x, y)" > l3.lua
$ luac -o run.luac l1.lua l2.lua l3.lua
$ lua run.luac
1   2

您可以使用luaL_loadfile,如果它成功加载,它会将一个函数放在堆栈的顶部.然后你可以使用 lua_call 运行这个函数来运行所有组合的编译文件.

You can load this file into Lua from C using luaL_loadfile, which places a function on the top of the stack if it loaded succesfully. Then you can just run this function using lua_call to run all the combined compiled files.

请注意,您可以将编译文件的内容作为字符串嵌入到您的项目中,无需将其保留在外部文件中.

Note that you can embed the contents of the compiled file as a string into your project, no need to keep it in external file.

LuaJIT 2 更新

如您所见,您可以使用 Lua 中的 Lua 编译器 来获取一个组合文件,该文件可以作为先前指出.这是一个简化版本,输出到标准输出:

As you have found, you can use the Lua Compiler in Lua to get a combined file which can be loaded as previously noted. This is a simplified version, which outputs to stdout:

-- http://lua-users.org/wiki/LuaCompilerInLua
-- compile the input file(s) passed as arguments and output them combined to stdout
local chunk = {}
for _, file in ipairs(arg) do
  chunk[#chunk + 1] = assert(loadfile(file))
end
if #chunk == 1 then
  chunk = chunk[1]
else
  -- combine multiple input files into a single chunk
  for i, func in ipairs(chunk) do
    chunk[i] = ("loadstring%q(...);"):format(string.dump(func))
  end
  chunk = assert(loadstring(table.concat(chunk)))
end
io.write(string.dump(chunk))

对于前面的示例,您可以按如下方式使用:

For the previous sample, you can use it as follows:

$ luajit combine.lua l1.lua l2.lua l3.lua > out.ljc
$ luajit out.ljc
1   2

这篇关于将所有内容保存在单个 lua 字节码块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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