ErrorConstructor.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. namespace Jint.Native.Error;
  5. public sealed class ErrorConstructor : Constructor
  6. {
  7. private readonly Func<Intrinsics, ObjectInstance> _intrinsicDefaultProto;
  8. internal ErrorConstructor(
  9. Engine engine,
  10. Realm realm,
  11. ObjectInstance functionPrototype,
  12. ObjectInstance objectPrototype,
  13. JsString name, Func<Intrinsics, ObjectInstance> intrinsicDefaultProto)
  14. : base(engine, realm, name)
  15. {
  16. _intrinsicDefaultProto = intrinsicDefaultProto;
  17. _prototype = functionPrototype;
  18. PrototypeObject = new ErrorPrototype(engine, realm, this, objectPrototype, name);
  19. _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
  20. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  21. }
  22. internal ErrorPrototype PrototypeObject { get; }
  23. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  24. {
  25. return Construct(arguments, this);
  26. }
  27. public ObjectInstance Construct(string? message = null)
  28. {
  29. return Construct(message != null ? new JsValue[]{ message } : System.Array.Empty<JsValue>(), this);
  30. }
  31. /// <summary>
  32. /// https://tc39.es/ecma262/#sec-nativeerror
  33. /// </summary>
  34. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  35. {
  36. var o = OrdinaryCreateFromConstructor(
  37. newTarget,
  38. _intrinsicDefaultProto,
  39. static (Engine engine, Realm _, object? _) => new JsError(engine));
  40. var jsValue = arguments.At(0);
  41. if (!jsValue.IsUndefined())
  42. {
  43. var msg = TypeConverter.ToJsString(jsValue);
  44. o.CreateNonEnumerableDataPropertyOrThrow(CommonProperties.Message, msg);
  45. }
  46. var stackString = BuildStackString();
  47. if (stackString is not null)
  48. {
  49. var stackDesc = new PropertyDescriptor(stackString, PropertyFlag.NonEnumerable);
  50. o.DefinePropertyOrThrow(CommonProperties.Stack, stackDesc);
  51. }
  52. var options = arguments.At(1);
  53. if (!options.IsUndefined())
  54. {
  55. o.InstallErrorCause(options);
  56. }
  57. return o;
  58. JsValue? BuildStackString()
  59. {
  60. var lastSyntaxNode = _engine.GetLastSyntaxElement();
  61. if (lastSyntaxNode == null)
  62. {
  63. return null;
  64. }
  65. var callStack = _engine.CallStack;
  66. var currentFunction = callStack.TryPeek(out var element) ? element.Function : null;
  67. // If the current function is the ErrorConstructor itself (i.e. "throw new Error(...)" was called
  68. // from script), exclude it from the stack trace, because the trace should begin at the throw point.
  69. return callStack.BuildCallStackString(lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
  70. }
  71. }
  72. }