ObjectConstructor.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. using Jint.Collections;
  2. using Jint.Native.Iterator;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Object
  7. {
  8. public sealed class ObjectConstructor : Constructor
  9. {
  10. private static readonly JsString _name = new JsString("Object");
  11. internal ObjectConstructor(
  12. Engine engine,
  13. Realm realm)
  14. : base(engine, realm, _name)
  15. {
  16. PrototypeObject = new ObjectPrototype(engine, realm, this);
  17. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
  18. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  19. }
  20. public ObjectPrototype PrototypeObject { get; }
  21. protected override void Initialize()
  22. {
  23. _prototype = _realm.Intrinsics.Function.PrototypeObject;
  24. const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  25. const PropertyFlag LengthFlags = PropertyFlag.Configurable;
  26. var properties = new PropertyDictionary(16, checkExistingKeys: false)
  27. {
  28. ["assign"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "assign", Assign, 2, LengthFlags), PropertyFlags),
  29. ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Entries, 1, LengthFlags), PropertyFlags),
  30. ["fromEntries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromEntries", FromEntries, 1, LengthFlags), PropertyFlags),
  31. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), PropertyFlags),
  32. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2, LengthFlags), PropertyFlags),
  33. ["getOwnPropertyDescriptors"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptors", GetOwnPropertyDescriptors, 1, LengthFlags), PropertyFlags),
  34. ["getOwnPropertyNames"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), PropertyFlags),
  35. ["getOwnPropertySymbols"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertySymbols", GetOwnPropertySymbols, 1, LengthFlags), PropertyFlags),
  36. ["groupBy"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "groupBy", GroupBy, 2, PropertyFlag.Configurable), 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, LengthFlags), 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. ["hasOwn"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwn", HasOwn, 2, LengthFlags), PropertyFlags),
  51. };
  52. SetProperties(properties);
  53. }
  54. /// <summary>
  55. /// https://tc39.es/ecma262/#sec-object.assign
  56. /// </summary>
  57. private JsValue Assign(JsValue thisObject, JsValue[] arguments)
  58. {
  59. var to = TypeConverter.ToObject(_realm, arguments.At(0));
  60. if (arguments.Length < 2)
  61. {
  62. return to;
  63. }
  64. for (var i = 1; i < arguments.Length; i++)
  65. {
  66. var nextSource = arguments[i];
  67. if (nextSource.IsNullOrUndefined())
  68. {
  69. continue;
  70. }
  71. var from = TypeConverter.ToObject(_realm, nextSource);
  72. var keys = from.GetOwnPropertyKeys();
  73. foreach (var nextKey in keys)
  74. {
  75. var desc = from.GetOwnProperty(nextKey);
  76. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  77. {
  78. var propValue = from.Get(nextKey);
  79. to.Set(nextKey, propValue, throwOnError: true);
  80. }
  81. }
  82. }
  83. return to;
  84. }
  85. /// <summary>
  86. /// https://tc39.es/ecma262/#sec-object.entries
  87. /// </summary>
  88. private JsValue Entries(JsValue thisObject, JsValue[] arguments)
  89. {
  90. var obj = TypeConverter.ToObject(_realm, arguments.At(0));
  91. var nameList = obj.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.KeyValue);
  92. return nameList;
  93. }
  94. /// <summary>
  95. /// https://tc39.es/ecma262/#sec-object.fromentries
  96. /// </summary>
  97. private JsValue FromEntries(JsValue thisObject, JsValue[] arguments)
  98. {
  99. var iterable = arguments.At(0);
  100. TypeConverter.CheckObjectCoercible(_engine, iterable);
  101. var obj = _realm.Intrinsics.Object.Construct(0);
  102. var adder = CreateDataPropertyOnObject.Instance;
  103. var iterator = arguments.At(0).GetIterator(_realm);
  104. IteratorProtocol.AddEntriesFromIterable(obj, iterator, adder);
  105. return obj;
  106. }
  107. /// <summary>
  108. /// https://tc39.es/ecma262/#sec-object.is
  109. /// </summary>
  110. private static JsValue Is(JsValue thisObject, JsValue[] arguments)
  111. {
  112. return SameValue(arguments.At(0), arguments.At(1));
  113. }
  114. /// <summary>
  115. /// https://tc39.es/ecma262/#sec-object-value
  116. /// </summary>
  117. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  118. {
  119. if (arguments.Length == 0)
  120. {
  121. return Construct(arguments);
  122. }
  123. if(arguments[0].IsNullOrUndefined())
  124. {
  125. return Construct(arguments);
  126. }
  127. return TypeConverter.ToObject(_realm, arguments[0]);
  128. }
  129. /// <summary>
  130. /// https://tc39.es/ecma262/#sec-object-value
  131. /// </summary>
  132. public ObjectInstance Construct(JsValue[] arguments)
  133. {
  134. return Construct(arguments, this);
  135. }
  136. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  137. {
  138. if (!ReferenceEquals(this, newTarget) && !newTarget.IsUndefined())
  139. {
  140. return OrdinaryCreateFromConstructor(
  141. newTarget,
  142. static intrinsics => intrinsics.Object.PrototypeObject,
  143. static (Engine engine, Realm _, object? _) => new JsObject(engine));
  144. }
  145. if (arguments.Length > 0)
  146. {
  147. var value = arguments[0];
  148. if (value is ObjectInstance oi)
  149. {
  150. return oi;
  151. }
  152. var type = value.Type;
  153. if (type is Types.String or Types.Number or Types.Boolean)
  154. {
  155. return TypeConverter.ToObject(_realm, value);
  156. }
  157. }
  158. return new JsObject(_engine);
  159. }
  160. internal ObjectInstance Construct(int propertyCount)
  161. {
  162. var obj = new JsObject(_engine);
  163. obj.SetProperties(propertyCount > 0 ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
  164. return obj;
  165. }
  166. /// <summary>
  167. /// https://tc39.es/ecma262/#sec-object.getprototypeof
  168. /// </summary>
  169. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  170. {
  171. var obj = TypeConverter.ToObject(_realm, arguments.At(0));
  172. return obj.Prototype ?? Null;
  173. }
  174. /// <summary>
  175. /// https://tc39.es/ecma262/#sec-object.setprototypeof
  176. /// </summary>
  177. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  178. {
  179. var oArg = arguments.At(0);
  180. TypeConverter.CheckObjectCoercible(_engine, oArg);
  181. var prototype = arguments.At(1);
  182. if (!prototype.IsObject() && !prototype.IsNull())
  183. {
  184. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  185. }
  186. if (!(oArg is ObjectInstance o))
  187. {
  188. return oArg;
  189. }
  190. if (!o.SetPrototypeOf(prototype))
  191. {
  192. ExceptionHelper.ThrowTypeError(_realm);
  193. }
  194. return o;
  195. }
  196. /// <summary>
  197. /// https://tc39.es/ecma262/#sec-object.hasown
  198. /// </summary>
  199. private JsValue HasOwn(JsValue thisObject, JsValue[] arguments)
  200. {
  201. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  202. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  203. return o.HasOwnProperty(property) ? JsBoolean.True : JsBoolean.False;
  204. }
  205. /// <summary>
  206. /// https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
  207. /// </summary>
  208. internal JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  209. {
  210. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  211. var p = arguments.At(1);
  212. var name = TypeConverter.ToPropertyKey(p);
  213. var desc = o.GetOwnProperty(name);
  214. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  215. }
  216. /// <summary>
  217. /// https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
  218. /// </summary>
  219. private JsValue GetOwnPropertyDescriptors(JsValue thisObject, JsValue[] arguments)
  220. {
  221. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  222. var ownKeys = o.GetOwnPropertyKeys();
  223. var descriptors = _realm.Intrinsics.Object.Construct(0);
  224. foreach (var key in ownKeys)
  225. {
  226. var desc = o.GetOwnProperty(key);
  227. var descriptor = PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  228. if (!ReferenceEquals(descriptor, Undefined))
  229. {
  230. descriptors.CreateDataProperty(key, descriptor);
  231. }
  232. }
  233. return descriptors;
  234. }
  235. /// <summary>
  236. /// https://tc39.es/ecma262/#sec-object.getownpropertynames
  237. /// </summary>
  238. private JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  239. {
  240. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  241. var names = o.GetOwnPropertyKeys(Types.String);
  242. return _realm.Intrinsics.Array.ConstructFast(names);
  243. }
  244. /// <summary>
  245. /// https://tc39.es/ecma262/#sec-object.getownpropertysymbols
  246. /// </summary>
  247. private JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  248. {
  249. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  250. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  251. return _realm.Intrinsics.Array.ConstructFast(keys);
  252. }
  253. /// <summary>
  254. /// https://tc39.es/ecma262/#sec-object.create
  255. /// </summary>
  256. private JsValue Create(JsValue thisObject, JsValue[] arguments)
  257. {
  258. var prototype = arguments.At(0);
  259. if (!prototype.IsObject() && !prototype.IsNull())
  260. {
  261. ExceptionHelper.ThrowTypeError(_realm, "Object prototype may only be an Object or null: " + prototype);
  262. }
  263. var obj = Engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  264. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  265. var properties = arguments.At(1);
  266. if (!properties.IsUndefined())
  267. {
  268. ObjectDefineProperties(obj, properties);
  269. }
  270. return obj;
  271. }
  272. /// <summary>
  273. /// https://tc39.es/ecma262/#sec-object.defineproperty
  274. /// </summary>
  275. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  276. {
  277. if (arguments.At(0) is not ObjectInstance o)
  278. {
  279. ExceptionHelper.ThrowTypeError(_realm, "Object.defineProperty called on non-object");
  280. return null;
  281. }
  282. var p = arguments.At(1);
  283. var name = TypeConverter.ToPropertyKey(p);
  284. var attributes = arguments.At(2);
  285. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  286. o.DefinePropertyOrThrow(name, desc);
  287. return arguments.At(0);
  288. }
  289. /// <summary>
  290. /// https://tc39.es/ecma262/#sec-object.defineproperties
  291. /// </summary>
  292. private JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  293. {
  294. var o = arguments.At(0) as ObjectInstance;
  295. if (o is null)
  296. {
  297. ExceptionHelper.ThrowTypeError(_realm, "Object.defineProperty called on non-object");
  298. }
  299. var properties = arguments.At(1);
  300. return ObjectDefineProperties(o, properties);
  301. }
  302. /// <summary>
  303. /// https://tc39.es/ecma262/#sec-objectdefineproperties
  304. /// </summary>
  305. private JsValue ObjectDefineProperties(ObjectInstance o, JsValue properties)
  306. {
  307. var props = TypeConverter.ToObject(_realm, properties);
  308. var keys = props.GetOwnPropertyKeys();
  309. var descriptors = new List<KeyValuePair<JsValue, PropertyDescriptor>>();
  310. for (var i = 0; i < keys.Count; i++)
  311. {
  312. var nextKey = keys[i];
  313. var propDesc = props.GetOwnProperty(nextKey);
  314. if (propDesc == PropertyDescriptor.Undefined || !propDesc.Enumerable)
  315. {
  316. continue;
  317. }
  318. var descObj = props.Get(nextKey);
  319. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, descObj);
  320. descriptors.Add(new KeyValuePair<JsValue, PropertyDescriptor>(nextKey, desc));
  321. }
  322. foreach (var pair in descriptors)
  323. {
  324. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  325. }
  326. return o;
  327. }
  328. /// <summary>
  329. /// https://tc39.es/ecma262/#sec-object.seal
  330. /// </summary>
  331. private JsValue Seal(JsValue thisObject, JsValue[] arguments)
  332. {
  333. if (arguments.At(0) is not ObjectInstance o)
  334. {
  335. return arguments.At(0);
  336. }
  337. var status = o.SetIntegrityLevel(IntegrityLevel.Sealed);
  338. if (!status)
  339. {
  340. ExceptionHelper.ThrowTypeError(_realm);
  341. }
  342. return o;
  343. }
  344. /// <summary>
  345. /// https://tc39.es/ecma262/#sec-object.freeze
  346. /// </summary>
  347. private JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  348. {
  349. if (arguments.At(0) is not ObjectInstance o)
  350. {
  351. return arguments.At(0);
  352. }
  353. var status = o.SetIntegrityLevel(IntegrityLevel.Frozen);
  354. if (!status)
  355. {
  356. ExceptionHelper.ThrowTypeError(_realm);
  357. }
  358. return o;
  359. }
  360. /// <summary>
  361. /// https://tc39.es/ecma262/#sec-object.preventextensions
  362. /// </summary>
  363. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  364. {
  365. if (arguments.At(0) is not ObjectInstance o)
  366. {
  367. return arguments.At(0);
  368. }
  369. if (!o.PreventExtensions())
  370. {
  371. ExceptionHelper.ThrowTypeError(_realm);
  372. }
  373. return o;
  374. }
  375. /// <summary>
  376. /// https://tc39.es/ecma262/#sec-object.issealed
  377. /// </summary>
  378. private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  379. {
  380. if (arguments.At(0) is not ObjectInstance o)
  381. {
  382. return JsBoolean.True;
  383. }
  384. return TestIntegrityLevel(o, IntegrityLevel.Sealed);
  385. }
  386. /// <summary>
  387. /// https://tc39.es/ecma262/#sec-object.isfrozen
  388. /// </summary>
  389. private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  390. {
  391. if (arguments.At(0) is not ObjectInstance o)
  392. {
  393. return JsBoolean.True;
  394. }
  395. return TestIntegrityLevel(o, IntegrityLevel.Frozen);
  396. }
  397. /// <summary>
  398. /// https://tc39.es/ecma262/#sec-testintegritylevel
  399. /// </summary>
  400. private static JsValue TestIntegrityLevel(ObjectInstance o, IntegrityLevel level)
  401. {
  402. if (o.Extensible)
  403. {
  404. return JsBoolean.False;
  405. }
  406. foreach (var k in o.GetOwnPropertyKeys())
  407. {
  408. var currentDesc = o.GetOwnProperty(k);
  409. if (currentDesc != PropertyDescriptor.Undefined)
  410. {
  411. if (currentDesc.Configurable)
  412. {
  413. return JsBoolean.False;
  414. }
  415. if (level == IntegrityLevel.Frozen && currentDesc.IsDataDescriptor())
  416. {
  417. if (currentDesc.Writable)
  418. {
  419. return JsBoolean.False;
  420. }
  421. }
  422. }
  423. }
  424. return JsBoolean.True;
  425. }
  426. /// <summary>
  427. /// https://tc39.es/ecma262/#sec-object.isextensible
  428. /// </summary>
  429. private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  430. {
  431. if (arguments.At(0) is not ObjectInstance o)
  432. {
  433. return JsBoolean.False;
  434. }
  435. return o.Extensible;
  436. }
  437. /// <summary>
  438. /// https://tc39.es/ecma262/#sec-object.keys
  439. /// </summary>
  440. private JsValue Keys(JsValue thisObject, JsValue[] arguments)
  441. {
  442. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  443. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  444. }
  445. /// <summary>
  446. /// https://tc39.es/ecma262/#sec-object.values
  447. /// </summary>
  448. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  449. {
  450. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  451. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Value);
  452. }
  453. /// <summary>
  454. /// https://tc39.es/proposal-array-grouping/#sec-object.groupby
  455. /// </summary>
  456. private JsValue GroupBy(JsValue thisObject, JsValue[] arguments)
  457. {
  458. var items = arguments.At(0);
  459. var callbackfn = arguments.At(1);
  460. var grouping = GroupByHelper.GroupBy(_engine, _realm, items, callbackfn, mapMode: false);
  461. var obj = OrdinaryObjectCreate(_engine, null);
  462. foreach (var pair in grouping)
  463. {
  464. obj.FastSetProperty(pair.Key, new PropertyDescriptor(pair.Value, PropertyFlag.ConfigurableEnumerableWritable));
  465. }
  466. return obj;
  467. }
  468. private sealed class CreateDataPropertyOnObject : ICallable
  469. {
  470. internal static readonly CreateDataPropertyOnObject Instance = new();
  471. private CreateDataPropertyOnObject()
  472. {
  473. }
  474. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  475. {
  476. var o = (ObjectInstance) thisObject;
  477. var key = arguments.At(0);
  478. var value = arguments.At(1);
  479. var propertyKey = TypeConverter.ToPropertyKey(key);
  480. o.CreateDataPropertyOrThrow(propertyKey, value);
  481. return Undefined;
  482. }
  483. }
  484. }
  485. }