JakobOvrum/LuaD

Return value of last statement/chunk?

Closed this issue · 4 comments

Is there a way to access the value returned from the last statement to be executed? LuaState.doString returns void, but I'd like to get the value returned by the operation for a REPL-like environment.

If there is no facility for this now, is this an easy feature to add?

Added LuaState.loadString and LuaState.loadFile and a unittest.

I don't have a testing environment on this computer, but I'm setting it up now, so it may or may not work atm.

The unittest fails at line 333 and there's some weird behavior going on--the results of calling a compiled function seem to include previous results as well. Here's a little test I threw together:

module luatest;
import std.stdio;
import luad.all;

pragma(lib, "dl");

void main()
{
    auto lua = new LuaState;
    lua.openLibs();
    LuaFunction func = lua.loadString("return 10 + 1");
    writeln(func());

    string hello() { return "Hello world"; };
    lua.set("greet", &hello);

    auto func2 = lua.loadString("greet()");
    writeln(func2());

}

Output:
[function: 0x8c72708, 11]
[function: 0x8c72708, 11, function: 0x8c72a98]

--Edit: actually, it looks more like the compiled functions are returning the contents of the stack.

Pushed a patch for loadString and loadFile so that they pop (like most other functions... haven't been working on this project for a while). LuaFunction does indeed dump the stack when called like that - but that shouldn't be a problem, the getStack function resets the stack after creating the LuaObject array. Hence I'm not sure why 11 still sticks around on the stack for the second call.

I tested your code against the current head, and it seems this bug was fixed somewhere along the line.