WeakSetPrototype.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.WeakSet
  8. {
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-weakset-objects
  11. /// </summary>
  12. public sealed class WeakSetPrototype : Prototype
  13. {
  14. private readonly WeakSetConstructor _constructor;
  15. internal WeakSetPrototype(
  16. Engine engine,
  17. Realm realm,
  18. WeakSetConstructor 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(5, 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. ["add"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "add", Add, 1, PropertyFlag.Configurable), propertyFlags),
  33. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  34. };
  35. SetProperties(properties);
  36. var symbols = new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("WeakSet", false, false, true)
  39. };
  40. SetSymbols(symbols);
  41. }
  42. private JsValue Add(JsValue thisObj, JsValue[] arguments)
  43. {
  44. var set = AssertWeakSetInstance(thisObj);
  45. set.WeakSetAdd(arguments.At(0));
  46. return thisObj;
  47. }
  48. private JsValue Delete(JsValue thisObj, JsValue[] arguments)
  49. {
  50. var set = AssertWeakSetInstance(thisObj);
  51. return set.WeakSetDelete(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  52. }
  53. private JsValue Has(JsValue thisObj, JsValue[] arguments)
  54. {
  55. var set = AssertWeakSetInstance(thisObj);
  56. return set.WeakSetHas(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
  57. }
  58. private WeakSetInstance AssertWeakSetInstance(JsValue thisObj)
  59. {
  60. var set = thisObj as WeakSetInstance;
  61. if (set is null)
  62. {
  63. ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakSet");
  64. }
  65. return set;
  66. }
  67. }
  68. }