2
0

WeakRefConstructor.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.WeakRef;
  6. /// <summary>
  7. /// https://tc39.es/ecma262/#sec-weak-ref-constructor
  8. /// </summary>
  9. internal sealed class WeakRefConstructor : FunctionInstance, IConstructor
  10. {
  11. private static readonly JsString _functionName = new("WeakRef");
  12. internal WeakRefConstructor(
  13. Engine engine,
  14. Realm realm,
  15. FunctionPrototype functionPrototype,
  16. ObjectPrototype objectPrototype)
  17. : base(engine, realm, _functionName)
  18. {
  19. _prototype = functionPrototype;
  20. PrototypeObject = new WeakRefPrototype(engine, realm, this, objectPrototype);
  21. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  22. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  23. }
  24. private WeakRefPrototype PrototypeObject { get; }
  25. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  26. {
  27. ExceptionHelper.ThrowTypeError(_realm, "Constructor WeakRef requires 'new'");
  28. return null;
  29. }
  30. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  31. {
  32. if (newTarget.IsUndefined())
  33. {
  34. ExceptionHelper.ThrowTypeError(_realm);
  35. }
  36. var target = arguments.At(0);
  37. if (target is not ObjectInstance)
  38. {
  39. ExceptionHelper.ThrowTypeError(_realm, "WeakRef: target must be an object");
  40. }
  41. var weakRef = OrdinaryCreateFromConstructor(
  42. newTarget,
  43. static intrinsics => intrinsics.WeakRef.PrototypeObject,
  44. static (Engine engine, Realm _, object? t) => new WeakRefInstance(engine, (ObjectInstance) t!),
  45. target);
  46. _engine.AddToKeptObjects(target);
  47. return weakRef;
  48. }
  49. }