ErrorConstructor.cs 3.2 KB

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