ErrorConstructor.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors.Specialized;
  5. namespace Jint.Native.Error
  6. {
  7. public 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 AllForbiddenPropertyDescriptor(1));
  22. // The initial value of Error.prototype is the Error prototype object
  23. obj.SetOwnProperty("prototype", new AllForbiddenPropertyDescriptor(obj.PrototypeObject));
  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. if (arguments.At(0) != Undefined)
  39. {
  40. instance.Put("message", TypeConverter.ToString(arguments.At(0)), false);
  41. }
  42. return instance;
  43. }
  44. public ErrorPrototype PrototypeObject { get; private set; }
  45. }
  46. }