ObjectPrototype.cs 4.4 KB

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