diff options
author | Linus Groh <mail@linusgroh.de> | 2021-12-28 23:50:23 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-29 00:16:51 +0100 |
commit | 451149df0b6ea94ea3aa0c1d630334c998ebc9d0 (patch) | |
tree | dd6a9099ae8b229c55ff96c2995c0d7c023b5d06 /Userland/Libraries/LibJS/Runtime | |
parent | 8d70a50aed89d9931ce7864b26734a01a9423f1c (diff) | |
download | serenity-451149df0b6ea94ea3aa0c1d630334c998ebc9d0.zip |
LibJS: Ensure get_new_target() never returns an empty value
Also add spec comments and remove a redundant exception check while
we're here :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/FunctionEnvironment.h | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/VM.cpp | 5 |
3 files changed, 16 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp index 198189515c..072e1b4681 100644 --- a/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp @@ -81,6 +81,8 @@ ThrowCompletionOr<Value> FunctionEnvironment::get_this_binding(GlobalObject& glo // 9.1.1.3.1 BindThisValue ( V ), https://tc39.es/ecma262/#sec-bindthisvalue ThrowCompletionOr<Value> FunctionEnvironment::bind_this_value(GlobalObject& global_object, Value this_value) { + VERIFY(!this_value.is_empty()); + // 1. Assert: envRec.[[ThisBindingStatus]] is not lexical. VERIFY(m_this_binding_status != ThisBindingStatus::Lexical); diff --git a/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.h b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.h index f1cabbc3e7..b2574e324d 100644 --- a/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.h +++ b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.h @@ -25,22 +25,22 @@ public: explicit FunctionEnvironment(Environment* parent_scope); virtual ~FunctionEnvironment() override; - // [[ThisValue]] Value this_value() const { return m_this_value; } void set_this_value(Value value) { m_this_value = value; } - // [[ThisBindingStatus]] ThisBindingStatus this_binding_status() const { return m_this_binding_status; } void set_this_binding_status(ThisBindingStatus status) { m_this_binding_status = status; } - // [[FunctionObject]] ECMAScriptFunctionObject& function_object() { return *m_function_object; } ECMAScriptFunctionObject const& function_object() const { return *m_function_object; } void set_function_object(ECMAScriptFunctionObject& function) { m_function_object = &function; } - // [[NewTarget]] Value new_target() const { return m_new_target; } - void set_new_target(Value new_target) { m_new_target = new_target; } + void set_new_target(Value new_target) + { + VERIFY(!new_target.is_empty()); + m_new_target = new_target; + } // Abstract operations ThrowCompletionOr<Value> get_super_base() const; @@ -53,10 +53,10 @@ private: virtual bool is_function_environment() const override { return true; } virtual void visit_edges(Visitor&) override; - Value m_this_value; - ThisBindingStatus m_this_binding_status { ThisBindingStatus::Uninitialized }; - ECMAScriptFunctionObject* m_function_object { nullptr }; - Value m_new_target; + Value m_this_value; // [[ThisValue]] + ThisBindingStatus m_this_binding_status { ThisBindingStatus::Uninitialized }; // [[ThisBindingStatus]] + ECMAScriptFunctionObject* m_function_object { nullptr }; // [[FunctionObject]] + Value m_new_target { js_undefined() }; // [[NewTarget]] }; template<> diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 0e949e9e65..a7ee1561af 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -497,9 +497,14 @@ String VM::join_arguments(size_t start_index) const return joined_arguments.build(); } +// 9.4.5 GetNewTarget ( ), https://tc39.es/ecma262/#sec-getnewtarget Value VM::get_new_target() { + // 1. Let envRec be GetThisEnvironment(). auto& env = get_this_environment(*this); + + // 2. Assert: envRec has a [[NewTarget]] field. + // 3. Return envRec.[[NewTarget]]. return verify_cast<FunctionEnvironment>(env).new_target(); } |