WeakMapPrototype.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  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 ClrFunction(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags),
  31. ["get"] = new PropertyDescriptor(new ClrFunction(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags),
  32. ["has"] = new PropertyDescriptor(new ClrFunction(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  33. ["set"] = new PropertyDescriptor(new ClrFunction(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, JsCallArguments arguments)
  43. {
  44. var map = AssertWeakMapInstance(thisObject);
  45. return map.WeakMapGet(arguments.At(0));
  46. }
  47. private JsValue Delete(JsValue thisObject, JsCallArguments 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, JsCallArguments 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, JsCallArguments 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. }