1

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!

3
  • Do you want to produce a standalone executable? Do you want to call C++ functions from lua? You should probably edit the question until you are fairly sure you are explaining it right. Commented Apr 11, 2017 at 21:44
  • The relevant C API functions are luaL_loadfile or luaL_loadstring and lua_dump. Commented Apr 11, 2017 at 22:15
  • Thank you! Exactly what I needed. Commented Apr 11, 2017 at 22:30

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.