Object.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <LibJS/Interpreter.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Accessor.h>
  11. #include <LibJS/Runtime/Array.h>
  12. #include <LibJS/Runtime/Error.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/NativeFunction.h>
  15. #include <LibJS/Runtime/Object.h>
  16. #include <LibJS/Runtime/PropertyDescriptor.h>
  17. #include <LibJS/Runtime/ProxyObject.h>
  18. #include <LibJS/Runtime/Shape.h>
  19. #include <LibJS/Runtime/TemporaryClearException.h>
  20. #include <LibJS/Runtime/Value.h>
  21. namespace JS {
  22. // 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  23. Object* Object::create(GlobalObject& global_object, Object* prototype)
  24. {
  25. if (!prototype)
  26. return global_object.heap().allocate<Object>(global_object, *global_object.empty_object_shape());
  27. else if (prototype == global_object.object_prototype())
  28. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  29. else
  30. return global_object.heap().allocate<Object>(global_object, *prototype);
  31. }
  32. Object::Object(GlobalObjectTag)
  33. {
  34. // This is the global object
  35. m_shape = heap().allocate_without_global_object<Shape>(*this);
  36. }
  37. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  38. {
  39. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  40. }
  41. Object::Object(Object& prototype)
  42. {
  43. m_shape = prototype.global_object().empty_object_shape();
  44. auto success = internal_set_prototype_of(&prototype);
  45. VERIFY(success);
  46. }
  47. Object::Object(Shape& shape)
  48. : m_shape(&shape)
  49. {
  50. m_storage.resize(shape.property_count());
  51. }
  52. void Object::initialize(GlobalObject&)
  53. {
  54. }
  55. Object::~Object()
  56. {
  57. }
  58. // 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations
  59. // 7.2.5 IsExtensible ( O ), https://tc39.es/ecma262/#sec-isextensible-o
  60. bool Object::is_extensible() const
  61. {
  62. return internal_is_extensible();
  63. }
  64. // 7.3 Operations on Objects, https://tc39.es/ecma262/#sec-operations-on-objects
  65. // 7.3.2 Get ( O, P ), https://tc39.es/ecma262/#sec-get-o-p
  66. Value Object::get(PropertyName const& property_name) const
  67. {
  68. // 1. Assert: Type(O) is Object.
  69. // 2. Assert: IsPropertyKey(P) is true.
  70. VERIFY(property_name.is_valid());
  71. // 3. Return ? O.[[Get]](P, O).
  72. return internal_get(property_name, this);
  73. }
  74. // 7.3.3 GetV ( V, P ) is defined as Value::get().
  75. // 7.3.4 Set ( O, P, V, Throw ), https://tc39.es/ecma262/#sec-set-o-p-v-throw
  76. bool Object::set(PropertyName const& property_name, Value value, ShouldThrowExceptions throw_exceptions)
  77. {
  78. VERIFY(!value.is_empty());
  79. auto& vm = this->vm();
  80. // 1. Assert: Type(O) is Object.
  81. // 2. Assert: IsPropertyKey(P) is true.
  82. VERIFY(property_name.is_valid());
  83. // 3. Assert: Type(Throw) is Boolean.
  84. // 4. Let success be ? O.[[Set]](P, V, O).
  85. auto success = internal_set(property_name, value, this);
  86. if (vm.exception())
  87. return {};
  88. // 5. If success is false and Throw is true, throw a TypeError exception.
  89. if (!success && throw_exceptions == ShouldThrowExceptions::Yes) {
  90. // FIXME: Improve/contextualize error message
  91. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectSetReturnedFalse);
  92. return {};
  93. }
  94. // 6. Return success.
  95. return success;
  96. }
  97. // 7.3.5 CreateDataProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createdataproperty
  98. bool Object::create_data_property(PropertyName const& property_name, Value value)
  99. {
  100. // 1. Assert: Type(O) is Object.
  101. // 2. Assert: IsPropertyKey(P) is true.
  102. VERIFY(property_name.is_valid());
  103. // 3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  104. auto new_descriptor = PropertyDescriptor {
  105. .value = value,
  106. .writable = true,
  107. .enumerable = true,
  108. .configurable = true,
  109. };
  110. // 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
  111. return internal_define_own_property(property_name, new_descriptor);
  112. }
  113. // 7.3.6 CreateMethodProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createmethodproperty
  114. bool Object::create_method_property(PropertyName const& property_name, Value value)
  115. {
  116. VERIFY(!value.is_empty());
  117. // 1. Assert: Type(O) is Object.
  118. // 2. Assert: IsPropertyKey(P) is true.
  119. VERIFY(property_name.is_valid());
  120. // 3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
  121. auto new_descriptor = PropertyDescriptor {
  122. .value = value,
  123. .writable = true,
  124. .enumerable = false,
  125. .configurable = true,
  126. };
  127. // 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
  128. return internal_define_own_property(property_name, new_descriptor);
  129. }
  130. // 7.3.7 CreateDataPropertyOrThrow ( O, P, V ), https://tc39.es/ecma262/#sec-createdatapropertyorthrow
  131. bool Object::create_data_property_or_throw(PropertyName const& property_name, Value value)
  132. {
  133. VERIFY(!value.is_empty());
  134. auto& vm = this->vm();
  135. // 1. Assert: Type(O) is Object.
  136. // 2. Assert: IsPropertyKey(P) is true.
  137. VERIFY(property_name.is_valid());
  138. // 3. Let success be ? CreateDataProperty(O, P, V).
  139. auto success = create_data_property(property_name, value);
  140. if (vm.exception())
  141. return {};
  142. // 4. If success is false, throw a TypeError exception.
  143. if (!success) {
  144. // FIXME: Improve/contextualize error message
  145. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDefineOwnPropertyReturnedFalse);
  146. return {};
  147. }
  148. // 5. Return success.
  149. return success;
  150. }
  151. // 7.3.6 CreateNonEnumerableDataPropertyOrThrow ( O, P, V ), https://tc39.es/proposal-error-cause/#sec-createnonenumerabledatapropertyorthrow
  152. bool Object::create_non_enumerable_data_property_or_throw(PropertyName const& property_name, Value value)
  153. {
  154. VERIFY(!value.is_empty());
  155. VERIFY(property_name.is_valid());
  156. // 1. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
  157. auto new_description = PropertyDescriptor { .value = value, .writable = true, .enumerable = false, .configurable = true };
  158. // 2. Return ? DefinePropertyOrThrow(O, P, newDesc).
  159. return define_property_or_throw(property_name, new_description);
  160. }
  161. // 7.3.8 DefinePropertyOrThrow ( O, P, desc ), https://tc39.es/ecma262/#sec-definepropertyorthrow
  162. bool Object::define_property_or_throw(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  163. {
  164. auto& vm = this->vm();
  165. // 1. Assert: Type(O) is Object.
  166. // 2. Assert: IsPropertyKey(P) is true.
  167. VERIFY(property_name.is_valid());
  168. // 3. Let success be ? O.[[DefineOwnProperty]](P, desc).
  169. auto success = internal_define_own_property(property_name, property_descriptor);
  170. if (vm.exception())
  171. return {};
  172. // 4. If success is false, throw a TypeError exception.
  173. if (!success) {
  174. // FIXME: Improve/contextualize error message
  175. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDefineOwnPropertyReturnedFalse);
  176. return {};
  177. }
  178. // 5. Return success.
  179. return success;
  180. }
  181. // 7.3.9 DeletePropertyOrThrow ( O, P ), https://tc39.es/ecma262/#sec-deletepropertyorthrow
  182. bool Object::delete_property_or_throw(PropertyName const& property_name)
  183. {
  184. auto& vm = this->vm();
  185. // 1. Assert: Type(O) is Object.
  186. // 2. Assert: IsPropertyKey(P) is true.
  187. VERIFY(property_name.is_valid());
  188. // 3. Let success be ? O.[[Delete]](P).
  189. auto success = internal_delete(property_name);
  190. if (vm.exception())
  191. return {};
  192. // 4. If success is false, throw a TypeError exception.
  193. if (!success) {
  194. // FIXME: Improve/contextualize error message
  195. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse);
  196. return {};
  197. }
  198. // 5. Return success.
  199. return success;
  200. }
  201. // 7.3.11 HasProperty ( O, P ), https://tc39.es/ecma262/#sec-hasproperty
  202. bool Object::has_property(PropertyName const& property_name) const
  203. {
  204. // 1. Assert: Type(O) is Object.
  205. // 2. Assert: IsPropertyKey(P) is true.
  206. VERIFY(property_name.is_valid());
  207. // 3. Return ? O.[[HasProperty]](P).
  208. return internal_has_property(property_name);
  209. }
  210. // 7.3.12 HasOwnProperty ( O, P ), https://tc39.es/ecma262/#sec-hasownproperty
  211. bool Object::has_own_property(PropertyName const& property_name) const
  212. {
  213. auto& vm = this->vm();
  214. // 1. Assert: Type(O) is Object.
  215. // 2. Assert: IsPropertyKey(P) is true.
  216. VERIFY(property_name.is_valid());
  217. // 3. Let desc be ? O.[[GetOwnProperty]](P).
  218. auto descriptor = internal_get_own_property(property_name);
  219. if (vm.exception())
  220. return {};
  221. // 4. If desc is undefined, return false.
  222. if (!descriptor.has_value())
  223. return false;
  224. // 5. Return true.
  225. return true;
  226. }
  227. // 7.3.15 SetIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-setintegritylevel
  228. bool Object::set_integrity_level(IntegrityLevel level)
  229. {
  230. auto& vm = this->vm();
  231. auto& global_object = this->global_object();
  232. // 1. Assert: Type(O) is Object.
  233. // 2. Assert: level is either sealed or frozen.
  234. VERIFY(level == IntegrityLevel::Sealed || level == IntegrityLevel::Frozen);
  235. // 3. Let status be ? O.[[PreventExtensions]]().
  236. auto status = internal_prevent_extensions();
  237. if (vm.exception())
  238. return {};
  239. // 4. If status is false, return false.
  240. if (!status)
  241. return false;
  242. // 5. Let keys be ? O.[[OwnPropertyKeys]]().
  243. auto keys = internal_own_property_keys();
  244. if (vm.exception())
  245. return {};
  246. // 6. If level is sealed, then
  247. if (level == IntegrityLevel::Sealed) {
  248. // a. For each element k of keys, do
  249. for (auto& key : keys) {
  250. auto property_name = PropertyName::from_value(global_object, key);
  251. // i. Perform ? DefinePropertyOrThrow(O, k, PropertyDescriptor { [[Configurable]]: false }).
  252. define_property_or_throw(property_name, { .configurable = false });
  253. if (vm.exception())
  254. return {};
  255. }
  256. }
  257. // 7. Else,
  258. else {
  259. // a. Assert: level is frozen.
  260. // b. For each element k of keys, do
  261. for (auto& key : keys) {
  262. auto property_name = PropertyName::from_value(global_object, key);
  263. // i. Let currentDesc be ? O.[[GetOwnProperty]](k).
  264. auto current_descriptor = internal_get_own_property(property_name);
  265. if (vm.exception())
  266. return {};
  267. // ii. If currentDesc is not undefined, then
  268. if (!current_descriptor.has_value())
  269. continue;
  270. PropertyDescriptor descriptor;
  271. // 1. If IsAccessorDescriptor(currentDesc) is true, then
  272. if (current_descriptor->is_accessor_descriptor()) {
  273. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false }.
  274. descriptor = { .configurable = false };
  275. }
  276. // 2. Else,
  277. else {
  278. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false, [[Writable]]: false }.
  279. descriptor = { .writable = false, .configurable = false };
  280. }
  281. // 3. Perform ? DefinePropertyOrThrow(O, k, desc).
  282. define_property_or_throw(property_name, descriptor);
  283. if (vm.exception())
  284. return {};
  285. }
  286. }
  287. // 8. Return true.
  288. return true;
  289. }
  290. // 7.3.16 TestIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-testintegritylevel
  291. bool Object::test_integrity_level(IntegrityLevel level) const
  292. {
  293. auto& vm = this->vm();
  294. // 1. Assert: Type(O) is Object.
  295. // 2. Assert: level is either sealed or frozen.
  296. VERIFY(level == IntegrityLevel::Sealed || level == IntegrityLevel::Frozen);
  297. // 3. Let extensible be ? IsExtensible(O).
  298. auto extensible = is_extensible();
  299. if (vm.exception())
  300. return {};
  301. // 4. If extensible is true, return false.
  302. // 5. NOTE: If the object is extensible, none of its properties are examined.
  303. if (extensible)
  304. return false;
  305. // 6. Let keys be ? O.[[OwnPropertyKeys]]().
  306. auto keys = internal_own_property_keys();
  307. if (vm.exception())
  308. return {};
  309. // 7. For each element k of keys, do
  310. for (auto& key : keys) {
  311. auto property_name = PropertyName::from_value(global_object(), key);
  312. // a. Let currentDesc be ? O.[[GetOwnProperty]](k).
  313. auto current_descriptor = internal_get_own_property(property_name);
  314. if (vm.exception())
  315. return {};
  316. // b. If currentDesc is not undefined, then
  317. if (!current_descriptor.has_value())
  318. continue;
  319. // i. If currentDesc.[[Configurable]] is true, return false.
  320. if (*current_descriptor->configurable)
  321. return false;
  322. // ii. If level is frozen and IsDataDescriptor(currentDesc) is true, then
  323. if (level == IntegrityLevel::Frozen && current_descriptor->is_data_descriptor()) {
  324. // 1. If currentDesc.[[Writable]] is true, return false.
  325. if (*current_descriptor->writable)
  326. return false;
  327. }
  328. }
  329. // 8. Return true.
  330. return true;
  331. }
  332. // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
  333. MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
  334. {
  335. // NOTE: This has been flattened for readability, so some `else` branches in the
  336. // spec text have been replaced with `continue`s in the loop below.
  337. auto& vm = this->vm();
  338. auto& global_object = this->global_object();
  339. // 1. Assert: Type(O) is Object.
  340. // 2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
  341. auto own_keys = internal_own_property_keys();
  342. if (vm.exception())
  343. return MarkedValueList { heap() };
  344. // 3. Let properties be a new empty List.
  345. auto properties = MarkedValueList { heap() };
  346. // 4. For each element key of ownKeys, do
  347. for (auto& key : own_keys) {
  348. // a. If Type(key) is String, then
  349. if (!key.is_string())
  350. continue;
  351. auto property_name = PropertyName::from_value(global_object, key);
  352. // i. Let desc be ? O.[[GetOwnProperty]](key).
  353. auto descriptor = internal_get_own_property(property_name);
  354. if (vm.exception())
  355. return MarkedValueList { heap() };
  356. // ii. If desc is not undefined and desc.[[Enumerable]] is true, then
  357. if (descriptor.has_value() && *descriptor->enumerable) {
  358. // 1. If kind is key, append key to properties.
  359. if (kind == PropertyKind::Key) {
  360. properties.append(key);
  361. continue;
  362. }
  363. // 2. Else,
  364. // a. Let value be ? Get(O, key).
  365. auto value = get(property_name);
  366. if (vm.exception())
  367. return MarkedValueList { heap() };
  368. // b. If kind is value, append value to properties.
  369. if (kind == PropertyKind::Value) {
  370. properties.append(value);
  371. continue;
  372. }
  373. // c. Else,
  374. // i. Assert: kind is key+value.
  375. VERIFY(kind == PropertyKind::KeyAndValue);
  376. // ii. Let entry be ! CreateArrayFromList(« key, value »).
  377. auto entry = Array::create_from(global_object, { key, value });
  378. // iii. Append entry to properties.
  379. properties.append(entry);
  380. }
  381. }
  382. // 5. Return properties.
  383. return properties;
  384. }
  385. // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  386. // 10.1.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getprototypeof
  387. Object* Object::internal_get_prototype_of() const
  388. {
  389. // 1. Return O.[[Prototype]].
  390. return const_cast<Object*>(prototype());
  391. }
  392. // 10.1.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
  393. bool Object::internal_set_prototype_of(Object* new_prototype)
  394. {
  395. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  396. // 2. Let current be O.[[Prototype]].
  397. // 3. If SameValue(V, current) is true, return true.
  398. if (prototype() == new_prototype)
  399. return true;
  400. // 4. Let extensible be O.[[Extensible]].
  401. // 5. If extensible is false, return false.
  402. if (!m_is_extensible)
  403. return false;
  404. // 6. Let p be V.
  405. auto* prototype = new_prototype;
  406. // 7. Let done be false.
  407. // 8. Repeat, while done is false,
  408. while (prototype) {
  409. // a. If p is null, set done to true.
  410. // b. Else if SameValue(p, O) is true, return false.
  411. if (prototype == this)
  412. return false;
  413. // c. Else,
  414. // i. If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1, set done to true.
  415. // NOTE: This is a best-effort implementation; we don't have a good way of detecting whether certain virtual
  416. // Object methods have been overridden by a given object, but as ProxyObject is the only one doing that for
  417. // [[SetPrototypeOf]], this check does the trick.
  418. if (is<ProxyObject>(prototype))
  419. break;
  420. // ii. Else, set p to p.[[Prototype]].
  421. prototype = prototype->prototype();
  422. }
  423. // 9. Set O.[[Prototype]] to V.
  424. auto& shape = this->shape();
  425. if (shape.is_unique())
  426. shape.set_prototype_without_transition(new_prototype);
  427. else
  428. m_shape = shape.create_prototype_transition(new_prototype);
  429. // 10. Return true.
  430. return true;
  431. }
  432. // 10.1.3 [[IsExtensible]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-isextensible
  433. bool Object::internal_is_extensible() const
  434. {
  435. // 1. Return O.[[Extensible]].
  436. return m_is_extensible;
  437. }
  438. // 10.1.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions
  439. bool Object::internal_prevent_extensions()
  440. {
  441. // 1. Set O.[[Extensible]] to false.
  442. m_is_extensible = false;
  443. // 2. Return true.
  444. return true;
  445. }
  446. // 10.1.5 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getownproperty-p
  447. Optional<PropertyDescriptor> Object::internal_get_own_property(PropertyName const& property_name) const
  448. {
  449. // 1. Assert: IsPropertyKey(P) is true.
  450. VERIFY(property_name.is_valid());
  451. // 2. If O does not have an own property with key P, return undefined.
  452. if (!storage_has(property_name))
  453. return {};
  454. // 3. Let D be a newly created Property Descriptor with no fields.
  455. PropertyDescriptor descriptor;
  456. // 4. Let X be O's own property whose key is P.
  457. auto [value, attributes] = *storage_get(property_name);
  458. // 5. If X is a data property, then
  459. if (!value.is_accessor()) {
  460. // a. Set D.[[Value]] to the value of X's [[Value]] attribute.
  461. descriptor.value = value.value_or(js_undefined());
  462. // b. Set D.[[Writable]] to the value of X's [[Writable]] attribute.
  463. descriptor.writable = attributes.is_writable();
  464. }
  465. // 6. Else,
  466. else {
  467. // a. Assert: X is an accessor property.
  468. // b. Set D.[[Get]] to the value of X's [[Get]] attribute.
  469. descriptor.get = value.as_accessor().getter();
  470. // c. Set D.[[Set]] to the value of X's [[Set]] attribute.
  471. descriptor.set = value.as_accessor().setter();
  472. }
  473. // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute.
  474. descriptor.enumerable = attributes.is_enumerable();
  475. // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
  476. descriptor.configurable = attributes.is_configurable();
  477. // 9. Return D.
  478. return descriptor;
  479. }
  480. // 10.1.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  481. bool Object::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  482. {
  483. VERIFY(property_name.is_valid());
  484. auto& vm = this->vm();
  485. // 1. Let current be ? O.[[GetOwnProperty]](P).
  486. auto current = internal_get_own_property(property_name);
  487. if (vm.exception())
  488. return {};
  489. // 2. Let extensible be ? IsExtensible(O).
  490. auto extensible = is_extensible();
  491. if (vm.exception())
  492. return {};
  493. // 3. Return ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current).
  494. return validate_and_apply_property_descriptor(this, property_name, extensible, property_descriptor, current);
  495. }
  496. // 10.1.7 [[HasProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
  497. bool Object::internal_has_property(PropertyName const& property_name) const
  498. {
  499. auto& vm = this->vm();
  500. // 1. Assert: IsPropertyKey(P) is true.
  501. VERIFY(property_name.is_valid());
  502. // 2. Let hasOwn be ? O.[[GetOwnProperty]](P).
  503. auto has_own = internal_get_own_property(property_name);
  504. if (vm.exception())
  505. return {};
  506. // 3. If hasOwn is not undefined, return true.
  507. if (has_own.has_value())
  508. return true;
  509. // 4. Let parent be ? O.[[GetPrototypeOf]]().
  510. auto parent = internal_get_prototype_of();
  511. if (vm.exception())
  512. return {};
  513. // 5. If parent is not null, then
  514. if (parent) {
  515. // a. Return ? parent.[[HasProperty]](P).
  516. return parent->internal_has_property(property_name);
  517. }
  518. // 6. Return false.
  519. return false;
  520. }
  521. // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
  522. Value Object::internal_get(PropertyName const& property_name, Value receiver) const
  523. {
  524. VERIFY(!receiver.is_empty());
  525. auto& vm = this->vm();
  526. // 1. Assert: IsPropertyKey(P) is true.
  527. VERIFY(property_name.is_valid());
  528. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  529. auto descriptor = internal_get_own_property(property_name);
  530. if (vm.exception())
  531. return {};
  532. // 3. If desc is undefined, then
  533. if (!descriptor.has_value()) {
  534. // a. Let parent be ? O.[[GetPrototypeOf]]().
  535. auto parent = internal_get_prototype_of();
  536. if (vm.exception())
  537. return {};
  538. // b. If parent is null, return undefined.
  539. if (!parent)
  540. return js_undefined();
  541. // c. Return ? parent.[[Get]](P, Receiver).
  542. return parent->internal_get(property_name, receiver);
  543. }
  544. // 4. If IsDataDescriptor(desc) is true, return desc.[[Value]].
  545. if (descriptor->is_data_descriptor())
  546. return *descriptor->value;
  547. // 5. Assert: IsAccessorDescriptor(desc) is true.
  548. VERIFY(descriptor->is_accessor_descriptor());
  549. // 6. Let getter be desc.[[Get]].
  550. auto* getter = *descriptor->get;
  551. // 7. If getter is undefined, return undefined.
  552. if (!getter)
  553. return js_undefined();
  554. // 8. Return ? Call(getter, Receiver).
  555. return vm.call(*getter, receiver);
  556. }
  557. static bool ordinary_set_with_own_descriptor(Object&, PropertyName const&, Value, Value, Optional<PropertyDescriptor>);
  558. // 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
  559. bool Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
  560. {
  561. VERIFY(!value.is_empty());
  562. VERIFY(!receiver.is_empty());
  563. auto& vm = this->vm();
  564. // 1. Assert: IsPropertyKey(P) is true.
  565. VERIFY(property_name.is_valid());
  566. // 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
  567. auto own_descriptor = internal_get_own_property(property_name);
  568. if (vm.exception())
  569. return {};
  570. // 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  571. return ordinary_set_with_own_descriptor(*this, property_name, value, receiver, own_descriptor);
  572. }
  573. // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  574. bool ordinary_set_with_own_descriptor(Object& object, PropertyName const& property_name, Value value, Value receiver, Optional<PropertyDescriptor> own_descriptor)
  575. {
  576. auto& vm = object.vm();
  577. // 1. Assert: IsPropertyKey(P) is true.
  578. VERIFY(property_name.is_valid());
  579. // 2. If ownDesc is undefined, then
  580. if (!own_descriptor.has_value()) {
  581. // a. Let parent be ? O.[[GetPrototypeOf]]().
  582. auto parent = object.internal_get_prototype_of();
  583. if (vm.exception())
  584. return {};
  585. // b. If parent is not null, then
  586. if (parent) {
  587. // i. Return ? parent.[[Set]](P, V, Receiver).
  588. return parent->internal_set(property_name, value, receiver);
  589. }
  590. // c. Else,
  591. else {
  592. // i. Set ownDesc to the PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  593. own_descriptor = PropertyDescriptor {
  594. .value = js_undefined(),
  595. .writable = true,
  596. .enumerable = true,
  597. .configurable = true,
  598. };
  599. }
  600. }
  601. // 3. If IsDataDescriptor(ownDesc) is true, then
  602. if (own_descriptor->is_data_descriptor()) {
  603. // a. If ownDesc.[[Writable]] is false, return false.
  604. if (!*own_descriptor->writable)
  605. return false;
  606. // b. If Type(Receiver) is not Object, return false.
  607. if (!receiver.is_object())
  608. return false;
  609. // c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P).
  610. auto existing_descriptor = receiver.as_object().internal_get_own_property(property_name);
  611. if (vm.exception())
  612. return {};
  613. // d. If existingDescriptor is not undefined, then
  614. if (existing_descriptor.has_value()) {
  615. // i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
  616. if (existing_descriptor->is_accessor_descriptor())
  617. return false;
  618. // ii. If existingDescriptor.[[Writable]] is false, return false.
  619. if (!*existing_descriptor->writable)
  620. return false;
  621. // iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }.
  622. auto value_descriptor = PropertyDescriptor { .value = value };
  623. // iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
  624. return receiver.as_object().internal_define_own_property(property_name, value_descriptor);
  625. }
  626. // e. Else,
  627. else {
  628. // i. Assert: Receiver does not currently have a property P.
  629. VERIFY(!receiver.as_object().storage_has(property_name));
  630. // ii. Return ? CreateDataProperty(Receiver, P, V).
  631. return receiver.as_object().create_data_property(property_name, value);
  632. }
  633. }
  634. // 4. Assert: IsAccessorDescriptor(ownDesc) is true.
  635. VERIFY(own_descriptor->is_accessor_descriptor());
  636. // 5. Let setter be ownDesc.[[Set]].
  637. auto* setter = *own_descriptor->set;
  638. // 6. If setter is undefined, return false.
  639. if (!setter)
  640. return false;
  641. // 7. Perform ? Call(setter, Receiver, « V »).
  642. (void)vm.call(*setter, receiver, value);
  643. // 8. Return true.
  644. return true;
  645. }
  646. // 10.1.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-delete-p
  647. bool Object::internal_delete(PropertyName const& property_name)
  648. {
  649. auto& vm = this->vm();
  650. // 1. Assert: IsPropertyKey(P) is true.
  651. VERIFY(property_name.is_valid());
  652. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  653. auto descriptor = internal_get_own_property(property_name);
  654. if (vm.exception())
  655. return {};
  656. // 3. If desc is undefined, return true.
  657. if (!descriptor.has_value())
  658. return true;
  659. // 4. If desc.[[Configurable]] is true, then
  660. if (*descriptor->configurable) {
  661. // a. Remove the own property with name P from O.
  662. storage_delete(property_name);
  663. // b. Return true.
  664. return true;
  665. }
  666. // 5. Return false.
  667. return false;
  668. }
  669. // 10.1.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
  670. MarkedValueList Object::internal_own_property_keys() const
  671. {
  672. auto& vm = this->vm();
  673. // 1. Let keys be a new empty List.
  674. MarkedValueList keys { heap() };
  675. // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
  676. for (auto& entry : m_indexed_properties) {
  677. // a. Add P as the last element of keys.
  678. keys.append(js_string(vm, String::number(entry.index())));
  679. }
  680. // 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
  681. for (auto& it : shape().property_table_ordered()) {
  682. if (it.key.is_string()) {
  683. // a. Add P as the last element of keys.
  684. keys.append(it.key.to_value(vm));
  685. }
  686. }
  687. // 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  688. for (auto& it : shape().property_table_ordered()) {
  689. if (it.key.is_symbol()) {
  690. // a. Add P as the last element of keys.
  691. keys.append(it.key.to_value(vm));
  692. }
  693. }
  694. // 5. Return keys.
  695. return keys;
  696. }
  697. // 10.4.7.2 SetImmutablePrototype ( O, V ), https://tc39.es/ecma262/#sec-set-immutable-prototype
  698. bool Object::set_immutable_prototype(Object* prototype)
  699. {
  700. auto& vm = this->vm();
  701. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  702. // 2. Let current be ? O.[[GetPrototypeOf]]().
  703. auto* current = internal_get_prototype_of();
  704. if (vm.exception())
  705. return {};
  706. // 3. If SameValue(V, current) is true, return true.
  707. if (prototype == current)
  708. return true;
  709. // 4. Return false.
  710. return false;
  711. }
  712. Optional<ValueAndAttributes> Object::storage_get(PropertyName const& property_name) const
  713. {
  714. VERIFY(property_name.is_valid());
  715. Value value;
  716. PropertyAttributes attributes;
  717. if (property_name.is_number()) {
  718. auto value_and_attributes = m_indexed_properties.get(property_name.as_number());
  719. if (!value_and_attributes.has_value())
  720. return {};
  721. value = value_and_attributes->value;
  722. attributes = value_and_attributes->attributes;
  723. } else {
  724. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  725. if (!metadata.has_value())
  726. return {};
  727. value = m_storage[metadata->offset];
  728. attributes = metadata->attributes;
  729. }
  730. return ValueAndAttributes { .value = value, .attributes = attributes };
  731. }
  732. bool Object::storage_has(PropertyName const& property_name) const
  733. {
  734. VERIFY(property_name.is_valid());
  735. if (property_name.is_number())
  736. return m_indexed_properties.has_index(property_name.as_number());
  737. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  738. }
  739. void Object::storage_set(PropertyName const& property_name, ValueAndAttributes const& value_and_attributes)
  740. {
  741. VERIFY(property_name.is_valid());
  742. auto [value, attributes] = value_and_attributes;
  743. if (property_name.is_number()) {
  744. auto index = property_name.as_number();
  745. m_indexed_properties.put(index, value, attributes);
  746. return;
  747. }
  748. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  749. // Transitions are primarily interesting when scripts add properties to objects.
  750. if (!m_transitions_enabled && !m_shape->is_unique()) {
  751. m_shape->add_property_without_transition(property_name, attributes);
  752. m_storage.resize(m_shape->property_count());
  753. m_storage[m_shape->property_count() - 1] = value;
  754. return;
  755. }
  756. auto property_name_string_or_symbol = property_name.to_string_or_symbol();
  757. auto metadata = shape().lookup(property_name_string_or_symbol);
  758. if (!metadata.has_value()) {
  759. if (!m_shape->is_unique() && shape().property_count() > 100) {
  760. // If you add more than 100 properties to an object, let's stop doing
  761. // transitions to avoid filling up the heap with shapes.
  762. ensure_shape_is_unique();
  763. }
  764. if (m_shape->is_unique()) {
  765. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  766. m_storage.resize(m_shape->property_count());
  767. } else if (m_transitions_enabled) {
  768. set_shape(*m_shape->create_put_transition(property_name_string_or_symbol, attributes));
  769. } else {
  770. m_shape->add_property_without_transition(property_name, attributes);
  771. m_storage.resize(m_shape->property_count());
  772. }
  773. metadata = shape().lookup(property_name_string_or_symbol);
  774. VERIFY(metadata.has_value());
  775. }
  776. if (attributes != metadata->attributes) {
  777. if (m_shape->is_unique()) {
  778. m_shape->reconfigure_property_in_unique_shape(property_name_string_or_symbol, attributes);
  779. } else {
  780. set_shape(*m_shape->create_configure_transition(property_name_string_or_symbol, attributes));
  781. }
  782. metadata = shape().lookup(property_name_string_or_symbol);
  783. VERIFY(metadata.has_value());
  784. }
  785. m_storage[metadata->offset] = value;
  786. }
  787. void Object::storage_delete(PropertyName const& property_name)
  788. {
  789. VERIFY(property_name.is_valid());
  790. VERIFY(storage_has(property_name));
  791. if (property_name.is_number())
  792. return m_indexed_properties.remove(property_name.as_number());
  793. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  794. VERIFY(metadata.has_value());
  795. ensure_shape_is_unique();
  796. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), metadata->offset);
  797. m_storage.remove(metadata->offset);
  798. }
  799. void Object::set_shape(Shape& new_shape)
  800. {
  801. m_storage.resize(new_shape.property_count());
  802. m_shape = &new_shape;
  803. }
  804. void Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  805. {
  806. auto& vm = this->vm();
  807. String formatted_property_name;
  808. if (property_name.is_number()) {
  809. formatted_property_name = property_name.to_string();
  810. } else if (property_name.is_string()) {
  811. formatted_property_name = property_name.as_string();
  812. } else {
  813. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  814. }
  815. FunctionObject* getter_function = nullptr;
  816. if (getter) {
  817. auto name = String::formatted("get {}", formatted_property_name);
  818. getter_function = NativeFunction::create(global_object(), name, move(getter));
  819. getter_function->define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  820. getter_function->define_direct_property(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  821. }
  822. FunctionObject* setter_function = nullptr;
  823. if (setter) {
  824. auto name = String::formatted("set {}", formatted_property_name);
  825. setter_function = NativeFunction::create(global_object(), name, move(setter));
  826. setter_function->define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  827. setter_function->define_direct_property(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  828. }
  829. return define_direct_accessor(property_name, getter_function, setter_function, attribute);
  830. }
  831. void Object::define_direct_accessor(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  832. {
  833. VERIFY(property_name.is_valid());
  834. auto existing_property = storage_get(property_name).value_or({}).value;
  835. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  836. if (!accessor) {
  837. accessor = Accessor::create(vm(), getter, setter);
  838. define_direct_property(property_name, accessor, attributes);
  839. } else {
  840. if (getter)
  841. accessor->set_getter(getter);
  842. if (setter)
  843. accessor->set_setter(setter);
  844. }
  845. }
  846. void Object::ensure_shape_is_unique()
  847. {
  848. if (shape().is_unique())
  849. return;
  850. m_shape = m_shape->create_unique_clone();
  851. }
  852. // Simple side-effect free property lookup, following the prototype chain. Non-standard.
  853. Value Object::get_without_side_effects(const PropertyName& property_name) const
  854. {
  855. auto* object = this;
  856. while (object) {
  857. auto value_and_attributes = object->storage_get(property_name);
  858. if (value_and_attributes.has_value())
  859. return value_and_attributes->value;
  860. object = object->prototype();
  861. }
  862. return {};
  863. }
  864. void Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  865. {
  866. auto& vm = this->vm();
  867. String function_name;
  868. if (property_name.is_string()) {
  869. function_name = property_name.as_string();
  870. } else {
  871. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  872. }
  873. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  874. function->define_direct_property(vm.names.length, Value(length), Attribute::Configurable);
  875. function->define_direct_property(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  876. define_direct_property(property_name, function, attribute);
  877. }
  878. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  879. Object* Object::define_properties(Value properties)
  880. {
  881. auto& vm = this->vm();
  882. auto& global_object = this->global_object();
  883. // 1. Assert: Type(O) is Object.
  884. // 2. Let props be ? ToObject(Properties).
  885. auto* props = properties.to_object(global_object);
  886. if (vm.exception())
  887. return {};
  888. // 3. Let keys be ? props.[[OwnPropertyKeys]]().
  889. auto keys = props->internal_own_property_keys();
  890. if (vm.exception())
  891. return {};
  892. struct NameAndDescriptor {
  893. PropertyName name;
  894. PropertyDescriptor descriptor;
  895. };
  896. // 4. Let descriptors be a new empty List.
  897. Vector<NameAndDescriptor> descriptors;
  898. // 5. For each element nextKey of keys, do
  899. for (auto& next_key : keys) {
  900. auto property_name = PropertyName::from_value(global_object, next_key);
  901. // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
  902. auto property_descriptor = props->internal_get_own_property(property_name);
  903. if (vm.exception())
  904. return {};
  905. // b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  906. if (property_descriptor.has_value() && *property_descriptor->enumerable) {
  907. // i. Let descObj be ? Get(props, nextKey).
  908. auto descriptor_object = props->get(property_name);
  909. if (vm.exception())
  910. return {};
  911. // ii. Let desc be ? ToPropertyDescriptor(descObj).
  912. auto descriptor = to_property_descriptor(global_object, descriptor_object);
  913. if (vm.exception())
  914. return {};
  915. // iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
  916. descriptors.append({ property_name, descriptor });
  917. }
  918. }
  919. // 6. For each element pair of descriptors, do
  920. for (auto& [name, descriptor] : descriptors) {
  921. // a. Let P be the first element of pair.
  922. // b. Let desc be the second element of pair.
  923. // c. Perform ? DefinePropertyOrThrow(O, P, desc).
  924. define_property_or_throw(name, descriptor);
  925. if (vm.exception())
  926. return {};
  927. }
  928. // 7. Return O.
  929. return this;
  930. }
  931. void Object::visit_edges(Cell::Visitor& visitor)
  932. {
  933. Cell::visit_edges(visitor);
  934. visitor.visit(m_shape);
  935. for (auto& value : m_storage)
  936. visitor.visit(value);
  937. m_indexed_properties.for_each_value([&visitor](auto& value) {
  938. visitor.visit(value);
  939. });
  940. }
  941. // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive
  942. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  943. {
  944. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  945. auto& vm = this->vm();
  946. AK::Array<PropertyName, 2> method_names;
  947. if (preferred_type == Value::PreferredType::String)
  948. method_names = { vm.names.toString, vm.names.valueOf };
  949. else
  950. method_names = { vm.names.valueOf, vm.names.toString };
  951. for (auto& method_name : method_names) {
  952. auto method = get(method_name);
  953. if (vm.exception())
  954. return {};
  955. if (method.is_function()) {
  956. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  957. if (!result.is_object())
  958. return result;
  959. }
  960. }
  961. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  962. return {};
  963. }
  964. Value Object::invoke_internal(PropertyName const& property_name, Optional<MarkedValueList> arguments)
  965. {
  966. auto& vm = this->vm();
  967. auto property = get(property_name);
  968. if (vm.exception())
  969. return {};
  970. if (!property.is_function()) {
  971. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  972. return {};
  973. }
  974. return vm.call(property.as_function(), this, move(arguments));
  975. }
  976. }