Object.cpp 44 KB

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