Object.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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 TRY_OR_DISCARD(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 = TRY_OR_DISCARD(internal_prevent_extensions());
  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. ThrowCompletionOr<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. ThrowCompletionOr<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. ThrowCompletionOr<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. ThrowCompletionOr<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 = TRY_OR_DISCARD(internal_get_prototype_of());
  511. // 5. If parent is not null, then
  512. if (parent) {
  513. // a. Return ? parent.[[HasProperty]](P).
  514. return parent->internal_has_property(property_name);
  515. }
  516. // 6. Return false.
  517. return false;
  518. }
  519. // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
  520. Value Object::internal_get(PropertyName const& property_name, Value receiver) const
  521. {
  522. VERIFY(!receiver.is_empty());
  523. auto& vm = this->vm();
  524. // 1. Assert: IsPropertyKey(P) is true.
  525. VERIFY(property_name.is_valid());
  526. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  527. auto descriptor = internal_get_own_property(property_name);
  528. if (vm.exception())
  529. return {};
  530. // 3. If desc is undefined, then
  531. if (!descriptor.has_value()) {
  532. // a. Let parent be ? O.[[GetPrototypeOf]]().
  533. auto* parent = TRY_OR_DISCARD(internal_get_prototype_of());
  534. // b. If parent is null, return undefined.
  535. if (!parent)
  536. return js_undefined();
  537. // c. Return ? parent.[[Get]](P, Receiver).
  538. return parent->internal_get(property_name, receiver);
  539. }
  540. // 4. If IsDataDescriptor(desc) is true, return desc.[[Value]].
  541. if (descriptor->is_data_descriptor())
  542. return *descriptor->value;
  543. // 5. Assert: IsAccessorDescriptor(desc) is true.
  544. VERIFY(descriptor->is_accessor_descriptor());
  545. // 6. Let getter be desc.[[Get]].
  546. auto* getter = *descriptor->get;
  547. // 7. If getter is undefined, return undefined.
  548. if (!getter)
  549. return js_undefined();
  550. // 8. Return ? Call(getter, Receiver).
  551. return TRY_OR_DISCARD(vm.call(*getter, receiver));
  552. }
  553. // 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
  554. bool Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
  555. {
  556. VERIFY(!value.is_empty());
  557. VERIFY(!receiver.is_empty());
  558. auto& vm = this->vm();
  559. // 1. Assert: IsPropertyKey(P) is true.
  560. VERIFY(property_name.is_valid());
  561. // 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
  562. auto own_descriptor = internal_get_own_property(property_name);
  563. if (vm.exception())
  564. return {};
  565. // 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  566. return ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
  567. }
  568. // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  569. bool Object::ordinary_set_with_own_descriptor(PropertyName const& property_name, Value value, Value receiver, Optional<PropertyDescriptor> own_descriptor)
  570. {
  571. auto& vm = this->vm();
  572. // 1. Assert: IsPropertyKey(P) is true.
  573. VERIFY(property_name.is_valid());
  574. // 2. If ownDesc is undefined, then
  575. if (!own_descriptor.has_value()) {
  576. // a. Let parent be ? O.[[GetPrototypeOf]]().
  577. auto* parent = TRY_OR_DISCARD(internal_get_prototype_of());
  578. // b. If parent is not null, then
  579. if (parent) {
  580. // i. Return ? parent.[[Set]](P, V, Receiver).
  581. return parent->internal_set(property_name, value, receiver);
  582. }
  583. // c. Else,
  584. else {
  585. // i. Set ownDesc to the PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  586. own_descriptor = PropertyDescriptor {
  587. .value = js_undefined(),
  588. .writable = true,
  589. .enumerable = true,
  590. .configurable = true,
  591. };
  592. }
  593. }
  594. // 3. If IsDataDescriptor(ownDesc) is true, then
  595. if (own_descriptor->is_data_descriptor()) {
  596. // a. If ownDesc.[[Writable]] is false, return false.
  597. if (!*own_descriptor->writable)
  598. return false;
  599. // b. If Type(Receiver) is not Object, return false.
  600. if (!receiver.is_object())
  601. return false;
  602. // c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P).
  603. auto existing_descriptor = receiver.as_object().internal_get_own_property(property_name);
  604. if (vm.exception())
  605. return {};
  606. // d. If existingDescriptor is not undefined, then
  607. if (existing_descriptor.has_value()) {
  608. // i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
  609. if (existing_descriptor->is_accessor_descriptor())
  610. return false;
  611. // ii. If existingDescriptor.[[Writable]] is false, return false.
  612. if (!*existing_descriptor->writable)
  613. return false;
  614. // iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }.
  615. auto value_descriptor = PropertyDescriptor { .value = value };
  616. // iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
  617. return receiver.as_object().internal_define_own_property(property_name, value_descriptor);
  618. }
  619. // e. Else,
  620. else {
  621. // i. Assert: Receiver does not currently have a property P.
  622. VERIFY(!receiver.as_object().storage_has(property_name));
  623. // ii. Return ? CreateDataProperty(Receiver, P, V).
  624. return receiver.as_object().create_data_property(property_name, value);
  625. }
  626. }
  627. // 4. Assert: IsAccessorDescriptor(ownDesc) is true.
  628. VERIFY(own_descriptor->is_accessor_descriptor());
  629. // 5. Let setter be ownDesc.[[Set]].
  630. auto* setter = *own_descriptor->set;
  631. // 6. If setter is undefined, return false.
  632. if (!setter)
  633. return false;
  634. // 7. Perform ? Call(setter, Receiver, « V »).
  635. (void)vm.call(*setter, receiver, value);
  636. // 8. Return true.
  637. return true;
  638. }
  639. // 10.1.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-delete-p
  640. bool Object::internal_delete(PropertyName const& property_name)
  641. {
  642. auto& vm = this->vm();
  643. // 1. Assert: IsPropertyKey(P) is true.
  644. VERIFY(property_name.is_valid());
  645. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  646. auto descriptor = internal_get_own_property(property_name);
  647. if (vm.exception())
  648. return {};
  649. // 3. If desc is undefined, return true.
  650. if (!descriptor.has_value())
  651. return true;
  652. // 4. If desc.[[Configurable]] is true, then
  653. if (*descriptor->configurable) {
  654. // a. Remove the own property with name P from O.
  655. storage_delete(property_name);
  656. // b. Return true.
  657. return true;
  658. }
  659. // 5. Return false.
  660. return false;
  661. }
  662. // 10.1.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
  663. MarkedValueList Object::internal_own_property_keys() const
  664. {
  665. auto& vm = this->vm();
  666. // 1. Let keys be a new empty List.
  667. MarkedValueList keys { heap() };
  668. // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
  669. for (auto& entry : m_indexed_properties) {
  670. // a. Add P as the last element of keys.
  671. keys.append(js_string(vm, String::number(entry.index())));
  672. }
  673. // 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
  674. for (auto& it : shape().property_table_ordered()) {
  675. if (it.key.is_string()) {
  676. // a. Add P as the last element of keys.
  677. keys.append(it.key.to_value(vm));
  678. }
  679. }
  680. // 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  681. for (auto& it : shape().property_table_ordered()) {
  682. if (it.key.is_symbol()) {
  683. // a. Add P as the last element of keys.
  684. keys.append(it.key.to_value(vm));
  685. }
  686. }
  687. // 5. Return keys.
  688. return keys;
  689. }
  690. // 10.4.7.2 SetImmutablePrototype ( O, V ), https://tc39.es/ecma262/#sec-set-immutable-prototype
  691. bool Object::set_immutable_prototype(Object* prototype)
  692. {
  693. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  694. // 2. Let current be ? O.[[GetPrototypeOf]]().
  695. auto* current = TRY_OR_DISCARD(internal_get_prototype_of());
  696. // 3. If SameValue(V, current) is true, return true.
  697. if (prototype == current)
  698. return true;
  699. // 4. Return false.
  700. return false;
  701. }
  702. Optional<ValueAndAttributes> Object::storage_get(PropertyName const& property_name) const
  703. {
  704. VERIFY(property_name.is_valid());
  705. Value value;
  706. PropertyAttributes attributes;
  707. if (property_name.is_number()) {
  708. auto value_and_attributes = m_indexed_properties.get(property_name.as_number());
  709. if (!value_and_attributes.has_value())
  710. return {};
  711. value = value_and_attributes->value;
  712. attributes = value_and_attributes->attributes;
  713. } else {
  714. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  715. if (!metadata.has_value())
  716. return {};
  717. value = m_storage[metadata->offset];
  718. attributes = metadata->attributes;
  719. }
  720. return ValueAndAttributes { .value = value, .attributes = attributes };
  721. }
  722. bool Object::storage_has(PropertyName const& property_name) const
  723. {
  724. VERIFY(property_name.is_valid());
  725. if (property_name.is_number())
  726. return m_indexed_properties.has_index(property_name.as_number());
  727. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  728. }
  729. void Object::storage_set(PropertyName const& property_name, ValueAndAttributes const& value_and_attributes)
  730. {
  731. VERIFY(property_name.is_valid());
  732. auto [value, attributes] = value_and_attributes;
  733. if (property_name.is_number()) {
  734. auto index = property_name.as_number();
  735. m_indexed_properties.put(index, value, attributes);
  736. return;
  737. }
  738. auto property_name_string_or_symbol = property_name.to_string_or_symbol();
  739. // NOTE: We don't do transitions or check for attribute changes during object initialization,
  740. // which makes building common runtime objects significantly faster. Transitions are primarily
  741. // interesting when scripts add properties to objects.
  742. if (!m_initialized) {
  743. if (m_shape->is_unique())
  744. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  745. else
  746. m_shape->add_property_without_transition(property_name_string_or_symbol, attributes);
  747. m_storage.append(value);
  748. return;
  749. }
  750. auto metadata = shape().lookup(property_name_string_or_symbol);
  751. if (!metadata.has_value()) {
  752. if (!m_shape->is_unique() && shape().property_count() > 100) {
  753. // If you add more than 100 properties to an object, let's stop doing
  754. // transitions to avoid filling up the heap with shapes.
  755. ensure_shape_is_unique();
  756. }
  757. if (m_shape->is_unique())
  758. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  759. else if (!m_transitions_enabled)
  760. m_shape->add_property_without_transition(property_name_string_or_symbol, attributes);
  761. else
  762. set_shape(*m_shape->create_put_transition(property_name_string_or_symbol, attributes));
  763. m_storage.append(value);
  764. return;
  765. }
  766. if (attributes != metadata->attributes) {
  767. if (m_shape->is_unique())
  768. m_shape->reconfigure_property_in_unique_shape(property_name_string_or_symbol, attributes);
  769. else if (!m_transitions_enabled)
  770. VERIFY_NOT_REACHED(); // We currently don't have a way of doing this, and it's not used anywhere either.
  771. else
  772. set_shape(*m_shape->create_configure_transition(property_name_string_or_symbol, attributes));
  773. }
  774. m_storage[metadata->offset] = value;
  775. }
  776. void Object::storage_delete(PropertyName const& property_name)
  777. {
  778. VERIFY(property_name.is_valid());
  779. VERIFY(storage_has(property_name));
  780. if (property_name.is_number())
  781. return m_indexed_properties.remove(property_name.as_number());
  782. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  783. VERIFY(metadata.has_value());
  784. ensure_shape_is_unique();
  785. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), metadata->offset);
  786. m_storage.remove(metadata->offset);
  787. }
  788. void Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  789. {
  790. auto& vm = this->vm();
  791. String formatted_property_name;
  792. if (property_name.is_number()) {
  793. formatted_property_name = property_name.to_string();
  794. } else if (property_name.is_string()) {
  795. formatted_property_name = property_name.as_string();
  796. } else {
  797. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  798. }
  799. FunctionObject* getter_function = nullptr;
  800. if (getter) {
  801. auto name = String::formatted("get {}", formatted_property_name);
  802. getter_function = NativeFunction::create(global_object(), name, move(getter));
  803. getter_function->define_direct_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  804. getter_function->define_direct_property_without_transition(vm.names.name, js_string(vm, name), Attribute::Configurable);
  805. }
  806. FunctionObject* setter_function = nullptr;
  807. if (setter) {
  808. auto name = String::formatted("set {}", formatted_property_name);
  809. setter_function = NativeFunction::create(global_object(), name, move(setter));
  810. setter_function->define_direct_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  811. setter_function->define_direct_property_without_transition(vm.names.name, js_string(vm, name), Attribute::Configurable);
  812. }
  813. return define_direct_accessor(property_name, getter_function, setter_function, attribute);
  814. }
  815. void Object::define_direct_accessor(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  816. {
  817. VERIFY(property_name.is_valid());
  818. auto existing_property = storage_get(property_name).value_or({}).value;
  819. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  820. if (!accessor) {
  821. accessor = Accessor::create(vm(), getter, setter);
  822. define_direct_property(property_name, accessor, attributes);
  823. } else {
  824. if (getter)
  825. accessor->set_getter(getter);
  826. if (setter)
  827. accessor->set_setter(setter);
  828. }
  829. }
  830. void Object::define_direct_property_without_transition(PropertyName const& property_name, Value value, PropertyAttributes attributes)
  831. {
  832. TemporaryChange disable_transitions(m_transitions_enabled, false);
  833. define_direct_property(property_name, value, attributes);
  834. }
  835. void Object::define_direct_accessor_without_transition(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  836. {
  837. TemporaryChange disable_transitions(m_transitions_enabled, false);
  838. define_direct_accessor(property_name, getter, setter, attributes);
  839. }
  840. void Object::ensure_shape_is_unique()
  841. {
  842. if (shape().is_unique())
  843. return;
  844. m_shape = m_shape->create_unique_clone();
  845. }
  846. // Simple side-effect free property lookup, following the prototype chain. Non-standard.
  847. Value Object::get_without_side_effects(const PropertyName& property_name) const
  848. {
  849. auto* object = this;
  850. while (object) {
  851. auto value_and_attributes = object->storage_get(property_name);
  852. if (value_and_attributes.has_value())
  853. return value_and_attributes->value;
  854. object = object->prototype();
  855. }
  856. return {};
  857. }
  858. void Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  859. {
  860. auto& vm = this->vm();
  861. String function_name;
  862. if (property_name.is_string()) {
  863. function_name = property_name.as_string();
  864. } else {
  865. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  866. }
  867. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  868. function->define_direct_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  869. function->define_direct_property_without_transition(vm.names.name, js_string(vm, function_name), Attribute::Configurable);
  870. define_direct_property(property_name, function, attribute);
  871. }
  872. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  873. Object* Object::define_properties(Value properties)
  874. {
  875. auto& vm = this->vm();
  876. auto& global_object = this->global_object();
  877. // 1. Assert: Type(O) is Object.
  878. // 2. Let props be ? ToObject(Properties).
  879. auto* props = properties.to_object(global_object);
  880. if (vm.exception())
  881. return {};
  882. // 3. Let keys be ? props.[[OwnPropertyKeys]]().
  883. auto keys = props->internal_own_property_keys();
  884. if (vm.exception())
  885. return {};
  886. struct NameAndDescriptor {
  887. PropertyName name;
  888. PropertyDescriptor descriptor;
  889. };
  890. // 4. Let descriptors be a new empty List.
  891. Vector<NameAndDescriptor> descriptors;
  892. // 5. For each element nextKey of keys, do
  893. for (auto& next_key : keys) {
  894. auto property_name = PropertyName::from_value(global_object, next_key);
  895. // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
  896. auto property_descriptor = props->internal_get_own_property(property_name);
  897. if (vm.exception())
  898. return {};
  899. // b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  900. if (property_descriptor.has_value() && *property_descriptor->enumerable) {
  901. // i. Let descObj be ? Get(props, nextKey).
  902. auto descriptor_object = props->get(property_name);
  903. if (vm.exception())
  904. return {};
  905. // ii. Let desc be ? ToPropertyDescriptor(descObj).
  906. auto descriptor = to_property_descriptor(global_object, descriptor_object);
  907. if (vm.exception())
  908. return {};
  909. // iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
  910. descriptors.append({ property_name, descriptor });
  911. }
  912. }
  913. // 6. For each element pair of descriptors, do
  914. for (auto& [name, descriptor] : descriptors) {
  915. // a. Let P be the first element of pair.
  916. // b. Let desc be the second element of pair.
  917. // c. Perform ? DefinePropertyOrThrow(O, P, desc).
  918. define_property_or_throw(name, descriptor);
  919. if (vm.exception())
  920. return {};
  921. }
  922. // 7. Return O.
  923. return this;
  924. }
  925. void Object::visit_edges(Cell::Visitor& visitor)
  926. {
  927. Cell::visit_edges(visitor);
  928. visitor.visit(m_shape);
  929. for (auto& value : m_storage)
  930. visitor.visit(value);
  931. m_indexed_properties.for_each_value([&visitor](auto& value) {
  932. visitor.visit(value);
  933. });
  934. }
  935. // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive
  936. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  937. {
  938. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  939. auto& vm = this->vm();
  940. AK::Array<PropertyName, 2> method_names;
  941. if (preferred_type == Value::PreferredType::String)
  942. method_names = { vm.names.toString, vm.names.valueOf };
  943. else
  944. method_names = { vm.names.valueOf, vm.names.toString };
  945. for (auto& method_name : method_names) {
  946. auto method = get(method_name);
  947. if (vm.exception())
  948. return {};
  949. if (method.is_function()) {
  950. auto result = TRY_OR_DISCARD(vm.call(method.as_function(), const_cast<Object*>(this)));
  951. if (!result.is_object())
  952. return result;
  953. }
  954. }
  955. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  956. return {};
  957. }
  958. }