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