ErrorConstructor.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 string _name;
  10. public ErrorConstructor(Engine engine) : base(engine, null, null, false)
  11. {
  12. }
  13. public static ErrorConstructor CreateErrorConstructor(Engine engine, string name)
  14. {
  15. var obj = new ErrorConstructor(engine);
  16. obj.Extensible = true;
  17. obj._name = name;
  18. // The value of the [[Prototype]] internal property of the Error constructor is the Function prototype object (15.11.3)
  19. obj.Prototype = engine.Function.PrototypeObject;
  20. obj.PrototypeObject = ErrorPrototype.CreatePrototypeObject(engine, obj, name);
  21. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  22. // The initial value of Error.prototype is the Error prototype object
  23. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  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. }