Skip to content

fix(ncs): push engine call arguments last-first (#351) - #352

Closed
addder wants to merge 1 commit into
OpenKotOR:masterfrom
addder:fix/ncs-engine-call-argument-order
Closed

addder wants to merge 1 commit into
OpenKotOR:masterfrom
addder:fix/ncs-engine-call-argument-order

Conversation

@addder

@addder addder commented Oct 22, 2026

Copy link
Copy Markdown

What was wrong

Since 7c73bbb (merged to master in #49), EngineCallExpression.compile pushes the arguments of engine routine calls first-argument-first. The NWScript VM, the original compiler and compiled game scripts push the last argument first, so the first argument ends up on top of the stack. Scripts from the built-in compiler pass swapped arguments or abort the VM with a type error, and omitted default arguments are pushed after the supplied ones. For example, SetGlobalNumber("A", 7) compiled to CONSTS "A"; CONSTI 7; ACTION 581, 2; it should be CONSTI 7; CONSTS "A"; ACTION 581, 2.

TestNCSCompiler did not notice, because it runs the output through PyKotor's own Interpreter, which got a compensating args_snap.reverse() in the same commit; the two errors cancelled.

The fix

  • EngineCallExpression.compile: iterate the arguments from last to first (param_index still indexes function.params) and replace the comment that described the wrong order.
  • Interpreter.do_action: the first parameter is on top of the stack, so pop in parameter order and drop args_snap.reverse().

User-defined function calls (compile_jsr) are not changed.

Tests

Four byte-level regression tests in TestNCSCompiler, next to the other engine call tests. They compare compile_nss output with hand-assembled NCS bytes and do not use the Interpreter:

  • test_enginecall_argument_order_bytes_set_global_number: SetGlobalNumber("A", 7)
  • test_enginecall_argument_order_bytes_three_mixed_types: a 3-argument call (object, int, float)
  • test_enginecall_argument_order_bytes_omitted_defaults: omitted default arguments
  • test_enginecall_argument_order_bytes_action_argument: an action argument (DelayCommand)

All four fail on the unpatched source and pass with this change.

I also compiled one call to every K1 and TSL routine whose parameters are int, float, string or object: all 185 (K1) and 221 (TSL) now push last-first.

Test results

Scoped command from CONTRIBUTING.md (Windows, Python 3.12, Libraries/PyKotor/tests):

passed failed skipped
master (ca61ce8) 1604 5 114
this branch 1608 5 114

The only difference is the four new tests. The same five tests in tests/common/test_decode_fallbacks.py fail on both (test_8bit_encoding_only, test_bom, test_no_valid_encoding_found_strict_errors, test_strict_error_handling_decoding_failure, test_with_BOM_included); they are not related to this change. ruff check reports the same 7 findings on the three touched files before and after, and ruff format --check is clean for them.

bleeding-edge has the same two code sites (its classes.py and interpreter.py differ from master, so this commit does not merge cleanly there).

Fixes #351

Since 7c73bbb (merged to master via OpenKotOR#49) the built-in NSS compiler pushed
the arguments of engine routine calls first-argument-first. The NWScript VM,
the original compiler and every compiled game script expect the last argument
to be pushed first, so that the first argument ends up on top of the stack.
Compiled scripts therefore passed swapped arguments or aborted the VM with a
type error, and omitted default arguments were pushed after the supplied ones.

TestNCSCompiler did not notice because it runs the compiled output through
PyKotor's own Interpreter, which got a compensating change in the same commit.

- EngineCallExpression.compile: iterate the arguments from last to first
  (param_index still indexes function.params); replace the comment that
  described the wrong order.
- Interpreter.do_action: the first parameter is on top of the stack, so pop in
  parameter order and drop the args_snap.reverse() workaround.
- Add byte-level regression tests next to TestNCSCompiler that compare
  compile_nss output with hand-assembled NCS bytes (no Interpreter involved):
  SetGlobalNumber("A", 7), a 3-argument call with mixed types, a call with
  omitted default arguments, and a call with an action argument.

User-defined function calls (compile_jsr) are not changed.
@ProjectSynchro

Copy link
Copy Markdown
Contributor

Closing this for now, Please see: #354

@github-actions github-actions Bot added the package: pykotor Related to PyKotor core label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: pykotor Related to PyKotor core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NSS compiler (since 2.0.0) pushes engine-function arguments in source order; NCS expects last argument first

2 participants