ObjectConstructor.cs 18 KB

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