简体   繁体   中英

Embed luac-bytecode into C/C++ source-file

How do you include a lua-bytecode-string into a C/C++ file ?

$ 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

luaL_loadfile(lua, bytestring.c_str());

which makes it unnecessary to load test.lua at run-time. You would not even have to interpret test.lua at run-time, right?

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. 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

This function uses lua_load to load the chunk in the zero-terminated string s.

Note: there are zeros as data in test .

使用luaL_loadbuffer代替luaL_loadstring

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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