KSP-KOS/KOS

Setting suffix of a lock misaligns stack. was: "incorrect error responce in terminal"

nuggreat opened this issue · 2 comments

When this code is typed into the terminal

lock vec1 to facing:vector.
set vec to facing:vector.
lock offset to vec:direction * v(0, -10, 0).
set vec1:start to offset.

kOS will throw the error "KOSDelegate.InsertPreBoundArgs: Stack arg bottom missing" when it should be throwing "Suffix start not found on object".

This only occurs when the code is manually typed into the terminal when the code is executed from a file it throws the expected suffix not found error.

Trimmed down case of this problem:

lock myself to ship.
set myself:name to "this is my ship".

This works in a program as expected (renames the ship), and in the interpreter it throws the stack arg bottom missing error.

So the problem doesn't seem to be that it says stack arg bottom missing instead of saying suffix not found. The problem has nothing to do with whether the suffix was found. It says start arg bottom missing even when the suffix does exist and the command would normally have worked in a program.

I found out the problem DOES happen in the program case, but you have to have more code after the 'set thing:suffix to thing' command.

This example demonstrates the same problem happens in a program - just have the 'set' command happen twice:

lock myself to ship.
set myself:name to "this is my ship". // works
set myself:name to "this is my ship". // issues stack arg bottom missing error.

This helps. If it only happened in the interpreter, that made no sense. Happening in both makes more sense and I understand the problem now.

The problem is caused by this: When the lefthand side of a SET is a set-able suffix of a FUNCTION call, AND that function call is invoked without parentheses (this is important), then the compiler does not insert the KOSArgBottom on the stack before calling the function, but the function expects one to be there and it consumes it. Adding the parenthesis fixes the problem, like so:

// testprog_1.ks - This DOES crash:
function aaa {
  return ship.
}
set aaa:name to "this is my ship". // works
set aaa:name to "this is my ship". // issues stack arg bottom missing error.
// testprog_2.ks - This DOES NOT crash when "()" is used to call it.
function aaa {
  return ship.
}
set aaa():name to "this is my ship". // works
set aaa():name to "this is my ship". // works

Why it didn't fail in program mode when it was the last line: Because when calling a program, the interpreter puts a KOSArgBottom on the stack for the sake of the program call. The SET command did consume that marker, which it shouldn't have, which did misalign the stack, but it did so right at the end and then as part of exiting the program, it wipes the context as it quit, which wipes all that away anyway so the misalignment didn't matter. Once there was more code after that within the program (the second SET command) then the misaligned stack was relevant.