Skip to content

VM: Fix chain assignment evaluation order - #1128

Open
schungx wants to merge 2 commits into
rhaiscript:mainfrom
schungx:vm-fix-chain-assignment-eval-order
Open

VM: Fix chain assignment evaluation order#1128
schungx wants to merge 2 commits into
rhaiscript:mainfrom
schungx:vm-fix-chain-assignment-eval-order

Conversation

@schungx

@schungx schungx commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

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:

S[K]=b;

The AST interpreter will come back with variable 'b' not found because it evaluates the value expression first.

Rhai Grain will come back with variable 'S' not found because 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.

@schungx
schungx requested a review from ImTheSquid August 13, 2026 08:46
@schungx schungx self-assigned this Aug 13, 2026
@schungx schungx added bug vm Issues related to the Rhai Grain bytecodes compiler and VM. labels Aug 13, 2026
… for proper number of arguments. Return an error instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug vm Issues related to the Rhai Grain bytecodes compiler and VM.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VM: Fuzz errors detected

1 participant