简体   繁体   English

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

[英]Embed luac-bytecode into C/C++ source-file

How do you include a lua-bytecode-string into a C/C++ file ? 您如何在C / C ++文件中包含lua-bytecode-string?

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

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

Now if you could insert this byte-string into a C/C++-file, you could actually 现在,如果您可以将此字节字符串插入C / C ++文件,则实际上

luaL_loadfile(lua, bytestring.c_str());

which makes it unnecessary to load test.lua at run-time. 这样就不必在运行时加载test.lua You would not even have to interpret test.lua at run-time, right? 您甚至test.lua在运行时解释test.lua ,对吗?

Update: 更新:

The first two comments to this question were helpful for generating a bytestring so that you can include it in your C/C++ code. 该问题的前两个注释有助于生成字节字符串,以便您可以将其包括在C / C ++代码中。 From this answer I got this idea: 从这个答案我得到了这个主意:

xxd -i test > test.h

which creates this: 这创建了这个:

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;

This is great, but this will not work with luaL_loadstring , since 很好,但这不适用于luaL_loadstring ,因为

This function uses lua_load to load the chunk in the zero-terminated string s. 此函数使用lua_load将块加载到以零结尾的字符串s中。

Note: there are zeros as data in test . 注意: test数据为零

使用luaL_loadbuffer代替luaL_loadstring

luaL_loadbuffer(L,test,test_len,"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM