diff options
author | Andreas Kling <kling@serenityos.org> | 2021-07-01 12:24:46 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-01 12:28:57 +0200 |
commit | 44221756ab7515617450400bf23d0e8df486c2b7 (patch) | |
tree | 0cca2e683a1d1efd822bdfa7e90995e2aa81d8d9 /Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp | |
parent | 56d25d721001f72bced2aa723d08a19eac9431fb (diff) | |
download | serenity-44221756ab7515617450400bf23d0e8df486c2b7.zip |
LibJS: Drop "Record" suffix from all the *Environment record classes
"Records" in the spec are basically C++ classes, so let's drop this
mouthful of a suffix.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp new file mode 100644 index 0000000000..722a5e55c7 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Interpreter.h> +#include <LibJS/Runtime/FunctionEnvironment.h> +#include <LibJS/Runtime/FunctionObject.h> +#include <LibJS/Runtime/GlobalObject.h> + +namespace JS { + +FunctionEnvironment::FunctionEnvironment(Environment* parent_scope, HashMap<FlyString, Variable> variables) + : DeclarativeEnvironment(variables, parent_scope) +{ +} + +FunctionEnvironment::~FunctionEnvironment() +{ +} + +void FunctionEnvironment::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_this_value); + visitor.visit(m_new_target); + visitor.visit(m_function_object); +} + +// 9.1.1.3.5 GetSuperBase ( ), https://tc39.es/ecma262/#sec-getsuperbase +Value FunctionEnvironment::get_super_base() const +{ + VERIFY(m_function_object); + auto home_object = m_function_object->home_object(); + if (home_object.is_undefined()) + return js_undefined(); + return home_object.as_object().prototype(); +} + +// 9.1.1.3.2 HasThisBinding ( ), https://tc39.es/ecma262/#sec-function-environment-records-hasthisbinding +bool FunctionEnvironment::has_this_binding() const +{ + if (this_binding_status() == ThisBindingStatus::Lexical) + return false; + return true; +} + +// 9.1.1.3.3 HasSuperBinding ( ), https://tc39.es/ecma262/#sec-function-environment-records-hassuperbinding +bool FunctionEnvironment::has_super_binding() const +{ + if (this_binding_status() == ThisBindingStatus::Lexical) + return false; + if (function_object().home_object().is_undefined()) + return false; + return true; +} + +// 9.1.1.3.4 GetThisBinding ( ), https://tc39.es/ecma262/#sec-function-environment-records-getthisbinding +Value FunctionEnvironment::get_this_binding(GlobalObject& global_object) const +{ + VERIFY(has_this_binding()); + if (this_binding_status() == ThisBindingStatus::Uninitialized) { + vm().throw_exception<ReferenceError>(global_object, ErrorType::ThisHasNotBeenInitialized); + return {}; + } + return m_this_value; +} + +// 9.1.1.3.1 BindThisValue ( V ), https://tc39.es/ecma262/#sec-bindthisvalue +Value FunctionEnvironment::bind_this_value(GlobalObject& global_object, Value this_value) +{ + VERIFY(this_binding_status() != ThisBindingStatus::Lexical); + if (this_binding_status() == ThisBindingStatus::Initialized) { + vm().throw_exception<ReferenceError>(global_object, ErrorType::ThisIsAlreadyInitialized); + return {}; + } + m_this_value = this_value; + m_this_binding_status = ThisBindingStatus::Initialized; + return this_value; +} + +} |