WeakMapPrototype.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  3. using Jint.Native.Object;
  4. using Jint.Native.Symbol;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.WeakMap;
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-weakmap-objects
  11. /// </summary>
  12. internal 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 ClrFunction(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags),
  32. ["get"] = new PropertyDescriptor(new ClrFunction(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags),
  33. ["has"] = new PropertyDescriptor(new ClrFunction(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  34. ["set"] = new PropertyDescriptor(new ClrFunction(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 thisObject, JsValue[] arguments)
  44. {
  45. var map = AssertWeakMapInstance(thisObject);
  46. return map.WeakMapGet(arguments.At(0));
  47. }
  48. private JsValue Delete(JsValue thisObject, JsValue[] arguments)
  49. {
  50. var map = AssertWeakMapInstance(thisObject);
  51. return arguments.Length > 0 && map.WeakMapDelete(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  52. }
  53. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  54. {
  55. var map = AssertWeakMapInstance(thisObject);
  56. map.WeakMapSet(arguments.At(0), arguments.At(1));
  57. return thisObject;
  58. }
  59. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  60. {
  61. var map = AssertWeakMapInstance(thisObject);
  62. return map.WeakMapHas(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  63. }
  64. private JsWeakMap AssertWeakMapInstance(JsValue thisObject)
  65. {
  66. if (thisObject is JsWeakMap map)
  67. {
  68. return map;
  69. }
  70. ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakMap");
  71. return default;
  72. }
  73. }