WeakMapPrototype.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.WeakMap;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-weakmap-objects
  10. /// </summary>
  11. internal sealed class WeakMapPrototype : Prototype
  12. {
  13. private readonly WeakMapConstructor _constructor;
  14. internal WeakMapPrototype(
  15. Engine engine,
  16. Realm realm,
  17. WeakMapConstructor constructor,
  18. ObjectPrototype prototype) : base(engine, realm)
  19. {
  20. _prototype = prototype;
  21. _constructor = constructor;
  22. }
  23. protected override void Initialize()
  24. {
  25. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  26. var properties = new PropertyDictionary(6, checkExistingKeys: false)
  27. {
  28. ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable),
  29. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  30. ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags),
  31. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags),
  32. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  33. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), propertyFlags),
  34. };
  35. SetProperties(properties);
  36. var symbols = new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("WeakMap", false, false, true)
  39. };
  40. SetSymbols(symbols);
  41. }
  42. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  43. {
  44. var map = AssertWeakMapInstance(thisObject);
  45. return map.WeakMapGet(arguments.At(0));
  46. }
  47. private JsValue Delete(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var map = AssertWeakMapInstance(thisObject);
  50. return arguments.Length > 0 && map.WeakMapDelete(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  51. }
  52. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  53. {
  54. var map = AssertWeakMapInstance(thisObject);
  55. map.WeakMapSet(arguments.At(0), arguments.At(1));
  56. return thisObject;
  57. }
  58. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  59. {
  60. var map = AssertWeakMapInstance(thisObject);
  61. return map.WeakMapHas(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  62. }
  63. private JsWeakMap AssertWeakMapInstance(JsValue thisObject)
  64. {
  65. if (thisObject is JsWeakMap map)
  66. {
  67. return map;
  68. }
  69. ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakMap");
  70. return default;
  71. }
  72. }