ObjectConstructor.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using Jint.Native.Function;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Object
  7. {
  8. public class ObjectConstructor : FunctionInstance
  9. {
  10. public ObjectConstructor(ObjectInstance prototype) : base(prototype, null, null)
  11. {
  12. prototype.DefineOwnProperty("hasOwnProperty", new DataDescriptor(new BuiltInPropertyWrapper((Func<ObjectInstance, string, bool>)HasOwnProperty, prototype)), false);
  13. prototype.DefineOwnProperty("toString", new DataDescriptor(new BuiltInPropertyWrapper((Func<ObjectInstance, string>)ToString, prototype)), false);
  14. }
  15. public override dynamic Call(Engine interpreter, object thisObject, dynamic[] arguments)
  16. {
  17. return Undefined.Instance;
  18. }
  19. public ObjectInstance Construct(dynamic[] arguments)
  20. {
  21. var instance = new ObjectInstance(this.Prototype);
  22. // the constructor is the function constructor of an object
  23. instance.DefineOwnProperty("constructor", new DataDescriptor(this) { Writable = true, Enumerable = false, Configurable = false }, false);
  24. instance.DefineOwnProperty("prototype", new DataDescriptor(this.Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
  25. return instance;
  26. }
  27. private static bool HasOwnProperty(ObjectInstance thisObject, string propertyName)
  28. {
  29. var desc = thisObject.GetOwnProperty(propertyName);
  30. return desc != Undefined.Instance;
  31. }
  32. private static string ToString(ObjectInstance thisObject)
  33. {
  34. if (thisObject == null || thisObject == Undefined.Instance)
  35. {
  36. return "[object Undefined]";
  37. }
  38. if (thisObject == Null.Instance)
  39. {
  40. return "[object Null]";
  41. }
  42. return string.Format("[object {0}]", thisObject.Class);
  43. }
  44. }
  45. }