ObjectPrototype.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. _properties = new StringDictionarySlim<PropertyDescriptor>(8)
  24. {
  25. ["constructor"] = new PropertyDescriptor(_objectConstructor, true, false, true),
  26. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToObjectString), true, false, true),
  27. ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString), true, false, true),
  28. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOF", ValueOf), true, false, true),
  29. ["hasOwnProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwnProperty", HasOwnProperty, 1), true, false, true),
  30. ["isPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isPrototypeOf", IsPrototypeOf, 1), true, false, true),
  31. ["propertyIsEnumerable"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "propertyIsEnumerable", PropertyIsEnumerable, 1), true, false, true)
  32. };
  33. }
  34. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  35. {
  36. var p = TypeConverter.ToPropertyKey(arguments[0]);
  37. var o = TypeConverter.ToObject(Engine, thisObject);
  38. var desc = o.GetOwnProperty(p);
  39. if (desc == PropertyDescriptor.Undefined)
  40. {
  41. return false;
  42. }
  43. return desc.Enumerable;
  44. }
  45. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  46. {
  47. var o = TypeConverter.ToObject(Engine, thisObject);
  48. return o;
  49. }
  50. private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
  51. {
  52. var arg = arguments[0];
  53. if (!arg.IsObject())
  54. {
  55. return false;
  56. }
  57. var v = arg.AsObject();
  58. var o = TypeConverter.ToObject(Engine, thisObject);
  59. while (true)
  60. {
  61. v = v.Prototype;
  62. if (ReferenceEquals(v, null))
  63. {
  64. return false;
  65. }
  66. if (o == v)
  67. {
  68. return true;
  69. }
  70. }
  71. }
  72. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  73. {
  74. var o = TypeConverter.ToObject(Engine, thisObject);
  75. var toString = o.Get("toString", o).TryCast<ICallable>(x =>
  76. {
  77. ExceptionHelper.ThrowTypeError(Engine);
  78. });
  79. return toString.Call(o, Arguments.Empty);
  80. }
  81. /// <summary>
  82. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
  83. /// </summary>
  84. /// <param name="thisObject"></param>
  85. /// <param name="arguments"></param>
  86. /// <returns></returns>
  87. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  88. {
  89. if (thisObject.IsUndefined())
  90. {
  91. return "[object Undefined]";
  92. }
  93. if (thisObject.IsNull())
  94. {
  95. return "[object Null]";
  96. }
  97. var o = TypeConverter.ToObject(Engine, thisObject);
  98. return "[object " + o.Class + "]";
  99. }
  100. /// <summary>
  101. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  102. /// </summary>
  103. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  104. {
  105. var p = TypeConverter.ToPropertyKey(arguments[0]);
  106. var o = TypeConverter.ToObject(Engine, thisObject);
  107. var desc = o.GetOwnProperty(p);
  108. return desc != PropertyDescriptor.Undefined;
  109. }
  110. }
  111. }