mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:28:12 +00:00
LibJS: Convert the PutValue AO to ThrowCompletionOr
This commit is contained in:
parent
390a04a985
commit
1aaaf521b8
6 changed files with 58 additions and 101 deletions
|
@ -695,7 +695,7 @@ struct ForInOfHeadState {
|
|||
if (lhs_kind == LexicalBinding)
|
||||
lhs_reference->initialize_referenced_binding(global_object, next_value);
|
||||
else
|
||||
lhs_reference->put_value(global_object, next_value);
|
||||
TRY(lhs_reference->put_value(global_object, next_value));
|
||||
if (auto* exception = interpreter.exception())
|
||||
return throw_completion(exception->value());
|
||||
return {};
|
||||
|
@ -749,9 +749,7 @@ static ThrowCompletionOr<ForInOfHeadState> for_in_of_head_execute(Interpreter& i
|
|||
return throw_completion(exception->value());
|
||||
|
||||
auto result = TRY(interpreter.vm().named_evaluation_if_anonymous_function(global_object, *variable.init(), binding_id));
|
||||
reference.put_value(global_object, result);
|
||||
if (auto* exception = interpreter.exception())
|
||||
return throw_completion(exception->value());
|
||||
TRY(reference.put_value(global_object, result));
|
||||
}
|
||||
} else {
|
||||
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));
|
||||
} else {
|
||||
auto reference = interpreter.vm().resolve_binding(name);
|
||||
|
||||
reference.put_value(global_object, class_constructor);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(reference.put_value(global_object, class_constructor));
|
||||
}
|
||||
|
||||
return {};
|
||||
|
@ -2148,9 +2143,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
|
|||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
reference.put_value(global_object, rhs_result);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
|
||||
|
||||
return rhs_result;
|
||||
},
|
||||
|
@ -2203,9 +2196,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
|
|||
return {};
|
||||
}
|
||||
|
||||
reference.put_value(global_object, rhs_result);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
|
||||
|
||||
return rhs_result;
|
||||
}
|
||||
|
@ -2259,13 +2250,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
reference.put_value(global_object, rhs_result);
|
||||
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(reference.put_value(global_object, rhs_result));
|
||||
|
||||
return rhs_result;
|
||||
}
|
||||
|
@ -2299,9 +2284,7 @@ Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_o
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
reference.put_value(global_object, new_value);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(reference.put_value(global_object, new_value));
|
||||
return m_prefixed ? new_value : old_value;
|
||||
}
|
||||
|
||||
|
@ -2396,35 +2379,31 @@ Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& globa
|
|||
|
||||
for (auto& declarator : m_declarations) {
|
||||
if (auto* init = declarator.init()) {
|
||||
declarator.target().visit(
|
||||
[&](NonnullRefPtr<Identifier> const& id) {
|
||||
TRY_OR_DISCARD(declarator.target().visit(
|
||||
[&](NonnullRefPtr<Identifier> const& id) -> ThrowCompletionOr<void> {
|
||||
auto reference = id->to_reference(interpreter, global_object);
|
||||
if (interpreter.exception())
|
||||
return;
|
||||
auto initializer_result_or_error = 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();
|
||||
if (auto* exception = interpreter.exception())
|
||||
return throw_completion(exception->value());
|
||||
auto initializer_result = TRY_OR_DISCARD(interpreter.vm().named_evaluation_if_anonymous_function(global_object, *init, id->string()));
|
||||
VERIFY(!initializer_result.is_empty());
|
||||
|
||||
if (m_declaration_kind == DeclarationKind::Var)
|
||||
reference.put_value(global_object, initializer_result);
|
||||
TRY(reference.put_value(global_object, initializer_result));
|
||||
else
|
||||
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);
|
||||
if (interpreter.exception())
|
||||
return;
|
||||
if (auto* exception = interpreter.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
Environment* environment = m_declaration_kind == DeclarationKind::Var ? nullptr : interpreter.lexical_environment();
|
||||
|
||||
// FIXME: I want to use TRY_OR_DISCARD here but can't return...
|
||||
auto result = interpreter.vm().binding_initialization(pattern, initializer_result, environment, global_object);
|
||||
(void)result;
|
||||
});
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
return interpreter.vm().binding_initialization(pattern, initializer_result, environment, global_object);
|
||||
}));
|
||||
} else if (m_declaration_kind != DeclarationKind::Var) {
|
||||
VERIFY(declarator.target().has<NonnullRefPtr<Identifier>>());
|
||||
auto& identifier = declarator.target().get<NonnullRefPtr<Identifier>>();
|
||||
|
|
|
@ -274,7 +274,8 @@ void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
|
|||
if (vm.exception())
|
||||
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
|
||||
|
|
|
@ -398,22 +398,22 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
|
|||
|
||||
for (size_t i = 0; i < m_formal_parameters.size(); ++i) {
|
||||
auto& parameter = m_formal_parameters[i];
|
||||
parameter.binding.visit(
|
||||
[&](auto const& param) {
|
||||
TRY(parameter.binding.visit(
|
||||
[&](auto const& param) -> ThrowCompletionOr<void> {
|
||||
Value argument_value;
|
||||
if (parameter.is_rest) {
|
||||
auto* array = MUST(Array::create(global_object(), 0));
|
||||
for (size_t rest_index = i; rest_index < execution_context_arguments.size(); ++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()) {
|
||||
argument_value = execution_context_arguments[i];
|
||||
} else if (parameter.default_value) {
|
||||
// FIXME: Support default arguments in the bytecode world!
|
||||
if (interpreter)
|
||||
argument_value = parameter.default_value->execute(*interpreter, global_object());
|
||||
if (vm.exception())
|
||||
return;
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
} else {
|
||||
argument_value = js_undefined();
|
||||
}
|
||||
|
@ -422,26 +422,21 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
|
|||
|
||||
if constexpr (IsSame<FlyString const&, decltype(param)>) {
|
||||
Reference reference = vm.resolve_binding(param, used_environment);
|
||||
if (vm.exception())
|
||||
return;
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
// Here the difference from hasDuplicates is important
|
||||
if (has_duplicates)
|
||||
reference.put_value(global_object(), argument_value);
|
||||
TRY(reference.put_value(global_object(), argument_value));
|
||||
else
|
||||
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)>) {
|
||||
// Here the difference from hasDuplicates is important
|
||||
auto result = vm.binding_initialization(param, argument_value, used_environment, global_object());
|
||||
if (result.is_error())
|
||||
return;
|
||||
return vm.binding_initialization(param, argument_value, used_environment, global_object());
|
||||
}
|
||||
|
||||
if (vm.exception())
|
||||
return;
|
||||
});
|
||||
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
}));
|
||||
}
|
||||
|
||||
Environment* var_environment;
|
||||
|
|
|
@ -13,53 +13,38 @@
|
|||
namespace JS {
|
||||
|
||||
// 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();
|
||||
|
||||
if (!is_valid_reference()) {
|
||||
vm.throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
|
||||
return;
|
||||
}
|
||||
if (!is_valid_reference())
|
||||
return vm.throw_completion<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
|
||||
|
||||
if (is_unresolvable()) {
|
||||
if (m_strict) {
|
||||
(void)throw_reference_error(global_object);
|
||||
return;
|
||||
}
|
||||
MUST(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
|
||||
return;
|
||||
if (m_strict)
|
||||
return throw_reference_error(global_object);
|
||||
TRY(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (is_property_reference()) {
|
||||
auto base_obj_or_error = m_base_value.to_object(global_object);
|
||||
if (base_obj_or_error.is_error())
|
||||
return;
|
||||
auto* base_obj = base_obj_or_error.release_value();
|
||||
auto* base_obj = TRY(m_base_value.to_object(global_object));
|
||||
|
||||
if (is_private_reference()) {
|
||||
base_obj->private_set(m_private_name, value);
|
||||
return;
|
||||
}
|
||||
if (is_private_reference())
|
||||
return base_obj->private_set(m_private_name, value);
|
||||
|
||||
auto succeeded_or_error = base_obj->internal_set(m_name, value, get_this_value());
|
||||
if (succeeded_or_error.is_error())
|
||||
return;
|
||||
auto succeeded = succeeded_or_error.release_value();
|
||||
if (!succeeded && m_strict) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
|
||||
return;
|
||||
}
|
||||
return;
|
||||
auto succeeded = TRY(base_obj->internal_set(m_name, value, get_this_value()));
|
||||
if (!succeeded && m_strict)
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
|
||||
return {};
|
||||
}
|
||||
|
||||
VERIFY(m_base_type == BaseType::Environment);
|
||||
VERIFY(m_base_environment);
|
||||
// These can throw, TRY() when converting put_value() to ThrowCompletionOr
|
||||
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
|
||||
(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
|
||||
|
|
|
@ -129,7 +129,7 @@ public:
|
|||
(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<bool> delete_(GlobalObject&);
|
||||
|
||||
|
|
|
@ -182,10 +182,7 @@ ThrowCompletionOr<void> VM::binding_initialization(FlyString const& target, Valu
|
|||
return {};
|
||||
}
|
||||
auto reference = resolve_binding(target);
|
||||
reference.put_value(global_object, value);
|
||||
if (auto* thrown_exception = exception())
|
||||
return JS::throw_completion(thrown_exception->value());
|
||||
return {};
|
||||
return reference.put_value(global_object, value);
|
||||
}
|
||||
|
||||
// 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));
|
||||
if (!environment)
|
||||
assignment_target.put_value(global_object, rest_object);
|
||||
TRY(assignment_target.put_value(global_object, rest_object));
|
||||
else
|
||||
assignment_target.initialize_referenced_binding(global_object, rest_object);
|
||||
|
||||
|
@ -284,7 +281,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
|
|||
}
|
||||
|
||||
if (!environment)
|
||||
reference.put_value(global_object, value_to_assign);
|
||||
TRY(reference.put_value(global_object, value_to_assign));
|
||||
else
|
||||
reference.initialize_referenced_binding(global_object, value_to_assign);
|
||||
continue;
|
||||
|
@ -321,7 +318,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
|
|||
} else {
|
||||
VERIFY(reference_to_assign_to.has_value());
|
||||
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
|
||||
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>()) {
|
||||
VERIFY(assignment_target.has_value());
|
||||
if (!environment)
|
||||
assignment_target->put_value(global_object, value);
|
||||
TRY(assignment_target->put_value(global_object, value));
|
||||
else
|
||||
assignment_target->initialize_referenced_binding(global_object, value);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue