2021-06-22 17:16:08 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2022-05-02 20:54:39 +02:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2021-06-22 17:16:08 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-10-09 16:52:34 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/DeclarativeEnvironment.h>
|
2022-11-23 13:45:57 +01:00
|
|
|
#include <LibJS/Runtime/EnvironmentCoordinate.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
2021-06-22 17:16:08 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/ObjectEnvironment.h>
|
2021-06-22 17:16:08 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(GlobalEnvironment);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2021-09-11 19:58:27 +01:00
|
|
|
// 9.1.2.5 NewGlobalEnvironment ( G, thisValue ), https://tc39.es/ecma262/#sec-newglobalenvironment
|
2022-08-28 14:53:39 +01:00
|
|
|
GlobalEnvironment::GlobalEnvironment(Object& global_object, Object& this_value)
|
2021-07-01 12:24:46 +02:00
|
|
|
: Environment(nullptr)
|
2021-09-11 19:58:27 +01:00
|
|
|
, m_global_this_value(&this_value)
|
2021-06-22 17:16:08 +02:00
|
|
|
{
|
2024-11-14 06:13:46 +13:00
|
|
|
m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
|
|
|
|
m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>();
|
2021-06-22 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:24:46 +02:00
|
|
|
void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
|
2021-06-22 17:16:08 +02:00
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_object_record);
|
2021-09-11 19:58:27 +01:00
|
|
|
visitor.visit(m_global_this_value);
|
2021-06-22 17:16:08 +02:00
|
|
|
visitor.visit(m_declarative_record);
|
|
|
|
}
|
|
|
|
|
2021-10-09 16:52:34 +01:00
|
|
|
// 9.1.1.4.11 GetThisBinding ( ), https://tc39.es/ecma262/#sec-global-environment-records-getthisbinding
|
2022-08-21 15:12:43 +01:00
|
|
|
ThrowCompletionOr<Value> GlobalEnvironment::get_this_binding(VM&) const
|
2021-06-22 17:16:08 +02:00
|
|
|
{
|
2021-10-09 16:52:34 +01:00
|
|
|
// 1. Return envRec.[[GlobalThisValue]].
|
2021-10-20 19:17:45 +01:00
|
|
|
return m_global_this_value;
|
2021-06-22 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 12:26:37 +02:00
|
|
|
// 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<bool> GlobalEnvironment::has_binding(Utf16FlyString const& name, Optional<size_t>*) const
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 17:07:32 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, return true.
|
2021-10-09 17:07:32 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name)))
|
2021-06-23 12:26:37 +02:00
|
|
|
return true;
|
2021-10-09 17:07:32 +01:00
|
|
|
|
|
|
|
// 3. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 4. Return ? ObjRec.HasBinding(N).
|
2021-06-23 12:26:37 +02:00
|
|
|
return m_object_record->has_binding(name);
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:25:57 +02:00
|
|
|
// 9.1.1.4.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::create_mutable_binding(VM& vm, Utf16FlyString const& name, bool can_be_deleted)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 18:53:25 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
|
2021-10-09 18:53:25 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name)))
|
2022-08-21 15:12:43 +01:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
|
2021-10-09 18:53:25 +01:00
|
|
|
|
2022-05-24 18:19:49 +01:00
|
|
|
// 3. Return ! DclRec.CreateMutableBinding(N, D).
|
2022-08-21 15:12:43 +01:00
|
|
|
return MUST(m_declarative_record->create_mutable_binding(vm, name, can_be_deleted));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:25:57 +02:00
|
|
|
// 9.1.1.4.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::create_immutable_binding(VM& vm, Utf16FlyString const& name, bool strict)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 19:00:06 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
|
2021-10-09 19:00:06 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name)))
|
2022-08-21 15:12:43 +01:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
|
2021-10-09 19:00:06 +01:00
|
|
|
|
2022-05-24 18:19:49 +01:00
|
|
|
// 3. Return ! DclRec.CreateImmutableBinding(N, S).
|
2022-08-21 15:12:43 +01:00
|
|
|
return MUST(m_declarative_record->create_immutable_binding(vm, name, strict));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 13:26:10 +01:00
|
|
|
// 9.1.1.4.4 InitializeBinding ( N, V, hint ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::initialize_binding(VM& vm, Utf16FlyString const& name, Value value, InitializeBindingHint hint)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 19:16:24 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, then
|
2021-10-09 17:07:32 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name))) {
|
2022-12-14 13:26:10 +01:00
|
|
|
// a. Return ! DclRec.InitializeBinding(N, V, hint).
|
|
|
|
return MUST(m_declarative_record->initialize_binding(vm, name, value, hint));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
2021-10-09 19:16:24 +01:00
|
|
|
|
|
|
|
// 3. Assert: If the binding exists, it must be in the object Environment Record.
|
2022-12-14 13:26:10 +01:00
|
|
|
// 4. Assert: hint is normal.
|
|
|
|
VERIFY(hint == Environment::InitializeBindingHint::Normal);
|
|
|
|
// 5. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 6. Return ? ObjRec.InitializeBinding(N, V, normal).
|
|
|
|
return m_object_record->initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal);
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:25:57 +02:00
|
|
|
// 9.1.1.4.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::set_mutable_binding(VM& vm, Utf16FlyString const& name, Value value, bool strict)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 19:34:54 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, then
|
2021-10-09 17:07:32 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name))) {
|
2022-05-05 09:28:08 +02:00
|
|
|
// a. Return ? DclRec.SetMutableBinding(N, V, S).
|
2022-08-21 15:12:43 +01:00
|
|
|
return m_declarative_record->set_mutable_binding(vm, name, value, strict);
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 19:34:54 +01:00
|
|
|
// 3. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 4. Return ? ObjRec.SetMutableBinding(N, V, S).
|
2022-08-21 15:12:43 +01:00
|
|
|
return m_object_record->set_mutable_binding(vm, name, value, strict);
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:25:57 +02:00
|
|
|
// 9.1.1.4.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<Value> GlobalEnvironment::get_binding_value(VM& vm, Utf16FlyString const& name, bool strict)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 19:43:19 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, then
|
2023-06-11 19:49:48 +02:00
|
|
|
Optional<size_t> index;
|
|
|
|
if (MUST(m_declarative_record->has_binding(name, &index))) {
|
2022-05-24 18:19:49 +01:00
|
|
|
// a. Return ? DclRec.GetBindingValue(N, S).
|
2023-06-11 19:49:48 +02:00
|
|
|
if (index.has_value())
|
2024-05-11 17:22:59 +02:00
|
|
|
return m_declarative_record->get_binding_value_direct(vm, index.value());
|
2022-08-21 15:12:43 +01:00
|
|
|
return m_declarative_record->get_binding_value(vm, name, strict);
|
2021-10-09 19:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 4. Return ? ObjRec.GetBindingValue(N, S).
|
2022-08-21 15:12:43 +01:00
|
|
|
return m_object_record->get_binding_value(vm, name, strict);
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:25:57 +02:00
|
|
|
// 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<bool> GlobalEnvironment::delete_binding(VM& vm, Utf16FlyString const& name)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-10-09 19:49:08 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. If ! DclRec.HasBinding(N) is true, then
|
2021-10-09 19:49:08 +01:00
|
|
|
if (MUST(m_declarative_record->has_binding(name))) {
|
2022-05-02 20:54:39 +02:00
|
|
|
// a. Return ! DclRec.DeleteBinding(N).
|
2022-08-21 15:12:43 +01:00
|
|
|
return MUST(m_declarative_record->delete_binding(vm, name));
|
2021-10-09 19:49:08 +01:00
|
|
|
}
|
2021-06-23 12:26:37 +02:00
|
|
|
|
2021-10-09 19:49:08 +01:00
|
|
|
// 3. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 4. Let globalObject be ObjRec.[[BindingObject]].
|
|
|
|
|
|
|
|
// 5. Let existingProp be ? HasOwnProperty(globalObject, N).
|
|
|
|
bool existing_prop = TRY(m_object_record->binding_object().has_own_property(name));
|
|
|
|
|
|
|
|
// 6. If existingProp is true, then
|
2021-06-23 12:26:37 +02:00
|
|
|
if (existing_prop) {
|
2025-04-28 17:02:01 -04:00
|
|
|
// a. Return ? ObjRec.DeleteBinding(N).
|
|
|
|
return TRY(m_object_record->delete_binding(vm, name));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
2021-10-09 19:49:08 +01:00
|
|
|
|
|
|
|
// 7. Return true.
|
2021-06-23 12:26:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.12 HasLexicalDeclaration ( envRec, N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
|
2025-08-02 19:27:29 -04:00
|
|
|
bool GlobalEnvironment::has_lexical_declaration(Utf16FlyString const& name) const
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
|
2022-05-02 20:54:39 +02:00
|
|
|
// 2. Return ! DclRec.HasBinding(N).
|
2021-10-09 17:07:32 +01:00
|
|
|
return MUST(m_declarative_record->has_binding(name));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.13 HasRestrictedGlobalProperty ( envRec, N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<bool> GlobalEnvironment::has_restricted_global_property(Utf16FlyString const& name) const
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 2. Let globalObject be ObjRec.[[BindingObject]].
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
auto& global_object = m_object_record->binding_object();
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
|
2021-12-29 15:50:50 +01:00
|
|
|
auto existing_prop = TRY(global_object.internal_get_own_property(name));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 4. If existingProp is undefined, return false.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
if (!existing_prop.has_value())
|
2021-06-23 12:26:37 +02:00
|
|
|
return false;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 5. If existingProp.[[Configurable]] is true, return false.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
if (*existing_prop->configurable)
|
2021-06-23 12:26:37 +02:00
|
|
|
return false;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 6. Return true.
|
2021-06-23 12:26:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.14 CanDeclareGlobalVar ( envRec, N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(Utf16FlyString const& name) const
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 2. Let globalObject be ObjRec.[[BindingObject]].
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
auto& global_object = m_object_record->binding_object();
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
|
2021-12-29 15:54:44 +01:00
|
|
|
bool has_property = TRY(global_object.has_own_property(name));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 4. If hasProperty is true, return true.
|
2021-06-23 12:26:37 +02:00
|
|
|
if (has_property)
|
|
|
|
return true;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 5. Return ? IsExtensible(globalObject).
|
2021-12-29 15:54:44 +01:00
|
|
|
return global_object.is_extensible();
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.15 CanDeclareGlobalFunction ( envRec, N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_function(Utf16FlyString const& name) const
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 2. Let globalObject be ObjRec.[[BindingObject]].
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
auto& global_object = m_object_record->binding_object();
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
|
2021-12-29 15:56:53 +01:00
|
|
|
auto existing_prop = TRY(global_object.internal_get_own_property(name));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 4. If existingProp is undefined, return ? IsExtensible(globalObject).
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
if (!existing_prop.has_value())
|
2021-12-29 15:56:53 +01:00
|
|
|
return TRY(global_object.is_extensible());
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 5. If existingProp.[[Configurable]] is true, return true.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
if (*existing_prop->configurable)
|
2021-06-23 12:26:37 +02:00
|
|
|
return true;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, return true.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
|
2021-06-23 12:26:37 +02:00
|
|
|
return true;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 7. Return false.
|
2021-06-23 12:26:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.16 CreateGlobalVarBinding ( envRec, N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(Utf16FlyString const& name, bool can_be_deleted)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2022-08-21 15:12:43 +01:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 2. Let globalObject be ObjRec.[[BindingObject]].
|
2022-08-22 19:00:49 +01:00
|
|
|
auto& global_object = m_object_record->binding_object();
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
|
2021-12-29 16:00:36 +01:00
|
|
|
auto has_property = TRY(global_object.has_own_property(name));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 4. Let extensible be ? IsExtensible(globalObject).
|
2021-12-29 16:00:36 +01:00
|
|
|
auto extensible = TRY(global_object.is_extensible());
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 5. If hasProperty is false and extensible is true, then
|
2021-06-23 12:26:37 +02:00
|
|
|
if (!has_property && extensible) {
|
2021-12-29 15:48:11 +01:00
|
|
|
// a. Perform ? ObjRec.CreateMutableBinding(N, D).
|
2022-08-21 15:12:43 +01:00
|
|
|
TRY(m_object_record->create_mutable_binding(vm, name, can_be_deleted));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
2022-12-14 13:26:10 +01:00
|
|
|
// b. Perform ? ObjRec.InitializeBinding(N, undefined, normal).
|
|
|
|
TRY(m_object_record->initialize_binding(vm, name, js_undefined(), Environment::InitializeBindingHint::Normal));
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
2021-12-29 15:48:11 +01:00
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 6. Return UNUSED.
|
2021-12-29 16:00:36 +01:00
|
|
|
return {};
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 9.1.1.4.17 CreateGlobalFunctionBinding ( envRec, N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
|
2025-08-02 19:27:29 -04:00
|
|
|
ThrowCompletionOr<void> GlobalEnvironment::create_global_function_binding(Utf16FlyString const& name, Value value, bool can_be_deleted)
|
2021-06-23 12:26:37 +02:00
|
|
|
{
|
2021-12-29 15:48:11 +01:00
|
|
|
// 1. Let ObjRec be envRec.[[ObjectRecord]].
|
|
|
|
// 2. Let globalObject be ObjRec.[[BindingObject]].
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
auto& global_object = m_object_record->binding_object();
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
|
2021-12-29 16:02:44 +01:00
|
|
|
auto existing_prop = TRY(global_object.internal_get_own_property(name));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
2021-06-23 12:26:37 +02:00
|
|
|
PropertyDescriptor desc;
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 4. If existingProp is undefined or existingProp.[[Configurable]] is true, then
|
|
|
|
if (!existing_prop.has_value() || *existing_prop->configurable) {
|
|
|
|
// a. Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
|
2021-12-29 15:48:11 +01:00
|
|
|
}
|
|
|
|
// 5. Else,
|
|
|
|
else {
|
|
|
|
// a. Let desc be the PropertyDescriptor { [[Value]]: V }.
|
LibJS: Rewrite most of Object for spec compliance :^)
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>
2021-07-04 18:14:16 +01:00
|
|
|
desc = { .value = value };
|
2021-12-29 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc).
|
2021-12-29 16:02:44 +01:00
|
|
|
TRY(global_object.define_property_or_throw(name, desc));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
|
|
|
// 7. Perform ? Set(globalObject, N, V, false).
|
2021-12-29 16:02:44 +01:00
|
|
|
TRY(global_object.set(name, value, Object::ShouldThrowExceptions::Yes));
|
2021-12-29 15:48:11 +01:00
|
|
|
|
2025-04-28 17:02:01 -04:00
|
|
|
// 8. Return UNUSED.
|
2021-12-29 16:02:44 +01:00
|
|
|
return {};
|
2021-06-23 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 17:16:08 +02:00
|
|
|
}
|