SuppressedErrorConstructor.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Jint.Native.Error;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.SuppressedError;
  6. internal sealed class SuppressedErrorConstructor : Constructor
  7. {
  8. private static readonly JsString _name = new("SuppressedError");
  9. internal SuppressedErrorConstructor(
  10. Engine engine,
  11. Realm realm,
  12. ErrorConstructor errorConstructor)
  13. : base(engine, realm, _name)
  14. {
  15. _prototype = errorConstructor;
  16. PrototypeObject = new SuppressedErrorPrototype(engine, realm, this, errorConstructor.PrototypeObject);
  17. _length = new PropertyDescriptor(JsNumber.PositiveThree, PropertyFlag.Configurable);
  18. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  19. }
  20. private SuppressedErrorPrototype PrototypeObject { get; }
  21. protected internal override JsValue Call(JsValue thisObject, JsCallArguments arguments)
  22. {
  23. return Construct(arguments, this);
  24. }
  25. /// <summary>
  26. /// https://tc39.es/ecma262/#sec-nativeerror
  27. /// </summary>
  28. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  29. {
  30. var error = arguments.At(0);
  31. var suppressed = arguments.At(1);
  32. var message = arguments.At(2);
  33. return Construct(newTarget, message, error, suppressed);
  34. }
  35. internal JsError Construct(JsValue newTarget, JsValue message, JsValue error, JsValue suppressed)
  36. {
  37. var o = OrdinaryCreateFromConstructor(
  38. newTarget,
  39. static intrinsics => intrinsics.SuppressedError.PrototypeObject,
  40. static (Engine engine, Realm _, object? _) => new JsError(engine));
  41. if (!message.IsUndefined())
  42. {
  43. var msg = TypeConverter.ToString(message);
  44. o.CreateNonEnumerableDataPropertyOrThrow(CommonProperties.Message, msg);
  45. }
  46. o.DefinePropertyOrThrow("error", new PropertyDescriptor(error, configurable: true, enumerable: false, writable: true));
  47. o.DefinePropertyOrThrow("suppressed", new PropertyDescriptor(suppressed, configurable: true, enumerable: false, writable: true));
  48. return o;
  49. }
  50. }