ErrorConstructor.cs 3.3 KB

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