ObjectConstructor.cs 19 KB

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