Skip to content

Commit

Permalink
LibJS/Bytecode: Ensure iterator is closed after array destructuring
Browse files Browse the repository at this point in the history
If destructuring didn't exhaust the iterator, we have to manually close
the iterator.

92 new passes on test262. :^)
  • Loading branch information
awesomekling committed Jun 26, 2023
1 parent e594c01 commit 963c510
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,8 @@ static Bytecode::CodeGenerationErrorOr<void> generate_array_binding_pattern_byte
*/

auto is_iterator_exhausted_register = generator.allocate_register();
generator.emit<Bytecode::Op::LoadImmediate>(Value(false));
generator.emit<Bytecode::Op::Store>(is_iterator_exhausted_register);

auto iterator_reg = generator.allocate_register();
generator.emit<Bytecode::Op::Load>(value_reg);
Expand Down Expand Up @@ -1306,6 +1308,18 @@ static Bytecode::CodeGenerationErrorOr<void> generate_array_binding_pattern_byte
first = false;
}

auto& done_block = generator.make_block();
auto& not_done_block = generator.make_block();

generator.emit<Bytecode::Op::Load>(is_iterator_exhausted_register);
generator.emit<Bytecode::Op::JumpConditional>().set_targets(
Bytecode::Label { done_block },
Bytecode::Label { not_done_block });

generator.switch_to_basic_block(not_done_block);
generator.emit<Bytecode::Op::Load>(iterator_reg);
generator.emit<Bytecode::Op::IteratorClose>(Completion::Type::Normal, Optional<Value> {});

return {};
}

Expand Down

0 comments on commit 963c510

Please sign in to comment.