1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibJS: Convert the PutValue AO to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-11-02 19:20:21 +02:00 committed by Linus Groh
parent 390a04a985
commit 1aaaf521b8
6 changed files with 58 additions and 101 deletions

View file

@ -695,7 +695,7 @@ struct ForInOfHeadState {
if (lhs_kind == LexicalBinding) if (lhs_kind == LexicalBinding)
lhs_reference->initialize_referenced_binding(global_object, next_value); lhs_reference->initialize_referenced_binding(global_object, next_value);
else else
lhs_reference->put_value(global_object, next_value); TRY(lhs_reference->put_value(global_object, next_value));
if (auto* exception = interpreter.exception()) if (auto* exception = interpreter.exception())
return throw_completion(exception->value()); return throw_completion(exception->value());
return {}; return {};
@ -749,9 +749,7 @@ static ThrowCompletionOr<ForInOfHeadState> for_in_of_head_execute(Interpreter& i
return throw_completion(exception->value()); return throw_completion(exception->value());
auto result = TRY(interpreter.vm().named_evaluation_if_anonymous_function(global_object, *variable.init(), binding_id)); auto result = TRY(interpreter.vm().named_evaluation_if_anonymous_function(global_object, *variable.init(), binding_id));
reference.put_value(global_object, result); TRY(reference.put_value(global_object, result));
if (auto* exception = interpreter.exception())
return throw_completion(exception->value());
} }
} else { } else {
state.lhs_kind = ForInOfHeadState::LexicalBinding; state.lhs_kind = ForInOfHeadState::LexicalBinding;
@ -1367,10 +1365,7 @@ Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_o
MUST(interpreter.lexical_environment()->initialize_binding(global_object, name, class_constructor)); MUST(interpreter.lexical_environment()->initialize_binding(global_object, name, class_constructor));
} else { } else {
auto reference = interpreter.vm().resolve_binding(name); auto reference = interpreter.vm().resolve_binding(name);
TRY_OR_DISCARD(reference.put_value(global_object, class_constructor));
reference.put_value(global_object, class_constructor);
if (interpreter.exception())
return {};
} }
return {}; return {};
@ -2148,9 +2143,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
reference.put_value(global_object, rhs_result); TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
if (interpreter.exception())
return {};
return rhs_result; return rhs_result;
}, },
@ -2203,9 +2196,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
return {}; return {};
} }
reference.put_value(global_object, rhs_result); TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
if (interpreter.exception())
return {};
return rhs_result; return rhs_result;
} }
@ -2259,13 +2250,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
if (interpreter.exception()) TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
return {};
reference.put_value(global_object, rhs_result);
if (interpreter.exception())
return {};
return rhs_result; return rhs_result;
} }
@ -2299,9 +2284,7 @@ Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_o
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
reference.put_value(global_object, new_value); TRY_OR_DISCARD(reference.put_value(global_object, new_value));
if (interpreter.exception())
return {};
return m_prefixed ? new_value : old_value; return m_prefixed ? new_value : old_value;
} }
@ -2396,35 +2379,31 @@ Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& globa
for (auto& declarator : m_declarations) { for (auto& declarator : m_declarations) {
if (auto* init = declarator.init()) { if (auto* init = declarator.init()) {
declarator.target().visit( TRY_OR_DISCARD(declarator.target().visit(
[&](NonnullRefPtr<Identifier> const& id) { [&](NonnullRefPtr<Identifier> const& id) -> ThrowCompletionOr<void> {
auto reference = id->to_reference(interpreter, global_object); auto reference = id->to_reference(interpreter, global_object);
if (interpreter.exception()) if (auto* exception = interpreter.exception())
return; return throw_completion(exception->value());
auto initializer_result_or_error = interpreter.vm().named_evaluation_if_anonymous_function(global_object, *init, id->string()); auto initializer_result = TRY_OR_DISCARD(interpreter.vm().named_evaluation_if_anonymous_function(global_object, *init, id->string()));
if (initializer_result_or_error.is_error())
return;
auto initializer_result = initializer_result_or_error.release_value();
VERIFY(!initializer_result.is_empty()); VERIFY(!initializer_result.is_empty());
if (m_declaration_kind == DeclarationKind::Var) if (m_declaration_kind == DeclarationKind::Var)
reference.put_value(global_object, initializer_result); TRY(reference.put_value(global_object, initializer_result));
else else
reference.initialize_referenced_binding(global_object, initializer_result); reference.initialize_referenced_binding(global_object, initializer_result);
if (auto* exception = interpreter.exception())
return throw_completion(exception->value());
return {};
}, },
[&](NonnullRefPtr<BindingPattern> const& pattern) { [&](NonnullRefPtr<BindingPattern> const& pattern) -> ThrowCompletionOr<void> {
auto initializer_result = init->execute(interpreter, global_object); auto initializer_result = init->execute(interpreter, global_object);
if (interpreter.exception()) if (auto* exception = interpreter.exception())
return; return throw_completion(exception->value());
Environment* environment = m_declaration_kind == DeclarationKind::Var ? nullptr : interpreter.lexical_environment(); Environment* environment = m_declaration_kind == DeclarationKind::Var ? nullptr : interpreter.lexical_environment();
// FIXME: I want to use TRY_OR_DISCARD here but can't return... return interpreter.vm().binding_initialization(pattern, initializer_result, environment, global_object);
auto result = interpreter.vm().binding_initialization(pattern, initializer_result, environment, global_object); }));
(void)result;
});
if (interpreter.exception())
return {};
} else if (m_declaration_kind != DeclarationKind::Var) { } else if (m_declaration_kind != DeclarationKind::Var) {
VERIFY(declarator.target().has<NonnullRefPtr<Identifier>>()); VERIFY(declarator.target().has<NonnullRefPtr<Identifier>>());
auto& identifier = declarator.target().get<NonnullRefPtr<Identifier>>(); auto& identifier = declarator.target().get<NonnullRefPtr<Identifier>>();

View file

@ -274,7 +274,8 @@ void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
if (vm.exception()) if (vm.exception())
return; return;
reference.put_value(interpreter.global_object(), interpreter.accumulator()); // TODO: ThrowCompletionOr<void> return
(void)reference.put_value(interpreter.global_object(), interpreter.accumulator());
} }
void GetById::execute_impl(Bytecode::Interpreter& interpreter) const void GetById::execute_impl(Bytecode::Interpreter& interpreter) const

View file

@ -398,22 +398,22 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
for (size_t i = 0; i < m_formal_parameters.size(); ++i) { for (size_t i = 0; i < m_formal_parameters.size(); ++i) {
auto& parameter = m_formal_parameters[i]; auto& parameter = m_formal_parameters[i];
parameter.binding.visit( TRY(parameter.binding.visit(
[&](auto const& param) { [&](auto const& param) -> ThrowCompletionOr<void> {
Value argument_value; Value argument_value;
if (parameter.is_rest) { if (parameter.is_rest) {
auto* array = MUST(Array::create(global_object(), 0)); auto* array = MUST(Array::create(global_object(), 0));
for (size_t rest_index = i; rest_index < execution_context_arguments.size(); ++rest_index) for (size_t rest_index = i; rest_index < execution_context_arguments.size(); ++rest_index)
array->indexed_properties().append(execution_context_arguments[rest_index]); array->indexed_properties().append(execution_context_arguments[rest_index]);
argument_value = move(array); argument_value = array;
} else if (i < execution_context_arguments.size() && !execution_context_arguments[i].is_undefined()) { } else if (i < execution_context_arguments.size() && !execution_context_arguments[i].is_undefined()) {
argument_value = execution_context_arguments[i]; argument_value = execution_context_arguments[i];
} else if (parameter.default_value) { } else if (parameter.default_value) {
// FIXME: Support default arguments in the bytecode world! // FIXME: Support default arguments in the bytecode world!
if (interpreter) if (interpreter)
argument_value = parameter.default_value->execute(*interpreter, global_object()); argument_value = parameter.default_value->execute(*interpreter, global_object());
if (vm.exception()) if (auto* exception = vm.exception())
return; return throw_completion(exception->value());
} else { } else {
argument_value = js_undefined(); argument_value = js_undefined();
} }
@ -422,26 +422,21 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
if constexpr (IsSame<FlyString const&, decltype(param)>) { if constexpr (IsSame<FlyString const&, decltype(param)>) {
Reference reference = vm.resolve_binding(param, used_environment); Reference reference = vm.resolve_binding(param, used_environment);
if (vm.exception()) if (auto* exception = vm.exception())
return; return throw_completion(exception->value());
// Here the difference from hasDuplicates is important // Here the difference from hasDuplicates is important
if (has_duplicates) if (has_duplicates)
reference.put_value(global_object(), argument_value); TRY(reference.put_value(global_object(), argument_value));
else else
reference.initialize_referenced_binding(global_object(), argument_value); reference.initialize_referenced_binding(global_object(), argument_value);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return {};
} else if (IsSame<NonnullRefPtr<BindingPattern> const&, decltype(param)>) { } else if (IsSame<NonnullRefPtr<BindingPattern> const&, decltype(param)>) {
// Here the difference from hasDuplicates is important // Here the difference from hasDuplicates is important
auto result = vm.binding_initialization(param, argument_value, used_environment, global_object()); return vm.binding_initialization(param, argument_value, used_environment, global_object());
if (result.is_error())
return;
} }
}));
if (vm.exception())
return;
});
if (auto* exception = vm.exception())
return throw_completion(exception->value());
} }
Environment* var_environment; Environment* var_environment;

View file

@ -13,53 +13,38 @@
namespace JS { namespace JS {
// 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue // 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue
void Reference::put_value(GlobalObject& global_object, Value value) ThrowCompletionOr<void> Reference::put_value(GlobalObject& global_object, Value value)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
if (!is_valid_reference()) { if (!is_valid_reference())
vm.throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment); return vm.throw_completion<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
return;
}
if (is_unresolvable()) { if (is_unresolvable()) {
if (m_strict) { if (m_strict)
(void)throw_reference_error(global_object); return throw_reference_error(global_object);
return; TRY(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
} return {};
MUST(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
return;
} }
if (is_property_reference()) { if (is_property_reference()) {
auto base_obj_or_error = m_base_value.to_object(global_object); auto* base_obj = TRY(m_base_value.to_object(global_object));
if (base_obj_or_error.is_error())
return;
auto* base_obj = base_obj_or_error.release_value();
if (is_private_reference()) { if (is_private_reference())
base_obj->private_set(m_private_name, value); return base_obj->private_set(m_private_name, value);
return;
}
auto succeeded_or_error = base_obj->internal_set(m_name, value, get_this_value()); auto succeeded = TRY(base_obj->internal_set(m_name, value, get_this_value()));
if (succeeded_or_error.is_error()) if (!succeeded && m_strict)
return; return vm.throw_completion<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
auto succeeded = succeeded_or_error.release_value(); return {};
if (!succeeded && m_strict) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
return;
}
return;
} }
VERIFY(m_base_type == BaseType::Environment); VERIFY(m_base_type == BaseType::Environment);
VERIFY(m_base_environment); VERIFY(m_base_environment);
// These can throw, TRY() when converting put_value() to ThrowCompletionOr
if (m_environment_coordinate.has_value()) if (m_environment_coordinate.has_value())
(void)static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(global_object, m_environment_coordinate->index, value, m_strict); return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(global_object, m_environment_coordinate->index, value, m_strict);
else else
(void)m_base_environment->set_mutable_binding(global_object, m_name.as_string(), value, m_strict); return m_base_environment->set_mutable_binding(global_object, m_name.as_string(), value, m_strict);
} }
Completion Reference::throw_reference_error(GlobalObject& global_object) const Completion Reference::throw_reference_error(GlobalObject& global_object) const

View file

@ -129,7 +129,7 @@ public:
(void)m_base_environment->initialize_binding(global_object, m_name.as_string(), value); (void)m_base_environment->initialize_binding(global_object, m_name.as_string(), value);
} }
void put_value(GlobalObject&, Value); ThrowCompletionOr<void> put_value(GlobalObject&, Value);
ThrowCompletionOr<Value> get_value(GlobalObject&) const; ThrowCompletionOr<Value> get_value(GlobalObject&) const;
ThrowCompletionOr<bool> delete_(GlobalObject&); ThrowCompletionOr<bool> delete_(GlobalObject&);

View file

@ -182,10 +182,7 @@ ThrowCompletionOr<void> VM::binding_initialization(FlyString const& target, Valu
return {}; return {};
} }
auto reference = resolve_binding(target); auto reference = resolve_binding(target);
reference.put_value(global_object, value); return reference.put_value(global_object, value);
if (auto* thrown_exception = exception())
return JS::throw_completion(thrown_exception->value());
return {};
} }
// 8.5.2 Runtime Semantics: BindingInitialization, https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization // 8.5.2 Runtime Semantics: BindingInitialization, https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization
@ -242,7 +239,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
TRY(rest_object->copy_data_properties(object, seen_names, global_object)); TRY(rest_object->copy_data_properties(object, seen_names, global_object));
if (!environment) if (!environment)
assignment_target.put_value(global_object, rest_object); TRY(assignment_target.put_value(global_object, rest_object));
else else
assignment_target.initialize_referenced_binding(global_object, rest_object); assignment_target.initialize_referenced_binding(global_object, rest_object);
@ -284,7 +281,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
} }
if (!environment) if (!environment)
reference.put_value(global_object, value_to_assign); TRY(reference.put_value(global_object, value_to_assign));
else else
reference.initialize_referenced_binding(global_object, value_to_assign); reference.initialize_referenced_binding(global_object, value_to_assign);
continue; continue;
@ -321,7 +318,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
} else { } else {
VERIFY(reference_to_assign_to.has_value()); VERIFY(reference_to_assign_to.has_value());
if (!environment) if (!environment)
reference_to_assign_to->put_value(global_object, value_to_assign); TRY(reference_to_assign_to->put_value(global_object, value_to_assign));
else else
reference_to_assign_to->initialize_referenced_binding(global_object, value_to_assign); reference_to_assign_to->initialize_referenced_binding(global_object, value_to_assign);
@ -419,7 +416,7 @@ ThrowCompletionOr<void> VM::iterator_binding_initialization(BindingPattern const
} else if (!entry.alias.has<Empty>()) { } else if (!entry.alias.has<Empty>()) {
VERIFY(assignment_target.has_value()); VERIFY(assignment_target.has_value());
if (!environment) if (!environment)
assignment_target->put_value(global_object, value); TRY(assignment_target->put_value(global_object, value));
else else
assignment_target->initialize_referenced_binding(global_object, value); assignment_target->initialize_referenced_binding(global_object, value);