diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index 54529bb984..735cd32d8a 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -688,7 +688,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body() if (m_kind != FunctionKind::Generator) return { Completion::Type::Return, result.value_or(js_undefined()), {} }; - return normal_completion(GeneratorObject::create(global_object(), result, this, vm.running_execution_context().lexical_environment, bytecode_interpreter->snapshot_frame())); + return normal_completion(TRY(GeneratorObject::create(global_object(), result, this, vm.running_execution_context().lexical_environment, bytecode_interpreter->snapshot_frame()))); } else { if (m_kind != FunctionKind::Regular) return vm.throw_completion(global_object(), ErrorType::NotImplemented, "Non regular function execution in AST interpreter"); diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp index 59308d9323..165c9e2653 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp @@ -13,11 +13,11 @@ namespace JS { -GeneratorObject* GeneratorObject::create(GlobalObject& global_object, Value initial_value, ECMAScriptFunctionObject* generating_function, Environment* generating_scope, Bytecode::RegisterWindow frame) +ThrowCompletionOr GeneratorObject::create(GlobalObject& global_object, Value initial_value, ECMAScriptFunctionObject* generating_function, Environment* generating_scope, Bytecode::RegisterWindow frame) { // This is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png) - auto generating_function_prototype = TRY_OR_DISCARD(generating_function->get(global_object.vm().names.prototype)); - auto* generating_function_prototype_object = TRY_OR_DISCARD(generating_function_prototype.to_object(global_object)); + auto generating_function_prototype = TRY(generating_function->get(global_object.vm().names.prototype)); + auto* generating_function_prototype_object = TRY(generating_function_prototype.to_object(global_object)); auto object = global_object.heap().allocate(global_object, global_object, *generating_function_prototype_object); object->m_generating_function = generating_function; object->m_environment = generating_scope; @@ -47,29 +47,26 @@ void GeneratorObject::visit_edges(Cell::Visitor& visitor) visitor.visit(m_previous_value); } -Value GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional value_to_throw) +ThrowCompletionOr GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional value_to_throw) { auto bytecode_interpreter = Bytecode::Interpreter::current(); VERIFY(bytecode_interpreter); - auto generated_value = [](Value value) -> Value { + auto generated_value = [](Value value) -> ThrowCompletionOr { if (value.is_object()) - return TRY_OR_DISCARD(value.as_object().get("result")); + return TRY(value.as_object().get("result")); return value.is_empty() ? js_undefined() : value; }; - auto generated_continuation = [&](Value value) -> Bytecode::BasicBlock const* { + auto generated_continuation = [&](Value value) -> ThrowCompletionOr { if (value.is_object()) { - auto number_value = TRY_OR_DISCARD(value.as_object().get("continuation")); - return reinterpret_cast(static_cast(TRY_OR_DISCARD(number_value.to_double(global_object)))); + auto number_value = TRY(value.as_object().get("continuation")); + return reinterpret_cast(static_cast(TRY(number_value.to_double(global_object)))); } return nullptr; }; - Value previous_generated_value { generated_value(m_previous_value) }; - - if (vm.exception()) - return {}; + auto previous_generated_value = TRY(generated_value(m_previous_value)); auto result = Object::create(global_object, global_object.object_prototype()); result->define_direct_property("value", previous_generated_value, JS::default_attributes); @@ -80,9 +77,7 @@ Value GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optionalleave_frame(); - m_done = generated_continuation(m_previous_value) == nullptr; + m_done = TRY(generated_continuation(m_previous_value)) == nullptr; - result->define_direct_property("value", generated_value(m_previous_value), JS::default_attributes); + result->define_direct_property("value", TRY(generated_value(m_previous_value)), JS::default_attributes); result->define_direct_property("done", Value(m_done), JS::default_attributes); - if (vm.exception()) - return {}; - return result; } diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorObject.h b/Userland/Libraries/LibJS/Runtime/GeneratorObject.h index 341f4e9782..beccc6c89b 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorObject.h +++ b/Userland/Libraries/LibJS/Runtime/GeneratorObject.h @@ -16,13 +16,13 @@ class GeneratorObject final : public Object { JS_OBJECT(GeneratorObject, Object); public: - static GeneratorObject* create(GlobalObject&, Value, ECMAScriptFunctionObject*, Environment*, Bytecode::RegisterWindow); + static ThrowCompletionOr create(GlobalObject&, Value, ECMAScriptFunctionObject*, Environment*, Bytecode::RegisterWindow); GeneratorObject(GlobalObject&, Object& prototype); virtual void initialize(GlobalObject&) override; virtual ~GeneratorObject() override; void visit_edges(Cell::Visitor&) override; - Value next_impl(VM&, GlobalObject&, Optional value_to_throw); + ThrowCompletionOr next_impl(VM&, GlobalObject&, Optional value_to_throw); void set_done() { m_done = true; } private: diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorObjectPrototype.cpp b/Userland/Libraries/LibJS/Runtime/GeneratorObjectPrototype.cpp index bbc0a922a4..339bcce4a5 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorObjectPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/GeneratorObjectPrototype.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include