WeakRefPrototype.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.WeakRef;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-properties-of-the-weak-ref-prototype-object
  10. /// </summary>
  11. internal sealed class WeakRefPrototype : Prototype
  12. {
  13. private readonly WeakRefConstructor _constructor;
  14. internal WeakRefPrototype(
  15. Engine engine,
  16. Realm realm,
  17. WeakRefConstructor 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(5, checkExistingKeys: false)
  27. {
  28. ["constructor"] = new(_constructor, PropertyFlag.NonEnumerable),
  29. ["deref"] = new(new ClrFunctionInstance(Engine, "deref", Deref, 0, PropertyFlag.Configurable), propertyFlags)
  30. };
  31. SetProperties(properties);
  32. var symbols = new SymbolDictionary(1)
  33. {
  34. [GlobalSymbolRegistry.ToStringTag] = new("WeakRef", false, false, true)
  35. };
  36. SetSymbols(symbols);
  37. }
  38. private JsValue Deref(JsValue thisObj, JsValue[] arguments)
  39. {
  40. var weakRef = thisObj as WeakRefInstance;
  41. if (weakRef is null)
  42. {
  43. ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakRef");
  44. }
  45. return weakRef.WeakRefDeref();
  46. }
  47. }