ObjectConstructor.cs 21 KB

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