Fixed a bug in for-loops when compound is modified

It's a simple fix, but it's basically just shadowing the compound's
name:

  var array = [1];
  for (var i in array) array.pushBack(i); //this will fail
This commit is contained in:
2026-05-24 19:45:28 +10:00
parent 254aceadfc
commit e59bcd0572
2 changed files with 14 additions and 0 deletions
+13
View File
@@ -763,6 +763,19 @@ static unsigned int writeInstructionForThen(Toy_Bytecode** mb, Toy_AstForThen as
emitString(mb, ast.condBranch->iterable.left->varDeclare.name); emitString(mb, ast.condBranch->iterable.left->varDeclare.name);
//BUGFIX: shadow the iterable's name
EMIT_BYTE(mb, code, TOY_OPCODE_READ);
EMIT_BYTE(mb, code, TOY_VALUE_NULL);
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, TOY_OPCODE_DECLARE);
EMIT_BYTE(mb, code, TOY_VALUE_NULL);
EMIT_BYTE(mb, code, TOY_VALUE_AS_STRING(ast.condBranch->iterable.right->varAccess.child->value.value)->info.length);
EMIT_BYTE(mb, code, true); //check for constness
emitString(mb, TOY_VALUE_AS_STRING(ast.condBranch->iterable.right->varAccess.child->value.value));
//write the body //write the body
writeBytecodeFromAst(mb, ast.thenBranch); writeBytecodeFromAst(mb, ast.thenBranch);
+1
View File
@@ -482,6 +482,7 @@ static void processEliminate(Toy_VM* vm) {
} }
static void processIterate(Toy_VM* vm) { static void processIterate(Toy_VM* vm) {
//URGENT: finish this
//ITERATE on [-2] based on type, with [-1] as counter //ITERATE on [-2] based on type, with [-1] as counter
//then delegate to processJump //then delegate to processJump