ObjectPrototype.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Jint.Collections;
  2. using Jint.Native.Array;
  3. using Jint.Native.Proxy;
  4. using Jint.Native.Symbol;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Object
  9. {
  10. public sealed class ObjectPrototype : Prototype
  11. {
  12. private readonly ObjectConstructor _constructor;
  13. internal ObjectChangeFlags _objectChangeFlags;
  14. internal ObjectPrototype(
  15. Engine engine,
  16. Realm realm,
  17. ObjectConstructor constructor) : base(engine, realm)
  18. {
  19. _constructor = constructor;
  20. }
  21. protected override void Initialize()
  22. {
  23. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  24. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  25. var properties = new PropertyDictionary(8, checkExistingKeys: false)
  26. {
  27. ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags),
  28. ["__proto__"] = new GetSetPropertyDescriptor(
  29. new ClrFunctionInstance(Engine, "get __proto__", (thisObject, _) => TypeConverter.ToObject(_realm, thisObject).GetPrototypeOf() ?? Null, 0, lengthFlags),
  30. new ClrFunctionInstance(Engine, "set __proto__", (thisObject, arguments) =>
  31. {
  32. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  33. var proto = arguments.At(0);
  34. if (!proto.IsObject() && !proto.IsNull() || thisObject is not ObjectInstance objectInstance)
  35. {
  36. return Undefined;
  37. }
  38. if (!objectInstance.SetPrototypeOf(proto))
  39. {
  40. ExceptionHelper.ThrowTypeError(_realm, "Invalid prototype");
  41. }
  42. return Undefined;
  43. }, 0, lengthFlags),
  44. enumerable: false, configurable: true),
  45. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToObjectString, 0, lengthFlags), propertyFlags),
  46. ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString, 0, lengthFlags), propertyFlags),
  47. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), propertyFlags),
  48. ["hasOwnProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwnProperty", HasOwnProperty, 1, lengthFlags), propertyFlags),
  49. ["isPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isPrototypeOf", IsPrototypeOf, 1, lengthFlags), propertyFlags),
  50. ["propertyIsEnumerable"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "propertyIsEnumerable", PropertyIsEnumerable, 1, lengthFlags), propertyFlags)
  51. };
  52. SetProperties(properties);
  53. }
  54. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  55. {
  56. TrackChanges(property);
  57. return base.DefineOwnProperty(property, desc);
  58. }
  59. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  60. {
  61. TrackChanges(property);
  62. base.SetOwnProperty(property, desc);
  63. }
  64. private void TrackChanges(JsValue property)
  65. {
  66. EnsureInitialized();
  67. if (ArrayInstance.IsArrayIndex(property, out _))
  68. {
  69. _objectChangeFlags |= ObjectChangeFlags.ArrayIndex;
  70. }
  71. else
  72. {
  73. _objectChangeFlags |= property.IsSymbol() ? ObjectChangeFlags.Symbol : ObjectChangeFlags.Property;
  74. }
  75. }
  76. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  77. {
  78. var p = TypeConverter.ToPropertyKey(arguments[0]);
  79. var o = TypeConverter.ToObject(_realm, thisObject);
  80. var desc = o.GetOwnProperty(p);
  81. if (desc == PropertyDescriptor.Undefined)
  82. {
  83. return JsBoolean.False;
  84. }
  85. return desc.Enumerable;
  86. }
  87. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  88. {
  89. var o = TypeConverter.ToObject(_realm, thisObject);
  90. return o;
  91. }
  92. private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
  93. {
  94. var arg = arguments[0];
  95. if (!arg.IsObject())
  96. {
  97. return JsBoolean.False;
  98. }
  99. var v = arg.AsObject();
  100. var o = TypeConverter.ToObject(_realm, thisObject);
  101. while (true)
  102. {
  103. v = v.Prototype;
  104. if (ReferenceEquals(v, null))
  105. {
  106. return JsBoolean.False;
  107. }
  108. if (ReferenceEquals(o, v))
  109. {
  110. return JsBoolean.True;
  111. }
  112. }
  113. }
  114. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  115. {
  116. var o = TypeConverter.ToObject(_realm, thisObject);
  117. var func = o.Get("toString");
  118. var callable = func as ICallable;
  119. if (callable is null)
  120. {
  121. ExceptionHelper.ThrowTypeError(_realm, "Can only invoke functions");
  122. }
  123. return TypeConverter.ToJsString(callable.Call(thisObject, arguments));
  124. }
  125. /// <summary>
  126. /// https://tc39.es/ecma262/#sec-object.prototype.tostring
  127. /// </summary>
  128. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  129. {
  130. if (thisObject.IsUndefined())
  131. {
  132. return "[object Undefined]";
  133. }
  134. if (thisObject.IsNull())
  135. {
  136. return "[object Null]";
  137. }
  138. var o = TypeConverter.ToObject(_realm, thisObject);
  139. var isArray = o.IsArray();
  140. var tag = o.Get(GlobalSymbolRegistry.ToStringTag);
  141. if (!tag.IsString())
  142. {
  143. if (isArray)
  144. {
  145. tag = "Array";
  146. }
  147. else if (o.IsCallable)
  148. {
  149. tag = "Function";
  150. }
  151. else
  152. {
  153. tag = (o is ProxyInstance ? ObjectClass.Object : o.Class).ToString();
  154. }
  155. }
  156. return "[object " + tag + "]";
  157. }
  158. /// <summary>
  159. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  160. /// </summary>
  161. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  162. {
  163. var p = TypeConverter.ToPropertyKey(arguments[0]);
  164. var o = TypeConverter.ToObject(_realm, thisObject);
  165. var desc = o.GetOwnProperty(p);
  166. return desc != PropertyDescriptor.Undefined;
  167. }
  168. }
  169. }