Settop
Settop

Reputation: 71

How to get a lua script into lua bytecode?

If I had a lua script, say

print'hi'

How would I get the lua bytecode equivalent to it using c++? I'm not sure if I'm explaining this right though. Thanks for all your help!

Upvotes: 1

Views: 2141

Answers (1)

lhf
lhf

Reputation: 72312

You need to load a script and then dump its bytecode.

The relevant C API functions are luaL_loadfile or luaL_loadstring for loading (they use the primitive lua_load) and lua_dump for dumping.

Loading is easy to do with these helper functions.

Dumping is a bt more work because of the need to provide a writer function. It may be easier to call string.dump after loading:

// load script, leave function on the stack
lua_getglobal(L,"string");
lua_getfield(L,"dump");
lua_pushvalue(L,-3);
lua_call(L,1,1);
// string containing bytecode left on the stack

Upvotes: 1

Related Questions