ObjectConstructor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using System.Collections.Generic;
  2. using Jint.Collections;
  3. using Jint.Native.Array;
  4. using Jint.Native.Function;
  5. using Jint.Native.String;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Object
  10. {
  11. public sealed class ObjectConstructor : FunctionInstance, IConstructor
  12. {
  13. private ObjectConstructor(Engine engine)
  14. : base(engine, "Object", null, null, false)
  15. {
  16. }
  17. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  18. {
  19. var obj = new ObjectConstructor(engine);
  20. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  21. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
  22. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  23. return obj;
  24. }
  25. protected override void Initialize()
  26. {
  27. _prototype = Engine.Function.PrototypeObject;
  28. var properties = new PropertyDictionary(15, checkExistingKeys: false)
  29. {
  30. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), true, false, true),
  31. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2), true, false, true),
  32. ["getOwnPropertyNames"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), true, false, true),
  33. ["getOwnPropertySymbols"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertySymbols", GetOwnPropertySymbols, 1), true, false, true),
  34. ["create"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "create", Create, 2), true, false, true),
  35. ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3), true, false, true),
  36. ["defineProperties"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperties", DefineProperties, 2), true, false, true),
  37. ["seal"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "seal", Seal, 1), true, false, true),
  38. ["freeze"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "freeze", Freeze, 1), true, false, true),
  39. ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1), true, false, true),
  40. ["isSealed"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isSealed", IsSealed, 1), true, false, true),
  41. ["isFrozen"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFrozen", IsFrozen, 1), true, false, true),
  42. ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1), true, false, true),
  43. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 1), true, false, true),
  44. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2), true, false, true)
  45. };
  46. SetProperties(properties);
  47. }
  48. public ObjectPrototype PrototypeObject { get; private set; }
  49. /// <summary>
  50. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  51. /// </summary>
  52. /// <param name="thisObject"></param>
  53. /// <param name="arguments"></param>
  54. /// <returns></returns>
  55. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  56. {
  57. if (arguments.Length == 0)
  58. {
  59. return Construct(arguments);
  60. }
  61. if(arguments[0].IsNullOrUndefined())
  62. {
  63. return Construct(arguments);
  64. }
  65. return TypeConverter.ToObject(_engine, arguments[0]);
  66. }
  67. /// <summary>
  68. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  69. /// </summary>
  70. public ObjectInstance Construct(JsValue[] arguments)
  71. {
  72. return Construct(arguments, this);
  73. }
  74. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  75. {
  76. if (arguments.Length > 0)
  77. {
  78. var value = arguments[0];
  79. if (value is ObjectInstance oi)
  80. {
  81. return oi;
  82. }
  83. var type = value.Type;
  84. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  85. {
  86. return TypeConverter.ToObject(_engine, value);
  87. }
  88. }
  89. var obj = new ObjectInstance(_engine)
  90. {
  91. _prototype = Engine.Object.PrototypeObject
  92. };
  93. return obj;
  94. }
  95. internal ObjectInstance Construct(int propertyCount)
  96. {
  97. var obj = new ObjectInstance(_engine)
  98. {
  99. _prototype = Engine.Object.PrototypeObject,
  100. };
  101. obj.SetProperties(propertyCount > 0 ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
  102. return obj;
  103. }
  104. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  105. {
  106. var obj = TypeConverter.ToObject(_engine, arguments.At(0));
  107. return obj.Prototype ?? Null;
  108. }
  109. public JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  110. {
  111. var oArg = arguments.At(0);
  112. TypeConverter.CheckObjectCoercible(_engine, oArg);
  113. var prototype = arguments.At(1);
  114. if (!prototype.IsObject() && !prototype.IsNull())
  115. {
  116. ExceptionHelper.ThrowTypeError(_engine, $"Object prototype may only be an Object or null: {prototype}");
  117. }
  118. if (!(oArg is ObjectInstance o))
  119. {
  120. return oArg;
  121. }
  122. if (!o.SetPrototypeOf(prototype))
  123. {
  124. ExceptionHelper.ThrowTypeError(_engine);
  125. }
  126. return o;
  127. }
  128. public JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  129. {
  130. var o = arguments.As<ObjectInstance>(0, _engine);
  131. var p = arguments.At(1);
  132. var name = TypeConverter.ToPropertyKey(p);
  133. var desc = o.GetOwnProperty(name);
  134. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  135. }
  136. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  137. {
  138. var o = arguments.As<ObjectInstance>(0, _engine);
  139. uint n = 0;
  140. ArrayInstance array = null;
  141. var ownProperties = o.GetOwnPropertyKeys(Types.String);
  142. if (o is StringInstance s)
  143. {
  144. var length = s.PrimitiveValue.Length;
  145. array = Engine.Array.ConstructFast((uint) (ownProperties.Count + length + 1));
  146. for (var i = 0; i < length; i++)
  147. {
  148. array.SetIndexValue(n++, TypeConverter.ToString(i), updateLength: false);
  149. }
  150. array.SetIndexValue(n++, CommonProperties.Length, updateLength: false);
  151. }
  152. array = array ?? Engine.Array.ConstructFast((uint) ownProperties.Count);
  153. for (var i = 0; i < ownProperties.Count; i++)
  154. {
  155. var p = ownProperties[i];
  156. array.SetIndexValue(n++, p, false);
  157. }
  158. array.SetLength(n);
  159. return array;
  160. }
  161. public JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  162. {
  163. var o = arguments.As<ObjectInstance>(0, _engine);
  164. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  165. return _engine.Array.Construct(keys.ToArray());
  166. }
  167. public JsValue Create(JsValue thisObject, JsValue[] arguments)
  168. {
  169. var prototype = arguments.At(0);
  170. if (!prototype.IsObject() && !prototype.IsNull())
  171. {
  172. ExceptionHelper.ThrowTypeError(_engine, "Object prototype may only be an Object or null: " + prototype);
  173. }
  174. var obj = Engine.Object.Construct(Arguments.Empty);
  175. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  176. var properties = arguments.At(1);
  177. if (!properties.IsUndefined())
  178. {
  179. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  180. jsValues[0] = obj;
  181. jsValues[1] = properties;
  182. DefineProperties(thisObject, jsValues);
  183. _engine._jsValueArrayPool.ReturnArray(jsValues);
  184. }
  185. return obj;
  186. }
  187. public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  188. {
  189. var o = arguments.As<ObjectInstance>(0, _engine);
  190. var p = arguments.At(1);
  191. var name = TypeConverter.ToPropertyKey(p);
  192. var attributes = arguments.At(2);
  193. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  194. o.DefinePropertyOrThrow(name, desc);
  195. return o;
  196. }
  197. public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  198. {
  199. var o = arguments.As<ObjectInstance>(0, _engine);
  200. var properties = arguments.At(1);
  201. var props = TypeConverter.ToObject(Engine, properties);
  202. var descriptors = new List<KeyValuePair<JsValue, PropertyDescriptor>>();
  203. foreach (var p in props.GetOwnProperties())
  204. {
  205. if (!p.Value.Enumerable)
  206. {
  207. continue;
  208. }
  209. var descObj = props.Get(p.Key, props);
  210. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  211. descriptors.Add(new KeyValuePair<JsValue, PropertyDescriptor>(p.Key, desc));
  212. }
  213. foreach (var pair in descriptors)
  214. {
  215. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  216. }
  217. return o;
  218. }
  219. public JsValue Seal(JsValue thisObject, JsValue[] arguments)
  220. {
  221. var o = arguments.As<ObjectInstance>(0, _engine);
  222. var properties = new List<KeyValuePair<JsValue, PropertyDescriptor>>(o.GetOwnProperties());
  223. foreach (var prop in properties)
  224. {
  225. var propertyDescriptor = prop.Value;
  226. if (propertyDescriptor.Configurable)
  227. {
  228. propertyDescriptor.Configurable = false;
  229. FastSetProperty(prop.Key, propertyDescriptor);
  230. }
  231. o.DefinePropertyOrThrow(prop.Key, propertyDescriptor);
  232. }
  233. o.PreventExtensions();
  234. return o;
  235. }
  236. public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  237. {
  238. var o = arguments.As<ObjectInstance>(0, _engine);
  239. var properties = new List<KeyValuePair<JsValue, PropertyDescriptor>>(o.GetOwnProperties());
  240. foreach (var p in properties)
  241. {
  242. var desc = o.GetOwnProperty(p.Key);
  243. if (desc.IsDataDescriptor())
  244. {
  245. if (desc.Writable)
  246. {
  247. var mutable = desc;
  248. mutable.Writable = false;
  249. desc = mutable;
  250. }
  251. }
  252. if (desc.Configurable)
  253. {
  254. var mutable = desc;
  255. mutable.Configurable = false;
  256. desc = mutable;
  257. }
  258. o.DefinePropertyOrThrow(p.Key, desc);
  259. }
  260. o.PreventExtensions();
  261. return o;
  262. }
  263. public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  264. {
  265. var o = arguments.As<ObjectInstance>(0, _engine);
  266. o.PreventExtensions();
  267. return o;
  268. }
  269. public JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  270. {
  271. var o = arguments.As<ObjectInstance>(0, _engine);
  272. foreach (var prop in o.GetOwnProperties())
  273. {
  274. if (prop.Value.Configurable)
  275. {
  276. return false;
  277. }
  278. }
  279. if (o.Extensible == false)
  280. {
  281. return true;
  282. }
  283. return false;
  284. }
  285. public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  286. {
  287. var o = arguments.As<ObjectInstance>(0, _engine);
  288. foreach (var pair in o.GetOwnProperties())
  289. {
  290. var desc = pair.Value;
  291. if (desc.IsDataDescriptor())
  292. {
  293. if (desc.Writable)
  294. {
  295. return false;
  296. }
  297. }
  298. if (desc.Configurable)
  299. {
  300. return false;
  301. }
  302. }
  303. if (o.Extensible == false)
  304. {
  305. return true;
  306. }
  307. return false;
  308. }
  309. public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  310. {
  311. var o = arguments.As<ObjectInstance>(0, _engine);
  312. return o.Extensible;
  313. }
  314. public JsValue Keys(JsValue thisObject, JsValue[] arguments)
  315. {
  316. return EnumerableOwnPropertyNames(arguments, EnumerableOwnPropertyNamesKind.Key);
  317. }
  318. private JsValue EnumerableOwnPropertyNames(JsValue[] arguments, EnumerableOwnPropertyNamesKind kind)
  319. {
  320. var o = arguments.As<ObjectInstance>(0, _engine);
  321. var ownKeys = o.GetOwnPropertyKeys(Types.String);
  322. var array = Engine.Array.ConstructFast((uint) ownKeys.Count);
  323. uint index = 0;
  324. for (var i = 0; i < ownKeys.Count; i++)
  325. {
  326. var property = ownKeys[i];
  327. var desc = o.GetOwnProperty(property);
  328. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  329. {
  330. if (kind == EnumerableOwnPropertyNamesKind.Key)
  331. {
  332. array.SetIndexValue(index, property, updateLength: false);
  333. }
  334. else
  335. {
  336. var value = o.Get(property, o);
  337. if (kind == EnumerableOwnPropertyNamesKind.Value)
  338. {
  339. array.SetIndexValue(index, value, updateLength: false);
  340. }
  341. else
  342. {
  343. array.SetIndexValue(index, _engine.Array.Construct(new[]
  344. {
  345. property,
  346. value
  347. }), updateLength: false);
  348. }
  349. }
  350. index++;
  351. }
  352. }
  353. array.SetLength(index);
  354. return array;
  355. }
  356. private enum EnumerableOwnPropertyNamesKind
  357. {
  358. Key,
  359. Value,
  360. KeyValue
  361. }
  362. }
  363. }