ErrorConstructor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 JsString _name;
  10. private static readonly JsString _functionName = new JsString("Error");
  11. public ErrorConstructor(Engine engine) : base(engine, _functionName, strict: false)
  12. {
  13. }
  14. public static ErrorConstructor CreateErrorConstructor(Engine engine, JsString name)
  15. {
  16. var obj = new ErrorConstructor(engine)
  17. {
  18. Extensible = true,
  19. _name = name,
  20. Prototype = engine.Function.PrototypeObject
  21. };
  22. // The value of the [[Prototype]] internal property of the Error constructor is the Function prototype object (15.11.3)
  23. obj.PrototypeObject = ErrorPrototype.CreatePrototypeObject(engine, obj, name);
  24. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
  25. // The initial value of Error.prototype is the Error prototype object
  26. obj._prototype = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  27. return obj;
  28. }
  29. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  30. {
  31. return Construct(arguments);
  32. }
  33. public ObjectInstance Construct(JsValue[] arguments)
  34. {
  35. var instance = new ErrorInstance(Engine, _name);
  36. instance.Prototype = PrototypeObject;
  37. instance.Extensible = true;
  38. var jsValue = arguments.At(0);
  39. if (!jsValue.IsUndefined())
  40. {
  41. instance.Put("message", TypeConverter.ToString(jsValue), false);
  42. }
  43. return instance;
  44. }
  45. public ErrorPrototype PrototypeObject { get; private set; }
  46. }
  47. }