将luac-bytecode嵌入到C / C ++源文件中

问题描述 投票:1回答:1

如何将lua-bytecode-string包含到C / C ++文件中?

$ luac -o test -s test.lua
$ cat test
LuaS�

xV(w@@A@$@&�printTestj

现在,如果您可以将此字节字符串插入到C / C ++文件中,您实际上可以

luaL_loadfile(lua, bytestring.c_str());

这使得无需在运行时加载test.lua。你甚至不必在运行时解释test.lua,对吗?

更新:

对此问题的前两个注释有助于生成字节串,以便您可以将其包含在C / C ++代码中。 From this answer我有这个想法:

xxd -i test > test.h

这创造了这个:

unsigned char test[] = {
  0x1b, 0x4c, 0x75, 0x61, 0x53, 0x00, 0x19, 0x93, 0x0d, 0x0a, 0x1a, 0x0a,
  0x04, 0x08, 0x04, 0x08, 0x08, 0x78, 0x56, 0x00, /* ... */, 0x00};
unsigned int test_len = 105;

这很好,但是这不适用于luaL_loadstring,因为

此函数使用lua_load加载以零结尾的字符串s中的块。

注意:test中有零作为数据。

c++ lua bytecode luac
1个回答
2
投票

使用luaL_loadbuffer而不是luaL_loadstring

luaL_loadbuffer(L,test,test_len,"");
© www.soinside.com 2019 - 2024. All rights reserved.