Repository navigation
Conversation
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.
Contributor
|
Closing this for now, Please see: #354 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Since 7c73bbb (merged to
masterin #49),EngineCallExpression.compilepushes 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 toCONSTS "A"; CONSTI 7; ACTION 581, 2; it should beCONSTI 7; CONSTS "A"; ACTION 581, 2.TestNCSCompilerdid not notice, because it runs the output through PyKotor's ownInterpreter, which got a compensatingargs_snap.reverse()in the same commit; the two errors cancelled.The fix
EngineCallExpression.compile: iterate the arguments from last to first (param_indexstill indexesfunction.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 dropargs_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 comparecompile_nssoutput with hand-assembled NCS bytes and do not use theInterpreter: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 argumentstest_enginecall_argument_order_bytes_action_argument: anactionargument (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):master(ca61ce8)The only difference is the four new tests. The same five tests in
tests/common/test_decode_fallbacks.pyfail 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 checkreports the same 7 findings on the three touched files before and after, andruff format --checkis clean for them.bleeding-edgehas the same two code sites (itsclasses.pyandinterpreter.pydiffer frommaster, so this commit does not merge cleanly there).Fixes #351