开发者

Lua Interpreter Question

开发者 https://www.devze.com 2023-01-26 19:26 出处:网络
How do I get the output from the following: lua_p开发者_JAVA技巧ushstring(L,\"print(i)\"); lua_call(L,0,0);

How do I get the output from the following:

lua_p开发者_JAVA技巧ushstring(L,"print(i)");
lua_call(L,0,0);


If you want to run arbitrary Lua code from C, what you need to use is luaL_dostring, as in this question: C & Lua: luaL_dostring return value

Edit: please note that Lua's default print function will still print its output straight to the console. You will need to redirect stdout in some way (probably to a pipe) if you want to capture its output.


That code shouldn't work at all. You're attempting to call a string. You need to push a function value onto the stack, then call lua_call.

lua_getglobal(L, "print");          // push print function onto the stack
lua_pushstring(L, "Hello, World!"); // push an argument onto the stack
lua_call(L,1,0);                    // invoke 'print' with 1 argument


If you mean the return value, it will be on the top of the stack.

If you meant the output from the print statement... that's a bit more difficult. The suggestion I read here is to replace print with a custom function that does what you need.

Of course, this is a bit complex, and I haven't touched lua in a while...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号