ObjectConstructor.cs 18 KB

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