Skip to content

Truly independent web browser

LibJS+LibWeb: Resolve a binding to the environment that has it

Shannon Boothe25a57f566f9
diff --git a/Libraries/LibJS/CMakeLists.txt b/Libraries/LibJS/CMakeLists.txt
index 3212f705fe1..d9ec3abd7c0 100644
--- a/Libraries/LibJS/CMakeLists.txt
+++ b/Libraries/LibJS/CMakeLists.txt
@@ -76,7 +76,6 @@ set(SOURCES
Runtime/PromiseJob.cpp
Runtime/PropertyDescriptor.cpp
Runtime/Realm.cpp
- Runtime/Reference.cpp
Runtime/RegExpObject.cpp
Runtime/Set.cpp
Runtime/SetIterator.cpp
diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h
index 9d9a0308060..88645420dca 100644
--- a/Libraries/LibJS/Forward.h
+++ b/Libraries/LibJS/Forward.h
@@ -170,7 +170,6 @@ class PropertyAttributes;
class PropertyDescriptor;
class PropertyKey;
class Realm;
-class Reference;
class Script;
class SetIterator;
class Shape;
diff --git a/Libraries/LibJS/Runtime/Reference.cpp b/Libraries/LibJS/Runtime/Reference.cpp
deleted file mode 100644
index 31c081c7c6c..00000000000
--- a/Libraries/LibJS/Runtime/Reference.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (c) 2020-2021, Andreas Kling <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibJS/EmbeddingABIConversions.h>
-#include <LibJS/Runtime/Reference.h>
-#include <LibJS/Runtime/VM.h>
-
-namespace JS {
-
-using namespace EmbeddingABI;
-
-// 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue
-ThrowCompletionOr<void> Reference::put_value(VM& vm, Value value)
-{
- // 4. If IsUnresolvableReference(V) is true, then
- if (is_unresolvable()) {
- // a. If V.[[Strict]] is true, throw a ReferenceError exception.
- if (m_strict == Strict::Yes)
- return throw_reference_error(vm);
-
- // b. Let globalObj be GetGlobalObject().
- auto& global_object = vm.current_realm()->global_object();
-
- // c. Perform ? Set(globalObj, V.[[ReferencedName]], W, false).
- TRY(completion_from_abi<void>(js_object_set(vm_to_abi(vm), object_to_abi(global_object), property_key_to_abi(m_name), value_to_abi(value), false)));
-
- // Return unused.
- return {};
- }
-
- // 6. Else,
- // a. Let base be V.[[Base]].
- // b. Assert: base is an Environment Record.
- VERIFY(m_base_environment);
-
- // c. Return ? base.SetMutableBinding(V.[[ReferencedName]], W, V.[[Strict]]) (see 9.1).
- return m_base_environment->set_mutable_binding(vm, m_name.as_string(), value, m_strict == Strict::Yes);
-}
-
-Completion Reference::throw_reference_error(VM& vm) const
-{
- return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, m_name.to_utf16_string());
-}
-
-// 6.2.4.5 GetValue ( V ), https://tc39.es/ecma262/#sec-getvalue
-ThrowCompletionOr<Value> Reference::get_value(VM& vm) const
-{
- // 3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
- if (is_unresolvable())
- return throw_reference_error(vm);
-
- // 5. Else,
- // a. Let base be V.[[Base]].
- // b. Assert: base is an Environment Record.
- VERIFY(m_base_environment);
-
- // c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
- return m_base_environment->get_binding_value(vm, m_name.as_string(), m_strict == Strict::Yes);
-}
-
-// 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
-ThrowCompletionOr<bool> Reference::delete_(VM& vm)
-{
- // 4. If IsUnresolvableReference(ref) is true, then
- if (is_unresolvable()) {
- // a. Assert: ref.[[Strict]] is false.
- VERIFY(m_strict == Strict::No);
- // b. Return true.
- return true;
- }
-
- // 6. Else,
- // a. Let base be ref.[[Base]].
- // b. Assert: base is an Environment Record.
- VERIFY(m_base_environment);
-
- // c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
- return m_base_environment->delete_binding(vm, m_name.as_string());
-}
-
-}
diff --git a/Libraries/LibJS/Runtime/Reference.h b/Libraries/LibJS/Runtime/Reference.h
deleted file mode 100644
index 44347c7a44f..00000000000
--- a/Libraries/LibJS/Runtime/Reference.h
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (c) 2020-2021, Andreas Kling <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Utf16FlyString.h>
-#include <LibGC/Cell.h>
-#include <LibGC/Ptr.h>
-#include <LibJS/Export.h>
-#include <LibJS/Runtime/Environment.h>
-#include <LibJS/Runtime/PropertyKey.h>
-#include <LibJS/Runtime/Value.h>
-
-namespace JS {
-
-// 6.2.5 The Reference Record Specification Type, https://tc39.es/ecma262/#sec-reference-record-specification-type
-// The Reference Records that VM::resolve_binding() resolves an identifier to: unresolvable, or with an Environment
-// Record as their base.
-class JS_API Reference {
-public:
- enum class BaseType : u8 {
- Unresolvable,
- Environment,
- };
-
- Reference(BaseType type, PropertyKey name, Strict strict)
- : m_name(move(name))
- , m_base_type(type)
- , m_strict(strict)
- {
- VERIFY(type == BaseType::Unresolvable);
- }
-
- Reference(Environment& base, Utf16FlyString referenced_name, Strict strict)
- : m_name(move(referenced_name))
- , m_base_environment(&base)
- , m_base_type(BaseType::Environment)
- , m_strict(strict)
- {
- }
-
- Environment& base_environment() const
- {
- VERIFY(m_base_type == BaseType::Environment);
- return *m_base_environment;
- }
-
- PropertyKey const& name() const { return m_name; }
- bool is_strict() const { return m_strict == Strict::Yes; }
-
- // 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
- bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
-
- // 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
- bool is_property_reference() const { return false; }
-
- // Note: Non-standard helper.
- bool is_environment_reference() const
- {
- return m_base_type == BaseType::Environment;
- }
-
- ThrowCompletionOr<void> put_value(VM&, Value);
- ThrowCompletionOr<Value> get_value(VM&) const;
- ThrowCompletionOr<bool> delete_(VM&);
-
- void visit_edges(GC::Cell::Visitor& visitor)
- {
- visitor.visit(m_base_environment);
- m_name.visit_edges(visitor);
- }
-
-private:
- Completion throw_reference_error(VM&) const;
-
- PropertyKey m_name;
- GC::Ptr<Environment> m_base_environment;
- BaseType m_base_type { BaseType::Unresolvable };
- Strict m_strict { Strict::No };
-};
-
-}
diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp
index 65cb2d34c92..abf03dca56d 100644
--- a/Libraries/LibJS/Runtime/VM.cpp
+++ b/Libraries/LibJS/Runtime/VM.cpp
@@ -15,7 +15,6 @@
#include <LibJS/Runtime/JobCallback.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/PromiseCapability.h>
-#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Script.h>
#include <LibJS/ScriptAndModuleABIConversions.h>
@@ -508,7 +507,7 @@ Vector<StackTraceElement> VM::stack_trace() const
return stack_trace;
}
-ThrowCompletionOr<Reference> VM::resolve_binding(Utf16FlyString const& name, Strict strict, GC::Ptr<Environment> environment)
+ThrowCompletionOr<GC::Ptr<Environment>> VM::resolve_binding(Utf16FlyString const& name, Strict strict, GC::Ptr<Environment> environment)
{
// 1. If env is not present or if env is undefined, then
// a. Set env to the running execution context's LexicalEnvironment.
@@ -519,10 +518,7 @@ ThrowCompletionOr<Reference> VM::resolve_binding(Utf16FlyString const& name, Str
VERIFY(environment);
// 4. Return ? GetIdentifierReference(env, name, strict).
- auto base_environment = TRY(completion_from_abi<GC::Ptr<Environment>>(js_environment_resolve_binding(vm_abi(*this), utf16_view_to_abi(name.view()), strict == Strict::Yes, cell_to_abi<JSEnvironment>(*environment))));
- if (!base_environment)
- return Reference { Reference::BaseType::Unresolvable, name, strict };
- return Reference { *base_environment, name, strict };
+ return completion_from_abi<GC::Ptr<Environment>>(js_environment_resolve_binding(vm_abi(*this), utf16_view_to_abi(name.view()), strict == Strict::Yes, cell_to_abi<JSEnvironment>(*environment)));
}
Completion VM::throw_engine_error(EngineErrorKind kind, Utf16View message)
diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h
index 145e76b5bce..edf6724ed20 100644
--- a/Libraries/LibJS/Runtime/VM.h
+++ b/Libraries/LibJS/Runtime/VM.h
@@ -324,7 +324,8 @@ public:
void finish_execution_generation();
// 9.4.2 ResolveBinding ( name [ , env ] ), https://tc39.es/ecma262/#sec-resolvebinding
- ThrowCompletionOr<Reference> resolve_binding(Utf16FlyString const&, Strict, GC::Ptr<Environment> = nullptr);
+ // Returns the base of the Reference Record, or null if the reference is unresolvable.
+ ThrowCompletionOr<GC::Ptr<Environment>> resolve_binding(Utf16FlyString const&, Strict, GC::Ptr<Environment> = nullptr);
// Has the TypeErrors that are thrown at the execution context stack depth the scope was created at created in
// `realm`, until the scope is restored. Callees that push execution contexts are unaffected.
diff --git a/Libraries/LibJS/Rust/src/embedding/environment.rs b/Libraries/LibJS/Rust/src/embedding/environment.rs
index 0d76a35ab31..d5520d424bd 100644
--- a/Libraries/LibJS/Rust/src/embedding/environment.rs
+++ b/Libraries/LibJS/Rust/src/embedding/environment.rs
@@ -232,10 +232,10 @@ pub unsafe extern "C" fn js_environment_delete_binding(
/// reference is unresolvable. HasBinding runs on the way, which for the object environment of a with statement can
/// run JavaScript and throw.
///
-/// The C++ Reference Record this resolves to has the environment as its base. GetValue, PutValue and delete of it are
-/// js_environment_get_binding_value(), js_environment_set_mutable_binding() and js_environment_delete_binding() with
-/// the same name and strictness, and for an unresolvable reference GetValue throws a ReferenceError, PutValue sets the
-/// property of the global object unless it is strict, and delete returns true. Borrows the name. Main thread only.
+/// GetValue, PutValue and delete of the Reference Record are js_environment_get_binding_value(),
+/// js_environment_set_mutable_binding() and js_environment_delete_binding() on the environment with the same name and
+/// strictness. For an unresolvable reference, GetValue throws a ReferenceError, PutValue sets the property of the global
+/// object unless it is strict, and delete returns true. Borrows the name. Main thread only.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn js_environment_resolve_binding(
vm: *mut JSVM,
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index c6987f90e37..8a494220f52 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -22,9 +22,9 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Date.h>
+#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Object.h>
-#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/VM.h>
#include <LibMediaClient/Client.h>
#include <LibURL/Parser.h>
@@ -420,14 +420,16 @@ WebIDL::ExceptionOr<void> Internals::mark_as_garbage(Utf16String const& variable
if (!outer_environment.has_value())
return vm.throw_completion<JS::ReferenceError>(JS::ErrorType::UnknownIdentifier, variable_name_fly);
- auto reference = TRY(vm.resolve_binding(variable_name_fly, JS::Strict::No, outer_environment.value()->lexical_environment));
- auto value = TRY(reference.get_value(vm));
+ auto environment = TRY(vm.resolve_binding(variable_name_fly, JS::Strict::No, outer_environment.value()->lexical_environment));
+ if (!environment)
+ return vm.throw_completion<JS::ReferenceError>(JS::ErrorType::UnknownIdentifier, variable_name_fly);
+ auto value = TRY(environment->get_binding_value(vm, variable_name_fly, false));
if (!JS::can_be_held_weakly(value))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::CannotBeHeldWeakly, value);
- TRY(reference.put_value(vm, JS::js_undefined()));
- (void)TRY(reference.delete_(vm));
+ TRY(environment->set_mutable_binding(vm, variable_name_fly, JS::js_undefined(), false));
+ (void)TRY(environment->delete_binding(vm, variable_name_fly));
vm.heap().uproot_cell(&value.as_cell());
return {};
}
diff --git a/Meta/Linters/check_libjs_includes.py b/Meta/Linters/check_libjs_includes.py
index 1b7541a2cea..63201a61c9d 100755
--- a/Meta/Linters/check_libjs_includes.py
+++ b/Meta/Linters/check_libjs_includes.py
@@ -74,7 +74,6 @@ ALLOWED_HEADERS = {
"LibJS/Runtime/PropertyDescriptor.h",
"LibJS/Runtime/PropertyKey.h",
"LibJS/Runtime/Realm.h",
- "LibJS/Runtime/Reference.h",
"LibJS/Runtime/RegExpObject.h",
"LibJS/Runtime/Set.h",
"LibJS/Runtime/SetIterator.h",