Object.cpp 43 KB

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