WeakMapPrototype.cs 3.1 KB

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