VM: Fix chain assignment evaluation order - #1128
Open
schungx wants to merge 2 commits into
Open
Conversation
… for proper number of arguments. Return an error instead.
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.
Closes #1115.
Main Issue
There is a difference between how the AST interpreter and the Grain VM handle assignment to chained expressions.
For instance, when running this script:
The AST interpreter will come back with
variable 'b' not foundbecause it evaluates the value expression first.Rhai Grain will come back with
variable 'S' not foundbecause it evaluates the lvalue expression first.This PR patches the VM such that it follows the AST interpreter's order and evaluates the value expression first, stashing the result into an temporary slot, processes the lvalue chain and then finally assigning the result into the chain target.
Bonus fix
Rhai sometimes checks for invariants using
assert!where it knows to be true. However, the VM may be running bytecodes that are corrupted, violating such invariants.A good example is indexers and property getters/setters. They are function calls compiled to AST with fixed numbers of arguments. Rhai's compiler will never emit an AST with the wrong numbers of arguments.
However, the VM may load an artifact that contains the wrong numbers of arguments for these function calls. There is no way for the VM to catch these cases.
Therefore the asserts have been replaced by proper errors which also resolves another fuzzing error.