ErrorConstructor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.ToJsString(jsValue);
  45. o.CreateNonEnumerableDataPropertyOrThrow(CommonProperties.Message, msg);
  46. }
  47. var stackString = BuildStackString();
  48. if (stackString is not null)
  49. {
  50. var stackDesc = new PropertyDescriptor(stackString, PropertyFlag.NonEnumerable);
  51. o.DefinePropertyOrThrow(CommonProperties.Stack, stackDesc);
  52. }
  53. var options = arguments.At(1);
  54. if (!options.IsUndefined())
  55. {
  56. o.InstallErrorCause(options);
  57. }
  58. return o;
  59. JsValue? BuildStackString()
  60. {
  61. var lastSyntaxNode = _engine.GetLastSyntaxElement();
  62. if (lastSyntaxNode == null)
  63. {
  64. return null;
  65. }
  66. var callStack = _engine.CallStack;
  67. var currentFunction = callStack.TryPeek(out var element) ? element.Function : null;
  68. // If the current function is the ErrorConstructor itself (i.e. "throw new Error(...)" was called
  69. // from script), exclude it from the stack trace, because the trace should begin at the throw point.
  70. return callStack.BuildCallStackString(lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
  71. }
  72. }
  73. }
  74. }