From 22a78e8a2ccc25752b96097b3b8d258964bbbb46 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Wed, 22 Jun 2022 22:15:59 +0300 Subject: [PATCH] LibJS: Implement the CanBeHeldWeakly abstract operation This AO is required for implementing the rest of the stage 3 'Symbol as WeakMap Keys Proposal'. --- .../LibJS/Runtime/AbstractOperations.cpp | 19 +++++++++++++++++++ .../LibJS/Runtime/AbstractOperations.h | 1 + 2 files changed, 20 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index c151aec699b..38d656822b1 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -456,6 +456,25 @@ Environment& get_this_environment(VM& vm) VERIFY_NOT_REACHED(); } +// 9.14 CanBeHeldWeakly ( v ), https://tc39.es/proposal-symbols-as-weakmap-keys/#sec-canbeheldweakly-abstract-operation +bool can_be_held_weakly(Value value) +{ + // 1. If Type(v) is Object, return true. + if (value.is_object()) + return true; + + // 2. If Type(v) is Symbol, then + if (value.is_symbol()) { + // a. For each element e of the GlobalSymbolRegistry List (see 19.4.2.2), do + // i. If SameValue(e.[[Symbol]], v) is true, return false. + // b. Return true. + return !value.as_symbol().is_global(); + } + + // 3. Return false. + return false; +} + // 13.3.7.2 GetSuperConstructor ( ), https://tc39.es/ecma262/#sec-getsuperconstructor Object* get_super_constructor(VM& vm) { diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 7c0a9cac83e..edfd8406ecc 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -24,6 +24,7 @@ ObjectEnvironment* new_object_environment(Object&, bool is_with_environment, Env FunctionEnvironment* new_function_environment(ECMAScriptFunctionObject&, Object* new_target); PrivateEnvironment* new_private_environment(VM& vm, PrivateEnvironment* outer); Environment& get_this_environment(VM&); +bool can_be_held_weakly(Value); Object* get_super_constructor(VM&); ThrowCompletionOr make_super_property_reference(GlobalObject&, Value actual_this, PropertyKey const&, bool strict); ThrowCompletionOr require_object_coercible(GlobalObject&, Value);