Skip to content

Commit

Permalink
Fix missing stack element
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Sep 24, 2024
1 parent 3aea236 commit 13403bd
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 22 deletions.
10 changes: 1 addition & 9 deletions src/main/java/org/itsallcode/luava/LowLevelLua.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ void pcall(final int nargs, final int nresults, final int errfunc) {
void pcallk(final int nargs, final int nresults, final int msgHandler, final long ctx,
final lua_KFunction.Function upcallFunction) {
final MemorySegment k = upcallFunction == null ? Lua.NULL() : lua_KFunction.allocate(upcallFunction, arena);
checkStatus("lua_pcallk", () -> {
final int status = Lua.lua_pcallk(state, nargs, nresults, msgHandler, ctx, k);
if (msgHandler != 0) {
this.stack.pop(1);
}
return status;
});
checkStatus("lua_pcallk", () -> Lua.lua_pcallk(state, nargs, nresults, msgHandler, ctx, k));
}

void loadString(final String chunk) {
Expand All @@ -70,8 +64,6 @@ void loadString(final String chunk) {
void checkStatus(final String functionName, final Supplier<Integer> nativeFunctionCall) {
final int status = nativeFunctionCall.get();
if (status != Lua.LUA_OK()) {
System.out.println(stack.printStack());
System.out.println("Getting error message..");
final String message = stack.toString(-1);
stack.pop(1);
throw new FunctionCallException(functionName, status, message);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/itsallcode/luava/LuaStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,11 @@ void pop(final int n) {
if (n > size) {
throw new IllegalStateException("Trying to pop " + n + " elements but stack has size " + size);
}
System.out.println("Popping " + n + " elements from stack with size " + size);
setTop(-n - 1);
}

void setTop(final int n) {
System.out.println(this.printStack());
Lua.lua_settop(state, n);
System.out.println(this.printStack());
}

boolean toBoolean(final int idx) {
Expand Down
12 changes: 2 additions & 10 deletions src/test/java/org/itsallcode/luava/LuaInterpreterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,18 @@ void getCallGlobalFunctionFails() {

@Test
void getCallGlobalFunctionWithMessageHandler() {
System.out.println(lua.stack().printStack());
lua.exec("function increment(x) error('failure') end");
System.out.println(lua.stack().printStack());
assertStackSize(0);
final LuaFunction function = lua.getGlobalFunction("increment", (final LuaInterpreter l) -> {
System.out.println(l.stack().printStack());
final String msg = l.stack().toString(-1);
// l.stack().pop(1);
System.out.println("Updated error: " + msg);
l.stack().pop(1);
l.stack().pushString("Updated error: " + msg);
System.out.println(l.stack().printStack());
return 1;
});
System.out.println("before add arg" + lua.stack().printStack());
function.addArgInteger(42);
System.out.println("after add arg" + lua.stack().printStack());
final FunctionCallException exception = assertThrows(FunctionCallException.class, () -> function.call(1, 1));
System.out.println("after call" + lua.stack().printStack());
assertThat(exception.getMessage(), equalTo(
"Function 'lua_pcallk' failed with error 2: [string \"function increment(x) error('failure') end\"]:1: failure"));
"Function 'lua_pcallk' failed with error 2: Updated error: [string \"function increment(x) error('failure') end\"]:1: failure"));
assertStackSize(0);
}

Expand Down

0 comments on commit 13403bd

Please sign in to comment.