Browse Source

Remove some duplication and align with spec naming (#2128)

Marko Lahma 1 month ago
parent
commit
3dc3134f69

+ 4 - 19
Jint/JsValueExtensions.cs

@@ -133,11 +133,14 @@ public static class JsValueExtensions
         return value._type == InternalTypes.Symbol;
     }
 
+    /// <summary>
+    /// https://tc39.es/ecma262/#sec-canbeheldweakly
+    /// </summary>
     [Pure]
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     internal static bool CanBeHeldWeakly(this JsValue value, GlobalSymbolRegistry symbolRegistry)
     {
-        return value.IsObject() || (value.IsSymbol() && !symbolRegistry.ContainsCustom(value));
+        return value.IsObject() || (value.IsSymbol() && symbolRegistry.KeyForSymbol(value).IsUndefined());
     }
 
     [Pure]
@@ -745,22 +748,4 @@ public static class JsValueExtensions
     {
         return key is JsNumber number && number.IsNegativeZero() ? JsNumber.PositiveZero : key;
     }
-
-    /// <summary>
-    /// https://tc39.es/ecma262/#sec-canbeheldweakly
-    /// </summary>
-    internal static bool CanBeHeldWeakly(this JsValue v, Engine engine)
-    {
-        if (v.IsObject())
-        {
-            return true;
-        }
-
-        if (v is JsSymbol symbol && engine.GlobalSymbolRegistry.KeyForSymbol(symbol).IsUndefined())
-        {
-            return true;
-        }
-
-        return false;
-    }
 }

+ 6 - 16
Jint/Native/Symbol/GlobalSymbolRegistry.cs

@@ -39,24 +39,14 @@ public sealed class GlobalSymbolRegistry
         return new JsSymbol(description);
     }
 
-    internal bool ContainsCustom(JsValue value)
+    /// <summary>
+    /// https://tc39.es/ecma262/#sec-keyforsymbol
+    /// </summary>
+    internal JsValue KeyForSymbol(JsValue value)
     {
-        return value is JsSymbol symbol && _customSymbolLookup?.ContainsKey(symbol._value) == true;
-    }
-
-    internal JsValue KeyForSymbol(JsSymbol symbol)
-    {
-        if (_customSymbolLookup == null)
-        {
-            return JsValue.Undefined;
-        }
-
-        foreach (var pair in _customSymbolLookup)
+        if (value is JsSymbol symbol && _customSymbolLookup?.TryGetValue(symbol._value, out var s) == true)
         {
-            if (pair.Value == symbol)
-            {
-                return pair.Key;
-            }
+            return s._value;
         }
 
         return JsValue.Undefined;

+ 1 - 1
Jint/Native/WeakMap/WeakMapPrototype.cs

@@ -72,7 +72,7 @@ internal sealed class WeakMapPrototype : Prototype
 
     private JsValue AssertCanBeHeldWeakly(JsValue key)
     {
-        if (!key.CanBeHeldWeakly(_engine))
+        if (!key.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
         {
             ExceptionHelper.ThrowTypeError(_realm);
         }