ErrorConstructor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. var stackDesc = new PropertyDescriptor(stackString, PropertyFlag.NonEnumerable);
  52. o.DefinePropertyOrThrow(CommonProperties.Stack, stackDesc);
  53. var options = arguments.At(1);
  54. o.InstallErrorCause(options);
  55. return o;
  56. JsValue BuildStackString()
  57. {
  58. var lastSyntaxNode = _engine.GetLastSyntaxNode();
  59. if (lastSyntaxNode == null)
  60. return Undefined;
  61. var callStack = _engine.CallStack;
  62. var currentFunction = callStack.TryPeek(out var element) ? element.Function : null;
  63. // If the current function is the ErrorConstructor itself (i.e. "throw new Error(...)" was called
  64. // from script), exclude it from the stack trace, because the trace should begin at the throw point.
  65. return callStack.BuildCallStackString(lastSyntaxNode.Location, currentFunction == this ? 1 : 0);
  66. }
  67. }
  68. }
  69. }