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

查看:107
本文介绍了将所有内容都保存在单个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 将文件从C加载到Lua中.如果成功加载,则在栈顶运行.然后,您可以使用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编译器来获取一个组合文件,该文件可以作为先前指出.这是一个简化的版本,输出到stdout:

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天全站免登陆