ErrorConstructor.cs 3.3 KB

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