From a24df37713508614cf66d7e83e4fb4f33feb0a68 Mon Sep 17 00:00:00 2001 From: davidot Date: Thu, 30 Dec 2021 23:00:37 +0100 Subject: [PATCH] LibJS: Convert resolve_this_binding() to ThrowCompletionOr Also add spec comments. --- Userland/Libraries/LibJS/AST.cpp | 2 +- Userland/Libraries/LibJS/Bytecode/Op.cpp | 8 +++++++- Userland/Libraries/LibJS/Runtime/VM.cpp | 6 ++++-- Userland/Libraries/LibJS/Runtime/VM.h | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index e0ee8d5346..eb0f869966 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -2252,7 +2252,7 @@ Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_o Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const { InterpreterNodeScope node_scope { interpreter, *this }; - return interpreter.vm().resolve_this_binding(global_object); + return TRY_OR_DISCARD(interpreter.vm().resolve_this_binding(global_object)); } void ThisExpression::dump(int indent) const diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index d22e20e49f..dda7d778cc 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -315,7 +315,13 @@ void Jump::execute_impl(Bytecode::Interpreter& interpreter) const void ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const { - interpreter.accumulator() = interpreter.vm().resolve_this_binding(interpreter.global_object()); + auto this_binding_or_error = interpreter.vm().resolve_this_binding(interpreter.global_object()); + if (this_binding_or_error.is_throw_completion()) { + interpreter.vm().throw_exception(interpreter.global_object(), this_binding_or_error.release_error().value()); + return; + } + + interpreter.accumulator() = this_binding_or_error.release_value(); } void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to) diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 67ab562598..50472808cd 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -503,10 +503,12 @@ void VM::throw_exception(Exception& exception) } // 9.4.4 ResolveThisBinding ( ), https://tc39.es/ecma262/#sec-resolvethisbinding -Value VM::resolve_this_binding(GlobalObject& global_object) +ThrowCompletionOr VM::resolve_this_binding(GlobalObject& global_object) { + // 1. Let envRec be GetThisEnvironment(). auto& environment = get_this_environment(*this); - return TRY_OR_DISCARD(environment.get_this_binding(global_object)); + // 2. Return ? envRec.GetThisBinding(). + return TRY(environment.get_this_binding(global_object)); } String VM::join_arguments(size_t start_index) const diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index 46529ef884..1741b71c5e 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -155,7 +155,7 @@ public: return running_execution_context().this_value; } - Value resolve_this_binding(GlobalObject&); + ThrowCompletionOr resolve_this_binding(GlobalObject&); Value last_value() const { return m_last_value; } void set_last_value(Badge, Value value) { m_last_value = value; }