mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 05:55:13 +00:00
This is a huge patch, I know. In hindsight this perhaps could've been done slightly more incremental, but I started and then fixed everything until it worked, and here we are. I tried splitting of some completely unrelated changes into separate commits, however. Anyway. This is a rewrite of most of Object, and by extension large parts of Array, Proxy, Reflect, String, TypedArray, and some other things. What we already had worked fine for about 90% of things, but getting the last 10% right proved to be increasingly difficult with the current code that sort of grew organically and is only very loosely based on the spec - this became especially obvious when we started fixing a large number of test262 failures. Key changes include: - 1:1 matching function names and parameters of all object-related functions, to avoid ambiguity. Previously we had things like put(), which the spec doesn't have - as a result it wasn't always clear which need to be used. - Better separation between object abstract operations and internal methods - the former are always the same, the latter can be overridden (and are therefore virtual). The internal methods (i.e. [[Foo]] in the spec) are now prefixed with 'internal_' for clarity - again, it was previously not always clear which AO a certain method represents, get() could've been both Get and [[Get]] (I don't know which one it was closer to right now). Note that some of the old names have been kept until all code relying on them is updated, but they are now simple wrappers around the closest matching standard abstract operation. - Simplifications of the storage layer: functions that write values to storage are now prefixed with 'storage_' to make their purpose clear, and as they are not part of the spec they should not contain any steps specified by it. Much functionality is now covered by the layers above it and was removed (e.g. handling of accessors, attribute checks). - PropertyAttributes has been greatly simplified, and is being replaced by PropertyDescriptor - a concept similar to the current implementation, but more aligned with the actual spec. See the commit message of the previous commit where it was introduced for details. - As a bonus, and since I had to look at the spec a whole lot anyway, I introduced more inline comments with the exact steps from the spec - this makes it super easy to verify correctness. - East-const all the things. As a result of all of this, things are much more correct but a bit slower now. Retaining speed wasn't a consideration at all, I have done no profiling of the new code - there might be low hanging fruits, which we can then harvest separately. Special thanks to Idan for helping me with this by tracking down bugs, updating everything outside of LibJS to work with these changes (LibWeb, Spreadsheet, HackStudio), as well as providing countless patches to fix regressions I introduced - there still are very few (we got it down to 5), but we also get many new passing test262 tests in return. :^) Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
235 lines
8.9 KiB
C++
235 lines
8.9 KiB
C++
/*
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibJS/AST.h>
|
|
#include <LibJS/Runtime/DeclarativeEnvironment.h>
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
#include <LibJS/Runtime/ObjectEnvironment.h>
|
|
|
|
namespace JS {
|
|
|
|
GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object)
|
|
: Environment(nullptr)
|
|
{
|
|
m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
|
|
m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>(global_object);
|
|
}
|
|
|
|
void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
|
|
{
|
|
Base::visit_edges(visitor);
|
|
visitor.visit(m_object_record);
|
|
visitor.visit(m_declarative_record);
|
|
}
|
|
|
|
Optional<Variable> GlobalEnvironment::get_from_environment(FlyString const& name) const
|
|
{
|
|
// FIXME: This should be a "composite" of the object record and the declarative record.
|
|
return m_object_record->get_from_environment(name);
|
|
}
|
|
|
|
bool GlobalEnvironment::put_into_environment(FlyString const& name, Variable variable)
|
|
{
|
|
// FIXME: This should be a "composite" of the object record and the declarative record.
|
|
return m_object_record->put_into_environment(name, variable);
|
|
}
|
|
|
|
bool GlobalEnvironment::delete_from_environment(FlyString const& name)
|
|
{
|
|
// FIXME: This should be a "composite" of the object record and the declarative record.
|
|
return object_record().delete_from_environment(name);
|
|
}
|
|
|
|
Value GlobalEnvironment::get_this_binding(GlobalObject&) const
|
|
{
|
|
return &global_object();
|
|
}
|
|
|
|
Value GlobalEnvironment::global_this_value() const
|
|
{
|
|
return &global_object();
|
|
}
|
|
|
|
// 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
|
|
bool GlobalEnvironment::has_binding(FlyString const& name) const
|
|
{
|
|
if (m_declarative_record->has_binding(name))
|
|
return true;
|
|
return m_object_record->has_binding(name);
|
|
}
|
|
|
|
// 9.1.1.4.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d
|
|
void GlobalEnvironment::create_mutable_binding(GlobalObject& global_object, FlyString const& name, bool can_be_deleted)
|
|
{
|
|
if (m_declarative_record->has_binding(name)) {
|
|
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
|
|
return;
|
|
}
|
|
m_declarative_record->create_mutable_binding(global_object, name, can_be_deleted);
|
|
}
|
|
|
|
// 9.1.1.4.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s
|
|
void GlobalEnvironment::create_immutable_binding(GlobalObject& global_object, FlyString const& name, bool strict)
|
|
{
|
|
if (m_declarative_record->has_binding(name)) {
|
|
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
|
|
return;
|
|
}
|
|
m_declarative_record->create_immutable_binding(global_object, name, strict);
|
|
}
|
|
|
|
// 9.1.1.4.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
|
|
void GlobalEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
|
|
{
|
|
if (m_declarative_record->has_binding(name)) {
|
|
m_declarative_record->initialize_binding(global_object, name, value);
|
|
return;
|
|
}
|
|
m_object_record->initialize_binding(global_object, name, value);
|
|
}
|
|
|
|
// 9.1.1.4.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s
|
|
void GlobalEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
|
|
{
|
|
if (m_declarative_record->has_binding(name)) {
|
|
m_declarative_record->set_mutable_binding(global_object, name, value, strict);
|
|
return;
|
|
}
|
|
|
|
m_object_record->set_mutable_binding(global_object, name, value, strict);
|
|
}
|
|
|
|
// 9.1.1.4.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s
|
|
Value GlobalEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
|
|
{
|
|
if (m_declarative_record->has_binding(name))
|
|
return m_declarative_record->get_binding_value(global_object, name, strict);
|
|
return m_object_record->get_binding_value(global_object, name, strict);
|
|
}
|
|
|
|
// 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
|
|
bool GlobalEnvironment::delete_binding(GlobalObject& global_object, FlyString const& name)
|
|
{
|
|
if (m_declarative_record->has_binding(name))
|
|
return m_declarative_record->delete_binding(global_object, name);
|
|
|
|
bool existing_prop = m_object_record->binding_object().has_own_property(name);
|
|
if (existing_prop) {
|
|
bool status = m_object_record->delete_binding(global_object, name);
|
|
if (status) {
|
|
m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
|
|
}
|
|
return status;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
|
|
bool GlobalEnvironment::has_var_declaration(FlyString const& name) const
|
|
{
|
|
return m_var_names.contains_slow(name);
|
|
}
|
|
|
|
// 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
|
|
bool GlobalEnvironment::has_lexical_declaration(FlyString const& name) const
|
|
{
|
|
return m_declarative_record->has_binding(name);
|
|
}
|
|
|
|
// 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
|
|
bool GlobalEnvironment::has_restricted_global_property(FlyString const& name) const
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = m_object_record->binding_object();
|
|
auto existing_prop = global_object.internal_get_own_property(name);
|
|
if (vm.exception())
|
|
return {};
|
|
if (!existing_prop.has_value())
|
|
return false;
|
|
if (*existing_prop->configurable)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
// 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
|
|
bool GlobalEnvironment::can_declare_global_var(FlyString const& name) const
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = m_object_record->binding_object();
|
|
bool has_property = global_object.has_own_property(name);
|
|
if (vm.exception())
|
|
return {};
|
|
if (has_property)
|
|
return true;
|
|
return global_object.is_extensible();
|
|
}
|
|
|
|
// 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
|
|
bool GlobalEnvironment::can_declare_global_function(FlyString const& name) const
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = m_object_record->binding_object();
|
|
auto existing_prop = global_object.internal_get_own_property(name);
|
|
if (vm.exception())
|
|
return {};
|
|
if (!existing_prop.has_value())
|
|
return global_object.is_extensible();
|
|
if (*existing_prop->configurable)
|
|
return true;
|
|
if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
// 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
|
|
void GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted)
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = m_object_record->binding_object();
|
|
bool has_property = global_object.has_own_property(name);
|
|
if (vm.exception())
|
|
return;
|
|
bool extensible = global_object.is_extensible();
|
|
if (vm.exception())
|
|
return;
|
|
if (!has_property && extensible) {
|
|
m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted);
|
|
if (vm.exception())
|
|
return;
|
|
m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined());
|
|
if (vm.exception())
|
|
return;
|
|
}
|
|
if (!m_var_names.contains_slow(name))
|
|
m_var_names.append(name);
|
|
}
|
|
|
|
// 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
|
|
void GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted)
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = m_object_record->binding_object();
|
|
auto existing_prop = global_object.internal_get_own_property(name);
|
|
if (vm.exception())
|
|
return;
|
|
PropertyDescriptor desc;
|
|
if (!existing_prop.has_value() || *existing_prop->configurable)
|
|
desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
|
|
else
|
|
desc = { .value = value };
|
|
global_object.define_property_or_throw(name, desc);
|
|
if (vm.exception())
|
|
return;
|
|
global_object.set(name, value, false);
|
|
if (vm.exception())
|
|
return;
|
|
if (!m_var_names.contains_slow(name))
|
|
m_var_names.append(name);
|
|
}
|
|
|
|
}
|