ladybird/Libraries/LibJS/Runtime/Reference.cpp

227 lines
9.1 KiB
C++
Raw Permalink Normal View History

/*
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Reference.h>
namespace JS {
// 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue
ThrowCompletionOr<void> Reference::put_value(VM& vm, Value value)
{
// 1. ReturnIfAbrupt(V).
// 2. ReturnIfAbrupt(W).
// 3. If V is not a Reference Record, throw a ReferenceError exception.
if (!is_valid_reference())
return vm.throw_completion<ReferenceError>(ErrorType::InvalidLeftHandAssignment);
// 4. If IsUnresolvableReference(V) is true, then
if (is_unresolvable()) {
// a. If V.[[Strict]] is true, throw a ReferenceError exception.
if (m_strict)
return throw_reference_error(vm);
// b. Let globalObj be GetGlobalObject().
auto& global_object = vm.get_global_object();
// c. Perform ? Set(globalObj, V.[[ReferencedName]], W, false).
TRY(global_object.set(name(), value, Object::ShouldThrowExceptions::No));
// Return unused.
return {};
}
// 5. If IsPropertyReference(V) is true, then
if (is_property_reference()) {
// a. Let baseObj be ? ToObject(V.[[Base]]).
auto base_obj = TRY(m_base_value.to_object(vm));
// b. If IsPrivateReference(V) is true, then
if (is_private_reference()) {
// i. Return ? PrivateSet(baseObj, V.[[ReferencedName]], W).
return base_obj->private_set(private_name(), value);
}
// c. Let succeeded be ? baseObj.[[Set]](V.[[ReferencedName]], W, GetThisValue(V)).
auto succeeded = TRY(base_obj->internal_set(name(), value, get_this_value()));
// d. If succeeded is false and V.[[Strict]] is true, throw a TypeError exception.
if (!succeeded && m_strict)
return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name(), m_base_value.to_string_without_side_effects());
// e. Return unused.
return {};
}
// 6. Else,
// a. Let base be V.[[Base]].
// b. Assert: base is an Environment Record.
VERIFY(m_base_type == BaseType::Environment);
LibJS: Make scoping follow the spec Before this we used an ad-hoc combination of references and 'variables' stored in a hashmap. This worked in most cases but is not spec like. Additionally hoisting, dynamically naming functions and scope analysis was not done properly. This patch fixes all of that by: - Implement BindingInitialization for destructuring assignment. - Implementing a new ScopePusher which tracks the lexical and var scoped declarations. This hoists functions to the top level if no lexical declaration name overlaps. Furthermore we do checking of redeclarations in the ScopePusher now requiring less checks all over the place. - Add methods for parsing the directives and statement lists instead of having that code duplicated in multiple places. This allows declarations to pushed to the appropriate scope more easily. - Remove the non spec way of storing 'variables' in DeclarativeEnvironment and make Reference follow the spec instead of checking both the bindings and 'variables'. - Remove all scoping related things from the Interpreter. And instead use environments as specified by the spec. This also includes fixing that NativeFunctions did not produce a valid FunctionEnvironment which could cause issues with callbacks and eval. All FunctionObjects now have a valid NewFunctionEnvironment implementation. - Remove execute_statements from Interpreter and instead use ASTNode::execute everywhere this simplifies AST.cpp as you no longer need to worry about which method to call. - Make ScopeNodes setup their own environment. This uses four different methods specified by the spec {Block, Function, Eval, Global}DeclarationInstantiation with the annexB extensions. - Implement and use NamedEvaluation where specified. Additionally there are fixes to things exposed by these changes to eval, {for, for-in, for-of} loops and assignment. Finally it also fixes some tests in test-js which where passing before but not now that we have correct behavior :^).
2021-09-22 10:44:56 +00:00
VERIFY(m_base_environment);
// c. Return ? base.SetMutableBinding(V.[[ReferencedName]], W, V.[[Strict]]) (see 9.1).
if (m_environment_coordinate.has_value())
return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(vm, m_environment_coordinate->index, value, m_strict);
else
return m_base_environment->set_mutable_binding(vm, name().as_string(), value, m_strict);
}
Completion Reference::throw_reference_error(VM& vm) const
{
if (is_private_reference())
return vm.throw_completion<ReferenceError>(ErrorType::ReferenceUnresolvable);
else
return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name().to_string_or_symbol().to_display_string());
}
// 6.2.4.5 GetValue ( V ), https://tc39.es/ecma262/#sec-getvalue
ThrowCompletionOr<Value> Reference::get_value(VM& vm) const
{
auto& realm = *vm.current_realm();
// 1. ReturnIfAbrupt(V).
// 2. If V is not a Reference Record, return V.
// 3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
if (!is_valid_reference() || is_unresolvable())
return throw_reference_error(vm);
// 4. If IsPropertyReference(V) is true, then
if (is_property_reference()) {
// a. Let baseObj be ? ToObject(V.[[Base]]).
// NOTE: Deferred as an optimization; we might not actually need to create an object.
// b. If IsPrivateReference(V) is true, then
if (is_private_reference()) {
// FIXME: We need to be able to specify the receiver for this
// if we want to use it in error messages in future
// as things currently stand this does the "wrong thing" but
// the error is unobservable
auto base_obj = TRY(m_base_value.to_object(vm));
// i. Return ? PrivateGet(baseObj, V.[[ReferencedName]]).
return base_obj->private_get(private_name());
}
// OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
GC::Ptr<Object> base_obj;
if (m_base_value.is_string()) {
auto string_value = TRY(m_base_value.as_string().get(vm, name()));
if (string_value.has_value())
return *string_value;
base_obj = realm.intrinsics().string_prototype();
} else if (m_base_value.is_number())
base_obj = realm.intrinsics().number_prototype();
else if (m_base_value.is_boolean())
base_obj = realm.intrinsics().boolean_prototype();
else if (m_base_value.is_bigint())
base_obj = realm.intrinsics().bigint_prototype();
else if (m_base_value.is_symbol())
base_obj = realm.intrinsics().symbol_prototype();
else
base_obj = TRY(m_base_value.to_object(vm));
2021-10-12 19:48:50 +00:00
// c. Return ? baseObj.[[Get]](V.[[ReferencedName]], GetThisValue(V)).
return base_obj->internal_get(name(), get_this_value());
}
// 5. Else,
// a. Let base be V.[[Base]].
// b. Assert: base is an Environment Record.
VERIFY(m_base_type == BaseType::Environment);
LibJS: Make scoping follow the spec Before this we used an ad-hoc combination of references and 'variables' stored in a hashmap. This worked in most cases but is not spec like. Additionally hoisting, dynamically naming functions and scope analysis was not done properly. This patch fixes all of that by: - Implement BindingInitialization for destructuring assignment. - Implementing a new ScopePusher which tracks the lexical and var scoped declarations. This hoists functions to the top level if no lexical declaration name overlaps. Furthermore we do checking of redeclarations in the ScopePusher now requiring less checks all over the place. - Add methods for parsing the directives and statement lists instead of having that code duplicated in multiple places. This allows declarations to pushed to the appropriate scope more easily. - Remove the non spec way of storing 'variables' in DeclarativeEnvironment and make Reference follow the spec instead of checking both the bindings and 'variables'. - Remove all scoping related things from the Interpreter. And instead use environments as specified by the spec. This also includes fixing that NativeFunctions did not produce a valid FunctionEnvironment which could cause issues with callbacks and eval. All FunctionObjects now have a valid NewFunctionEnvironment implementation. - Remove execute_statements from Interpreter and instead use ASTNode::execute everywhere this simplifies AST.cpp as you no longer need to worry about which method to call. - Make ScopeNodes setup their own environment. This uses four different methods specified by the spec {Block, Function, Eval, Global}DeclarationInstantiation with the annexB extensions. - Implement and use NamedEvaluation where specified. Additionally there are fixes to things exposed by these changes to eval, {for, for-in, for-of} loops and assignment. Finally it also fixes some tests in test-js which where passing before but not now that we have correct behavior :^).
2021-09-22 10:44:56 +00:00
VERIFY(m_base_environment);
// c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
if (m_environment_coordinate.has_value())
return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(vm, m_environment_coordinate->index);
return m_base_environment->get_binding_value(vm, name().as_string(), m_strict);
}
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 17:14:16 +00:00
// 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
ThrowCompletionOr<bool> Reference::delete_(VM& vm)
{
// 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
// UnaryExpression : delete UnaryExpression
// NOTE: The following steps have already been evaluated by the time we get here:
// 1. Let ref be the result of evaluating UnaryExpression.
// 2. ReturnIfAbrupt(ref).
// 3. If ref is not a Reference Record, return true.
// 4. If IsUnresolvableReference(ref) is true, then
if (is_unresolvable()) {
// a. Assert: ref.[[Strict]] is false.
VERIFY(!m_strict);
// b. Return true.
return true;
}
// 5. If IsPropertyReference(ref) is true, then
if (is_property_reference()) {
// a. Assert: IsPrivateReference(ref) is false.
2021-10-12 19:48:50 +00:00
VERIFY(!is_private_reference());
// b. If IsSuperReference(ref) is true, throw a ReferenceError exception.
if (is_super_reference())
return vm.throw_completion<ReferenceError>(ErrorType::UnsupportedDeleteSuperProperty);
// c. Let baseObj be ? ToObject(ref.[[Base]]).
auto base_obj = TRY(m_base_value.to_object(vm));
// d. Let deleteStatus be ? baseObj.[[Delete]](ref.[[ReferencedName]]).
bool delete_status = TRY(base_obj->internal_delete(name()));
// e. If deleteStatus is false and ref.[[Strict]] is true, throw a TypeError exception.
if (!delete_status && m_strict)
return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishDeleteProperty, name(), m_base_value.to_string_without_side_effects());
// f. Return deleteStatus.
return delete_status;
}
// 6. Else,
// a. Let base be ref.[[Base]].
// b. Assert: base is an Environment Record.
VERIFY(m_base_type == BaseType::Environment);
// c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
return m_base_environment->delete_binding(vm, name().as_string());
}
// 6.2.4.8 InitializeReferencedBinding ( V, W ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
// 1.2.1.1 InitializeReferencedBinding ( V, W, hint ), https://tc39.es/proposal-explicit-resource-management/#sec-initializereferencedbinding
ThrowCompletionOr<void> Reference::initialize_referenced_binding(VM& vm, Value value, Environment::InitializeBindingHint hint) const
{
VERIFY(!is_unresolvable());
VERIFY(m_base_type == BaseType::Environment);
return m_base_environment->initialize_binding(vm, name().as_string(), value, hint);
}
2021-10-12 19:48:50 +00:00
// 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
Reference make_private_reference(VM& vm, Value base_value, DeprecatedFlyString const& private_identifier)
2021-10-12 19:48:50 +00:00
{
// 1. Let privEnv be the running execution context's PrivateEnvironment.
auto private_environment = vm.running_execution_context().private_environment;
// 2. Assert: privEnv is not null.
2021-10-12 19:48:50 +00:00
VERIFY(private_environment);
// 3. Let privateName be ResolvePrivateIdentifier(privEnv, privateIdentifier).
auto private_name = private_environment->resolve_private_identifier(private_identifier);
// 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
return Reference { base_value, private_name };
2021-10-12 19:48:50 +00:00
}
}