ObjectPrototype.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Jint.Collections;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Object
  6. {
  7. public sealed class ObjectPrototype : ObjectInstance
  8. {
  9. private ObjectConstructor _objectConstructor;
  10. private ObjectPrototype(Engine engine) : base(engine)
  11. {
  12. }
  13. public static ObjectPrototype CreatePrototypeObject(Engine engine, ObjectConstructor objectConstructor)
  14. {
  15. var obj = new ObjectPrototype(engine)
  16. {
  17. _objectConstructor = objectConstructor
  18. };
  19. return obj;
  20. }
  21. protected override void Initialize()
  22. {
  23. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  24. var properties = new PropertyDictionary(8, checkExistingKeys: false)
  25. {
  26. ["constructor"] = new PropertyDescriptor(_objectConstructor, propertyFlags),
  27. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToObjectString), propertyFlags),
  28. ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString), propertyFlags),
  29. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOF", ValueOf), propertyFlags),
  30. ["hasOwnProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwnProperty", HasOwnProperty, 1), propertyFlags),
  31. ["isPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isPrototypeOf", IsPrototypeOf, 1), propertyFlags),
  32. ["propertyIsEnumerable"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "propertyIsEnumerable", PropertyIsEnumerable, 1), propertyFlags)
  33. };
  34. SetProperties(properties);
  35. }
  36. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  37. {
  38. var p = TypeConverter.ToPropertyKey(arguments[0]);
  39. var o = TypeConverter.ToObject(Engine, thisObject);
  40. var desc = o.GetOwnProperty(p);
  41. if (desc == PropertyDescriptor.Undefined)
  42. {
  43. return false;
  44. }
  45. return desc.Enumerable;
  46. }
  47. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var o = TypeConverter.ToObject(Engine, thisObject);
  50. return o;
  51. }
  52. private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
  53. {
  54. var arg = arguments[0];
  55. if (!arg.IsObject())
  56. {
  57. return false;
  58. }
  59. var v = arg.AsObject();
  60. var o = TypeConverter.ToObject(Engine, thisObject);
  61. while (true)
  62. {
  63. v = v.Prototype;
  64. if (ReferenceEquals(v, null))
  65. {
  66. return false;
  67. }
  68. if (ReferenceEquals(o, v))
  69. {
  70. return true;
  71. }
  72. }
  73. }
  74. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  75. {
  76. var o = TypeConverter.ToObject(Engine, thisObject);
  77. var toString = o.Get("toString", o).TryCast<ICallable>(x =>
  78. {
  79. ExceptionHelper.ThrowTypeError(Engine);
  80. });
  81. return toString.Call(o, Arguments.Empty);
  82. }
  83. /// <summary>
  84. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
  85. /// </summary>
  86. /// <param name="thisObject"></param>
  87. /// <param name="arguments"></param>
  88. /// <returns></returns>
  89. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  90. {
  91. if (thisObject.IsUndefined())
  92. {
  93. return "[object Undefined]";
  94. }
  95. if (thisObject.IsNull())
  96. {
  97. return "[object Null]";
  98. }
  99. var o = TypeConverter.ToObject(Engine, thisObject);
  100. return "[object " + o.Class + "]";
  101. }
  102. /// <summary>
  103. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  104. /// </summary>
  105. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  106. {
  107. var p = TypeConverter.ToPropertyKey(arguments[0]);
  108. var o = TypeConverter.ToObject(Engine, thisObject);
  109. var desc = o.GetOwnProperty(p);
  110. return desc != PropertyDescriptor.Undefined;
  111. }
  112. }
  113. }