ErrorConstructor.cs 3.5 KB

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