ErrorConstructor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Error;
  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 override void Initialize()
  25. {
  26. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  27. {
  28. ["isError"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunction(Engine, "isError", IsError, 1), PropertyFlag.NonEnumerable)),
  29. };
  30. SetProperties(properties);
  31. }
  32. protected internal override JsValue Call(JsValue thisObject, JsCallArguments arguments)
  33. {
  34. return Construct(arguments, this);
  35. }
  36. public ObjectInstance Construct(string? message = null)
  37. {
  38. return Construct(message != null ? [message] : [], this);
  39. }
  40. /// <summary>
  41. /// https://tc39.es/ecma262/#sec-nativeerror
  42. /// </summary>
  43. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  44. {
  45. var o = OrdinaryCreateFromConstructor(
  46. newTarget,
  47. _intrinsicDefaultProto,
  48. static (Engine engine, Realm _, object? _) => new JsError(engine));
  49. var jsValue = arguments.At(0);
  50. if (!jsValue.IsUndefined())
  51. {
  52. var msg = TypeConverter.ToJsString(jsValue);
  53. o.CreateNonEnumerableDataPropertyOrThrow(CommonProperties.Message, msg);
  54. }
  55. var stackString = BuildStackString();
  56. if (stackString is not null)
  57. {
  58. var stackDesc = new PropertyDescriptor(stackString, PropertyFlag.NonEnumerable);
  59. o.DefinePropertyOrThrow(CommonProperties.Stack, stackDesc);
  60. }
  61. var options = arguments.At(1);
  62. if (!options.IsUndefined())
  63. {
  64. o.InstallErrorCause(options);
  65. }
  66. return o;
  67. JsValue? BuildStackString()
  68. {
  69. var lastSyntaxNode = _engine.GetLastSyntaxElement();
  70. if (lastSyntaxNode == null)
  71. {
  72. return null;
  73. }
  74. var callStack = _engine.CallStack;
  75. var currentFunction = callStack.TryPeek(out var element) ? element.Function : null;
  76. // If the current function is the ErrorConstructor itself (i.e. "throw new Error(...)" was called
  77. // from script), exclude it from the stack trace, because the trace should begin at the throw point.
  78. return callStack.BuildCallStackString(_engine, lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
  79. }
  80. }
  81. /// <summary>
  82. /// https://tc39.es/proposal-is-error/
  83. /// </summary>
  84. private static JsValue IsError(JsValue? thisObj, JsCallArguments arguments)
  85. {
  86. return arguments.At(0) is JsError;
  87. }
  88. }