如何将 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++ 代码中。 从这个答案我得到了这个想法:
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
。