AggregateErrorConstructor.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Jint.Native.Error;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Native.TypedArray;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Native.AggregateError;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-aggregate-error-constructor
  10. /// </summary>
  11. internal sealed class AggregateErrorConstructor : FunctionInstance, IConstructor
  12. {
  13. private static readonly JsString _name = new("AggregateError");
  14. internal AggregateErrorConstructor(
  15. Engine engine,
  16. Realm realm,
  17. ErrorConstructor errorConstructor)
  18. : base(engine, realm, _name)
  19. {
  20. _prototype = errorConstructor;
  21. PrototypeObject = new AggregateErrorPrototype(engine, realm, this, errorConstructor.PrototypeObject);
  22. _length = new PropertyDescriptor(JsNumber.PositiveTwo, PropertyFlag.Configurable);
  23. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  24. }
  25. private AggregateErrorPrototype PrototypeObject { get; }
  26. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  27. {
  28. return Construct(arguments, this);
  29. }
  30. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
  31. /// <summary>
  32. /// https://tc39.es/ecma262/#sec-nativeerror
  33. /// </summary>
  34. private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  35. {
  36. var errors = arguments.At(0);
  37. var message = arguments.At(1);
  38. var options = arguments.At(2);
  39. var o = OrdinaryCreateFromConstructor(
  40. newTarget,
  41. static intrinsics => intrinsics.AggregateError.PrototypeObject,
  42. static (Engine engine, Realm _, object? _) => new ErrorInstance(engine));
  43. if (!message.IsUndefined())
  44. {
  45. var msg = TypeConverter.ToString(message);
  46. o.CreateNonEnumerableDataPropertyOrThrow("message", msg);
  47. }
  48. o.InstallErrorCause(options);
  49. var errorsList = TypedArrayConstructor.IterableToList(_realm, errors);
  50. o.DefinePropertyOrThrow("errors", new PropertyDescriptor(_realm.Intrinsics.Array.CreateArrayFromList(errorsList), configurable: true, enumerable: false, writable: true));
  51. return o;
  52. }
  53. }