WeakRefPrototype.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Jint.Native.Object;
  2. using Jint.Native.Symbol;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.WeakRef;
  7. /// <summary>
  8. /// https://tc39.es/ecma262/#sec-properties-of-the-weak-ref-prototype-object
  9. /// </summary>
  10. internal sealed class WeakRefPrototype : Prototype
  11. {
  12. private readonly WeakRefConstructor _constructor;
  13. internal WeakRefPrototype(
  14. Engine engine,
  15. Realm realm,
  16. WeakRefConstructor constructor,
  17. ObjectPrototype prototype) : base(engine, realm)
  18. {
  19. _prototype = prototype;
  20. _constructor = constructor;
  21. }
  22. protected override void Initialize()
  23. {
  24. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  25. var properties = new PropertyDictionary(5, checkExistingKeys: false)
  26. {
  27. ["constructor"] = new(_constructor, PropertyFlag.NonEnumerable),
  28. ["deref"] = new(new ClrFunction(Engine, "deref", Deref, 0, PropertyFlag.Configurable), propertyFlags)
  29. };
  30. SetProperties(properties);
  31. var symbols = new SymbolDictionary(1)
  32. {
  33. [GlobalSymbolRegistry.ToStringTag] = new("WeakRef", false, false, true)
  34. };
  35. SetSymbols(symbols);
  36. }
  37. private JsValue Deref(JsValue thisObject, JsCallArguments arguments)
  38. {
  39. if (thisObject is JsWeakRef weakRef)
  40. {
  41. return weakRef.WeakRefDeref();
  42. }
  43. Throw.TypeError(_realm, "object must be a WeakRef");
  44. return default;
  45. }
  46. }