Object.cpp 41 KB

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