ObjectConstructor.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  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 : FunctionInstance, IConstructor
  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(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, 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. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
  137. private 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 ObjectInstance(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 ObjectInstance(_engine);
  160. }
  161. internal ObjectInstance Construct(int propertyCount)
  162. {
  163. var obj = new ObjectInstance(_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. var o = arguments.At(0) as ObjectInstance;
  279. if (o is null)
  280. {
  281. ExceptionHelper.ThrowTypeError(_realm, "Object.defineProperty called on non-object");
  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.UnwrapJsValue(propDesc);
  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 = SetIntegrityLevel(o, 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 = SetIntegrityLevel(o, IntegrityLevel.Frozen);
  355. if (!status)
  356. {
  357. ExceptionHelper.ThrowTypeError(_realm);
  358. }
  359. return o;
  360. }
  361. /// <summary>
  362. /// https://tc39.es/ecma262/#sec-setintegritylevel
  363. /// </summary>
  364. private static bool SetIntegrityLevel(ObjectInstance o, IntegrityLevel level)
  365. {
  366. var status = o.PreventExtensions();
  367. if (!status)
  368. {
  369. return false;
  370. }
  371. var keys = o.GetOwnPropertyKeys();
  372. if (level == IntegrityLevel.Sealed)
  373. {
  374. for (var i = 0; i < keys.Count; i++)
  375. {
  376. var k = keys[i];
  377. o.DefinePropertyOrThrow(k, new PropertyDescriptor { Configurable = false });
  378. }
  379. }
  380. else
  381. {
  382. for (var i = 0; i < keys.Count; i++)
  383. {
  384. var k = keys[i];
  385. var currentDesc = o.GetOwnProperty(k);
  386. if (currentDesc != PropertyDescriptor.Undefined)
  387. {
  388. PropertyDescriptor desc;
  389. if (currentDesc.IsAccessorDescriptor())
  390. {
  391. desc = new PropertyDescriptor { Configurable = false };
  392. }
  393. else
  394. {
  395. desc = new PropertyDescriptor { Configurable = false, Writable = false };
  396. }
  397. o.DefinePropertyOrThrow(k, desc);
  398. }
  399. }
  400. }
  401. return true;
  402. }
  403. private enum IntegrityLevel
  404. {
  405. Sealed,
  406. Frozen
  407. }
  408. /// <summary>
  409. /// https://tc39.es/ecma262/#sec-object.preventextensions
  410. /// </summary>
  411. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  412. {
  413. if (!(arguments.At(0) is ObjectInstance o))
  414. {
  415. return arguments.At(0);
  416. }
  417. if (!o.PreventExtensions())
  418. {
  419. ExceptionHelper.ThrowTypeError(_realm);
  420. }
  421. return o;
  422. }
  423. /// <summary>
  424. /// https://tc39.es/ecma262/#sec-object.issealed
  425. /// </summary>
  426. private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  427. {
  428. if (arguments.At(0) is not ObjectInstance o)
  429. {
  430. return true;
  431. }
  432. return TestIntegrityLevel(o, IntegrityLevel.Sealed);
  433. }
  434. /// <summary>
  435. /// https://tc39.es/ecma262/#sec-object.isfrozen
  436. /// </summary>
  437. private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  438. {
  439. if (arguments.At(0) is not ObjectInstance o)
  440. {
  441. return true;
  442. }
  443. return TestIntegrityLevel(o, IntegrityLevel.Frozen);
  444. }
  445. /// <summary>
  446. /// https://tc39.es/ecma262/#sec-testintegritylevel
  447. /// </summary>
  448. private static JsValue TestIntegrityLevel(ObjectInstance o, IntegrityLevel level)
  449. {
  450. if (o.Extensible)
  451. {
  452. return JsBoolean.False;
  453. }
  454. foreach (var k in o.GetOwnPropertyKeys())
  455. {
  456. var currentDesc = o.GetOwnProperty(k);
  457. if (currentDesc != PropertyDescriptor.Undefined)
  458. {
  459. if (currentDesc.Configurable)
  460. {
  461. return JsBoolean.False;
  462. }
  463. if (level == IntegrityLevel.Frozen && currentDesc.IsDataDescriptor())
  464. {
  465. if (currentDesc.Writable)
  466. {
  467. return JsBoolean.False;
  468. }
  469. }
  470. }
  471. }
  472. return JsBoolean.True;
  473. }
  474. /// <summary>
  475. /// https://tc39.es/ecma262/#sec-object.isextensible
  476. /// </summary>
  477. private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  478. {
  479. if (arguments.At(0) is not ObjectInstance o)
  480. {
  481. return false;
  482. }
  483. return o.Extensible;
  484. }
  485. /// <summary>
  486. /// https://tc39.es/ecma262/#sec-object.keys
  487. /// </summary>
  488. private JsValue Keys(JsValue thisObject, JsValue[] arguments)
  489. {
  490. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  491. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  492. }
  493. /// <summary>
  494. /// https://tc39.es/ecma262/#sec-object.values
  495. /// </summary>
  496. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  497. {
  498. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  499. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Value);
  500. }
  501. private sealed class CreateDataPropertyOnObject : ICallable
  502. {
  503. internal static readonly CreateDataPropertyOnObject Instance = new();
  504. private CreateDataPropertyOnObject()
  505. {
  506. }
  507. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  508. {
  509. var o = (ObjectInstance) thisObject;
  510. var key = arguments.At(0);
  511. var value = arguments.At(1);
  512. var propertyKey = TypeConverter.ToPropertyKey(key);
  513. o.CreateDataPropertyOrThrow(propertyKey, value);
  514. return Undefined;
  515. }
  516. }
  517. }
  518. }