1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 20:17:41 +00:00

LibJS/Bytecode: Implement var/lexical binding destructuring in for/of

This commit is contained in:
Luke Wilde 2022-12-09 18:47:51 +00:00 committed by Linus Groh
parent 64cfe2b163
commit 758a4cb1a6

View file

@ -2273,13 +2273,13 @@ static Bytecode::CodeGenerationErrorOr<void> for_in_of_body_evaluation(Bytecode:
auto destructuring = head_result.is_destructuring; auto destructuring = head_result.is_destructuring;
// 5. If destructuring is true and if lhsKind is assignment, then // 5. If destructuring is true and if lhsKind is assignment, then
if (destructuring) { if (destructuring && head_result.lhs_kind == LHSKind::Assignment) {
// a. Assert: lhs is a LeftHandSideExpression. // a. Assert: lhs is a LeftHandSideExpression.
// b. Let assignmentPattern be the AssignmentPattern that is covered by lhs. // b. Let assignmentPattern be the AssignmentPattern that is covered by lhs.
// FIXME: Implement this. // FIXME: Implement this.
return Bytecode::CodeGenerationError { return Bytecode::CodeGenerationError {
&node, &node,
"Unimplemented: destructuring in for-in/of"sv, "Unimplemented: assignment destructuring in for/of"sv,
}; };
} }
// 6. Repeat, // 6. Repeat,
@ -2326,7 +2326,9 @@ static Bytecode::CodeGenerationErrorOr<void> for_in_of_body_evaluation(Bytecode:
TRY(generator.emit_store_to_reference(**ptr)); TRY(generator.emit_store_to_reference(**ptr));
} else { } else {
auto& binding_pattern = lhs.get<NonnullRefPtr<BindingPattern>>(); auto& binding_pattern = lhs.get<NonnullRefPtr<BindingPattern>>();
TRY(generate_binding_pattern_bytecode(generator, *binding_pattern, Bytecode::Op::SetVariable::InitializationMode::Set, Bytecode::Register::accumulator())); auto value_register = generator.allocate_register();
generator.emit<Bytecode::Op::Store>(value_register);
TRY(generate_binding_pattern_bytecode(generator, *binding_pattern, Bytecode::Op::SetVariable::InitializationMode::Set, value_register));
} }
} }
} }
@ -2386,9 +2388,9 @@ static Bytecode::CodeGenerationErrorOr<void> for_in_of_body_evaluation(Bytecode:
} }
// j. Else, // j. Else,
else { else {
// FIXME: Implement destructuring // FIXME: i. If lhsKind is assignment, then
// i. If lhsKind is assignment, then // 1. Let status be Completion(DestructuringAssignmentEvaluation of assignmentPattern with argument nextValue).
// 1. Let status be Completion(DestructuringAssignmentEvaluation of assignmentPattern with argument nextValue).
// ii. Else if lhsKind is varBinding, then // ii. Else if lhsKind is varBinding, then
// 1. Assert: lhs is a ForBinding. // 1. Assert: lhs is a ForBinding.
// 2. Let status be Completion(BindingInitialization of lhs with arguments nextValue and undefined). // 2. Let status be Completion(BindingInitialization of lhs with arguments nextValue and undefined).
@ -2396,10 +2398,20 @@ static Bytecode::CodeGenerationErrorOr<void> for_in_of_body_evaluation(Bytecode:
// 1. Assert: lhsKind is lexicalBinding. // 1. Assert: lhsKind is lexicalBinding.
// 2. Assert: lhs is a ForDeclaration. // 2. Assert: lhs is a ForDeclaration.
// 3. Let status be Completion(ForDeclarationBindingInitialization of lhs with arguments nextValue and iterationEnv). // 3. Let status be Completion(ForDeclarationBindingInitialization of lhs with arguments nextValue and iterationEnv).
return Bytecode::CodeGenerationError { if (head_result.lhs_kind == LHSKind::VarBinding || head_result.lhs_kind == LHSKind::LexicalBinding) {
&node, auto& declaration = static_cast<VariableDeclaration const&>(*lhs.get<NonnullRefPtr<ASTNode>>());
"Unimplemented: destructuring in for-in/of"sv, VERIFY(declaration.declarations().size() == 1);
}; auto& binding_pattern = declaration.declarations().first().target().get<NonnullRefPtr<BindingPattern>>();
auto value_register = generator.allocate_register();
generator.emit<Bytecode::Op::Store>(value_register);
TRY(generate_binding_pattern_bytecode(generator, *binding_pattern, head_result.lhs_kind == LHSKind::VarBinding ? Bytecode::Op::SetVariable::InitializationMode::Set : Bytecode::Op::SetVariable::InitializationMode::Initialize, value_register));
} else {
return Bytecode::CodeGenerationError {
&node,
"Unimplemented: assignment destructuring in for/of"sv,
};
}
} }
// FIXME: Implement iteration closure. // FIXME: Implement iteration closure.