Object.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <LibJS/Interpreter.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Accessor.h>
  11. #include <LibJS/Runtime/Array.h>
  12. #include <LibJS/Runtime/Error.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/NativeFunction.h>
  15. #include <LibJS/Runtime/Object.h>
  16. #include <LibJS/Runtime/PropertyDescriptor.h>
  17. #include <LibJS/Runtime/ProxyObject.h>
  18. #include <LibJS/Runtime/Shape.h>
  19. #include <LibJS/Runtime/TemporaryClearException.h>
  20. #include <LibJS/Runtime/Value.h>
  21. namespace JS {
  22. // 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  23. Object* Object::create(GlobalObject& global_object, Object* prototype)
  24. {
  25. if (!prototype)
  26. return global_object.heap().allocate<Object>(global_object, *global_object.empty_object_shape());
  27. else if (prototype == global_object.object_prototype())
  28. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  29. else
  30. return global_object.heap().allocate<Object>(global_object, *prototype);
  31. }
  32. Object::Object(GlobalObjectTag)
  33. {
  34. // This is the global object
  35. m_shape = heap().allocate_without_global_object<Shape>(*this);
  36. }
  37. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  38. {
  39. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  40. }
  41. Object::Object(Object& prototype)
  42. {
  43. m_shape = prototype.global_object().empty_object_shape();
  44. 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(PropertyName const& property_name) const
  67. {
  68. // 1. Assert: Type(O) is Object.
  69. // 2. Assert: IsPropertyKey(P) is true.
  70. VERIFY(property_name.is_valid());
  71. // 3. Return ? O.[[Get]](P, O).
  72. return TRY(internal_get(property_name, this));
  73. }
  74. // 7.3.3 GetV ( V, P ) is defined as Value::get().
  75. // 7.3.4 Set ( O, P, V, Throw ), https://tc39.es/ecma262/#sec-set-o-p-v-throw
  76. ThrowCompletionOr<bool> Object::set(PropertyName const& property_name, Value value, ShouldThrowExceptions throw_exceptions)
  77. {
  78. VERIFY(!value.is_empty());
  79. auto& vm = this->vm();
  80. // 1. Assert: Type(O) is Object.
  81. // 2. Assert: IsPropertyKey(P) is true.
  82. VERIFY(property_name.is_valid());
  83. // 3. Assert: Type(Throw) is Boolean.
  84. // 4. Let success be ? O.[[Set]](P, V, O).
  85. auto success = TRY(internal_set(property_name, 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(PropertyName const& property_name, Value value)
  96. {
  97. // 1. Assert: Type(O) is Object.
  98. // 2. Assert: IsPropertyKey(P) is true.
  99. VERIFY(property_name.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_name, new_descriptor);
  109. }
  110. // 7.3.6 CreateMethodProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createmethodproperty
  111. ThrowCompletionOr<bool> Object::create_method_property(PropertyName const& property_name, 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_name.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_name, 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(PropertyName const& property_name, 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_name.is_valid());
  135. // 3. Let success be ? CreateDataProperty(O, P, V).
  136. auto success = TRY(create_data_property(property_name, 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.6 CreateNonEnumerableDataPropertyOrThrow ( O, P, V ), https://tc39.es/proposal-error-cause/#sec-createnonenumerabledatapropertyorthrow
  146. ThrowCompletionOr<bool> Object::create_non_enumerable_data_property_or_throw(PropertyName const& property_name, Value value)
  147. {
  148. VERIFY(!value.is_empty());
  149. VERIFY(property_name.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_name, new_description);
  154. }
  155. // 7.3.8 DefinePropertyOrThrow ( O, P, desc ), https://tc39.es/ecma262/#sec-definepropertyorthrow
  156. ThrowCompletionOr<bool> Object::define_property_or_throw(PropertyName const& property_name, 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_name.is_valid());
  162. // 3. Let success be ? O.[[DefineOwnProperty]](P, desc).
  163. auto success = TRY(internal_define_own_property(property_name, 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.9 DeletePropertyOrThrow ( O, P ), https://tc39.es/ecma262/#sec-deletepropertyorthrow
  173. ThrowCompletionOr<bool> Object::delete_property_or_throw(PropertyName const& property_name)
  174. {
  175. auto& vm = this->vm();
  176. // 1. Assert: Type(O) is Object.
  177. // 2. Assert: IsPropertyKey(P) is true.
  178. VERIFY(property_name.is_valid());
  179. // 3. Let success be ? O.[[Delete]](P).
  180. auto success = TRY(internal_delete(property_name));
  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.11 HasProperty ( O, P ), https://tc39.es/ecma262/#sec-hasproperty
  190. ThrowCompletionOr<bool> Object::has_property(PropertyName const& property_name) const
  191. {
  192. // 1. Assert: Type(O) is Object.
  193. // 2. Assert: IsPropertyKey(P) is true.
  194. VERIFY(property_name.is_valid());
  195. // 3. Return ? O.[[HasProperty]](P).
  196. return internal_has_property(property_name);
  197. }
  198. // 7.3.12 HasOwnProperty ( O, P ), https://tc39.es/ecma262/#sec-hasownproperty
  199. ThrowCompletionOr<bool> Object::has_own_property(PropertyName const& property_name) const
  200. {
  201. // 1. Assert: Type(O) is Object.
  202. // 2. Assert: IsPropertyKey(P) is true.
  203. VERIFY(property_name.is_valid());
  204. // 3. Let desc be ? O.[[GetOwnProperty]](P).
  205. auto descriptor = TRY(internal_get_own_property(property_name));
  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.15 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_name = PropertyName::from_value(global_object, key);
  231. // i. Perform ? DefinePropertyOrThrow(O, k, PropertyDescriptor { [[Configurable]]: false }).
  232. TRY(define_property_or_throw(property_name, { .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_name = PropertyName::from_value(global_object, key);
  241. // i. Let currentDesc be ? O.[[GetOwnProperty]](k).
  242. auto current_descriptor = TRY(internal_get_own_property(property_name));
  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_name, descriptor));
  259. }
  260. }
  261. // 8. Return true.
  262. return true;
  263. }
  264. // 7.3.16 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_name = PropertyName::from_value(global_object(), key);
  281. // a. Let currentDesc be ? O.[[GetOwnProperty]](k).
  282. auto current_descriptor = TRY(internal_get_own_property(property_name));
  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.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
  300. ThrowCompletionOr<MarkedValueList> 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 = MarkedValueList { 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_name = PropertyName::from_value(global_object, key);
  316. // i. Let desc be ? O.[[GetOwnProperty]](key).
  317. auto descriptor = TRY(internal_get_own_property(property_name));
  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_name));
  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.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties
  346. ThrowCompletionOr<Object*> Object::copy_data_properties(Value source, HashTable<PropertyName, PropertyNameTraits> 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 = PropertyName::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. // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  364. // 10.1.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getprototypeof
  365. ThrowCompletionOr<Object*> Object::internal_get_prototype_of() const
  366. {
  367. // 1. Return O.[[Prototype]].
  368. return const_cast<Object*>(prototype());
  369. }
  370. // 10.1.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
  371. ThrowCompletionOr<bool> Object::internal_set_prototype_of(Object* new_prototype)
  372. {
  373. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  374. // 2. Let current be O.[[Prototype]].
  375. // 3. If SameValue(V, current) is true, return true.
  376. if (prototype() == new_prototype)
  377. return true;
  378. // 4. Let extensible be O.[[Extensible]].
  379. // 5. If extensible is false, return false.
  380. if (!m_is_extensible)
  381. return false;
  382. // 6. Let p be V.
  383. auto* prototype = new_prototype;
  384. // 7. Let done be false.
  385. // 8. Repeat, while done is false,
  386. while (prototype) {
  387. // a. If p is null, set done to true.
  388. // b. Else if SameValue(p, O) is true, return false.
  389. if (prototype == this)
  390. return false;
  391. // c. Else,
  392. // i. If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1, set done to true.
  393. // NOTE: This is a best-effort implementation; we don't have a good way of detecting whether certain virtual
  394. // Object methods have been overridden by a given object, but as ProxyObject is the only one doing that for
  395. // [[SetPrototypeOf]], this check does the trick.
  396. if (is<ProxyObject>(prototype))
  397. break;
  398. // ii. Else, set p to p.[[Prototype]].
  399. prototype = prototype->prototype();
  400. }
  401. // 9. Set O.[[Prototype]] to V.
  402. set_prototype(new_prototype);
  403. // 10. Return true.
  404. return true;
  405. }
  406. // 10.1.3 [[IsExtensible]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-isextensible
  407. ThrowCompletionOr<bool> Object::internal_is_extensible() const
  408. {
  409. // 1. Return O.[[Extensible]].
  410. return m_is_extensible;
  411. }
  412. // 10.1.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions
  413. ThrowCompletionOr<bool> Object::internal_prevent_extensions()
  414. {
  415. // 1. Set O.[[Extensible]] to false.
  416. m_is_extensible = false;
  417. // 2. Return true.
  418. return true;
  419. }
  420. // 10.1.5 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getownproperty-p
  421. ThrowCompletionOr<Optional<PropertyDescriptor>> Object::internal_get_own_property(PropertyName const& property_name) const
  422. {
  423. // 1. Assert: IsPropertyKey(P) is true.
  424. VERIFY(property_name.is_valid());
  425. // 2. If O does not have an own property with key P, return undefined.
  426. auto maybe_storage_entry = storage_get(property_name);
  427. if (!maybe_storage_entry.has_value())
  428. return Optional<PropertyDescriptor> {};
  429. // 3. Let D be a newly created Property Descriptor with no fields.
  430. PropertyDescriptor descriptor;
  431. // 4. Let X be O's own property whose key is P.
  432. auto [value, attributes] = *maybe_storage_entry;
  433. // 5. If X is a data property, then
  434. if (!value.is_accessor()) {
  435. // a. Set D.[[Value]] to the value of X's [[Value]] attribute.
  436. descriptor.value = value.value_or(js_undefined());
  437. // b. Set D.[[Writable]] to the value of X's [[Writable]] attribute.
  438. descriptor.writable = attributes.is_writable();
  439. }
  440. // 6. Else,
  441. else {
  442. // a. Assert: X is an accessor property.
  443. // b. Set D.[[Get]] to the value of X's [[Get]] attribute.
  444. descriptor.get = value.as_accessor().getter();
  445. // c. Set D.[[Set]] to the value of X's [[Set]] attribute.
  446. descriptor.set = value.as_accessor().setter();
  447. }
  448. // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute.
  449. descriptor.enumerable = attributes.is_enumerable();
  450. // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
  451. descriptor.configurable = attributes.is_configurable();
  452. // 9. Return D.
  453. return { descriptor };
  454. }
  455. // 10.1.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  456. ThrowCompletionOr<bool> Object::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  457. {
  458. VERIFY(property_name.is_valid());
  459. // 1. Let current be ? O.[[GetOwnProperty]](P).
  460. auto current = TRY(internal_get_own_property(property_name));
  461. // 2. Let extensible be ? IsExtensible(O).
  462. auto extensible = TRY(is_extensible());
  463. // 3. Return ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current).
  464. return validate_and_apply_property_descriptor(this, property_name, extensible, property_descriptor, current);
  465. }
  466. // 10.1.7 [[HasProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
  467. ThrowCompletionOr<bool> Object::internal_has_property(PropertyName const& property_name) const
  468. {
  469. auto& vm = this->vm();
  470. // 1. Assert: IsPropertyKey(P) is true.
  471. VERIFY(property_name.is_valid());
  472. // 2. Let hasOwn be ? O.[[GetOwnProperty]](P).
  473. auto has_own = TRY(internal_get_own_property(property_name));
  474. // 3. If hasOwn is not undefined, return true.
  475. if (has_own.has_value())
  476. return true;
  477. // 4. Let parent be ? O.[[GetPrototypeOf]]().
  478. auto* parent = TRY(internal_get_prototype_of());
  479. // 5. If parent is not null, then
  480. if (parent) {
  481. // a. Return ? parent.[[HasProperty]](P).
  482. auto result = parent->internal_has_property(property_name);
  483. if (auto* exception = vm.exception())
  484. return throw_completion(exception->value());
  485. return result;
  486. }
  487. // 6. Return false.
  488. return false;
  489. }
  490. // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
  491. ThrowCompletionOr<Value> Object::internal_get(PropertyName const& property_name, Value receiver) const
  492. {
  493. VERIFY(!receiver.is_empty());
  494. auto& vm = this->vm();
  495. // 1. Assert: IsPropertyKey(P) is true.
  496. VERIFY(property_name.is_valid());
  497. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  498. auto descriptor = TRY(internal_get_own_property(property_name));
  499. // 3. If desc is undefined, then
  500. if (!descriptor.has_value()) {
  501. // a. Let parent be ? O.[[GetPrototypeOf]]().
  502. auto* parent = TRY(internal_get_prototype_of());
  503. // b. If parent is null, return undefined.
  504. if (!parent)
  505. return js_undefined();
  506. // c. Return ? parent.[[Get]](P, Receiver).
  507. return parent->internal_get(property_name, receiver);
  508. }
  509. // 4. If IsDataDescriptor(desc) is true, return desc.[[Value]].
  510. if (descriptor->is_data_descriptor())
  511. return *descriptor->value;
  512. // 5. Assert: IsAccessorDescriptor(desc) is true.
  513. VERIFY(descriptor->is_accessor_descriptor());
  514. // 6. Let getter be desc.[[Get]].
  515. auto* getter = *descriptor->get;
  516. // 7. If getter is undefined, return undefined.
  517. if (!getter)
  518. return js_undefined();
  519. // 8. Return ? Call(getter, Receiver).
  520. return TRY(vm.call(*getter, receiver));
  521. }
  522. // 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
  523. ThrowCompletionOr<bool> Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
  524. {
  525. VERIFY(!value.is_empty());
  526. VERIFY(!receiver.is_empty());
  527. // 1. Assert: IsPropertyKey(P) is true.
  528. VERIFY(property_name.is_valid());
  529. // 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
  530. auto own_descriptor = TRY(internal_get_own_property(property_name));
  531. // 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  532. return ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
  533. }
  534. // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  535. ThrowCompletionOr<bool> Object::ordinary_set_with_own_descriptor(PropertyName const& property_name, Value value, Value receiver, Optional<PropertyDescriptor> own_descriptor)
  536. {
  537. auto& vm = this->vm();
  538. // 1. Assert: IsPropertyKey(P) is true.
  539. VERIFY(property_name.is_valid());
  540. // 2. If ownDesc is undefined, then
  541. if (!own_descriptor.has_value()) {
  542. // a. Let parent be ? O.[[GetPrototypeOf]]().
  543. auto* parent = TRY(internal_get_prototype_of());
  544. // b. If parent is not null, then
  545. if (parent) {
  546. // i. Return ? parent.[[Set]](P, V, Receiver).
  547. return TRY(parent->internal_set(property_name, value, receiver));
  548. }
  549. // c. Else,
  550. else {
  551. // i. Set ownDesc to the PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  552. own_descriptor = PropertyDescriptor {
  553. .value = js_undefined(),
  554. .writable = true,
  555. .enumerable = true,
  556. .configurable = true,
  557. };
  558. }
  559. }
  560. // 3. If IsDataDescriptor(ownDesc) is true, then
  561. if (own_descriptor->is_data_descriptor()) {
  562. // a. If ownDesc.[[Writable]] is false, return false.
  563. if (!*own_descriptor->writable)
  564. return false;
  565. // b. If Type(Receiver) is not Object, return false.
  566. if (!receiver.is_object())
  567. return false;
  568. // c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P).
  569. auto existing_descriptor = TRY(receiver.as_object().internal_get_own_property(property_name));
  570. // d. If existingDescriptor is not undefined, then
  571. if (existing_descriptor.has_value()) {
  572. // i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
  573. if (existing_descriptor->is_accessor_descriptor())
  574. return false;
  575. // ii. If existingDescriptor.[[Writable]] is false, return false.
  576. if (!*existing_descriptor->writable)
  577. return false;
  578. // iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }.
  579. auto value_descriptor = PropertyDescriptor { .value = value };
  580. // iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
  581. return TRY(receiver.as_object().internal_define_own_property(property_name, value_descriptor));
  582. }
  583. // e. Else,
  584. else {
  585. // i. Assert: Receiver does not currently have a property P.
  586. VERIFY(!receiver.as_object().storage_has(property_name));
  587. // ii. Return ? CreateDataProperty(Receiver, P, V).
  588. return TRY(receiver.as_object().create_data_property(property_name, value));
  589. }
  590. }
  591. // 4. Assert: IsAccessorDescriptor(ownDesc) is true.
  592. VERIFY(own_descriptor->is_accessor_descriptor());
  593. // 5. Let setter be ownDesc.[[Set]].
  594. auto* setter = *own_descriptor->set;
  595. // 6. If setter is undefined, return false.
  596. if (!setter)
  597. return false;
  598. // 7. Perform ? Call(setter, Receiver, « V »).
  599. (void)TRY(vm.call(*setter, receiver, value));
  600. // 8. Return true.
  601. return true;
  602. }
  603. // 10.1.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-delete-p
  604. ThrowCompletionOr<bool> Object::internal_delete(PropertyName const& property_name)
  605. {
  606. // 1. Assert: IsPropertyKey(P) is true.
  607. VERIFY(property_name.is_valid());
  608. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  609. auto descriptor = TRY(internal_get_own_property(property_name));
  610. // 3. If desc is undefined, return true.
  611. if (!descriptor.has_value())
  612. return true;
  613. // 4. If desc.[[Configurable]] is true, then
  614. if (*descriptor->configurable) {
  615. // a. Remove the own property with name P from O.
  616. storage_delete(property_name);
  617. // b. Return true.
  618. return true;
  619. }
  620. // 5. Return false.
  621. return false;
  622. }
  623. // 10.1.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
  624. ThrowCompletionOr<MarkedValueList> Object::internal_own_property_keys() const
  625. {
  626. auto& vm = this->vm();
  627. // 1. Let keys be a new empty List.
  628. MarkedValueList keys { heap() };
  629. // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
  630. for (auto& entry : m_indexed_properties) {
  631. // a. Add P as the last element of keys.
  632. keys.append(js_string(vm, String::number(entry.index())));
  633. }
  634. // 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
  635. for (auto& it : shape().property_table_ordered()) {
  636. if (it.key.is_string()) {
  637. // a. Add P as the last element of keys.
  638. keys.append(it.key.to_value(vm));
  639. }
  640. }
  641. // 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  642. for (auto& it : shape().property_table_ordered()) {
  643. if (it.key.is_symbol()) {
  644. // a. Add P as the last element of keys.
  645. keys.append(it.key.to_value(vm));
  646. }
  647. }
  648. // 5. Return keys.
  649. return { move(keys) };
  650. }
  651. // 10.4.7.2 SetImmutablePrototype ( O, V ), https://tc39.es/ecma262/#sec-set-immutable-prototype
  652. ThrowCompletionOr<bool> Object::set_immutable_prototype(Object* prototype)
  653. {
  654. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  655. // 2. Let current be ? O.[[GetPrototypeOf]]().
  656. auto* current = TRY(internal_get_prototype_of());
  657. // 3. If SameValue(V, current) is true, return true.
  658. if (prototype == current)
  659. return true;
  660. // 4. Return false.
  661. return false;
  662. }
  663. Optional<ValueAndAttributes> Object::storage_get(PropertyName const& property_name) const
  664. {
  665. VERIFY(property_name.is_valid());
  666. Value value;
  667. PropertyAttributes attributes;
  668. if (property_name.is_number()) {
  669. auto value_and_attributes = m_indexed_properties.get(property_name.as_number());
  670. if (!value_and_attributes.has_value())
  671. return {};
  672. value = value_and_attributes->value;
  673. attributes = value_and_attributes->attributes;
  674. } else {
  675. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  676. if (!metadata.has_value())
  677. return {};
  678. value = m_storage[metadata->offset];
  679. attributes = metadata->attributes;
  680. }
  681. return ValueAndAttributes { .value = value, .attributes = attributes };
  682. }
  683. bool Object::storage_has(PropertyName const& property_name) const
  684. {
  685. VERIFY(property_name.is_valid());
  686. if (property_name.is_number())
  687. return m_indexed_properties.has_index(property_name.as_number());
  688. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  689. }
  690. void Object::storage_set(PropertyName const& property_name, ValueAndAttributes const& value_and_attributes)
  691. {
  692. VERIFY(property_name.is_valid());
  693. auto [value, attributes] = value_and_attributes;
  694. if (property_name.is_number()) {
  695. auto index = property_name.as_number();
  696. m_indexed_properties.put(index, value, attributes);
  697. return;
  698. }
  699. auto property_name_string_or_symbol = property_name.to_string_or_symbol();
  700. auto metadata = shape().lookup(property_name_string_or_symbol);
  701. if (!metadata.has_value()) {
  702. if (!m_shape->is_unique() && shape().property_count() > 100) {
  703. // If you add more than 100 properties to an object, let's stop doing
  704. // transitions to avoid filling up the heap with shapes.
  705. ensure_shape_is_unique();
  706. }
  707. if (m_shape->is_unique())
  708. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  709. else
  710. set_shape(*m_shape->create_put_transition(property_name_string_or_symbol, attributes));
  711. m_storage.append(value);
  712. return;
  713. }
  714. if (attributes != metadata->attributes) {
  715. if (m_shape->is_unique())
  716. m_shape->reconfigure_property_in_unique_shape(property_name_string_or_symbol, attributes);
  717. else
  718. set_shape(*m_shape->create_configure_transition(property_name_string_or_symbol, attributes));
  719. }
  720. m_storage[metadata->offset] = value;
  721. }
  722. void Object::storage_delete(PropertyName const& property_name)
  723. {
  724. VERIFY(property_name.is_valid());
  725. VERIFY(storage_has(property_name));
  726. if (property_name.is_number())
  727. return m_indexed_properties.remove(property_name.as_number());
  728. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  729. VERIFY(metadata.has_value());
  730. ensure_shape_is_unique();
  731. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), metadata->offset);
  732. m_storage.remove(metadata->offset);
  733. }
  734. void Object::set_prototype(Object* new_prototype)
  735. {
  736. if (prototype() == new_prototype)
  737. return;
  738. auto& shape = this->shape();
  739. if (shape.is_unique())
  740. shape.set_prototype_without_transition(new_prototype);
  741. else
  742. m_shape = shape.create_prototype_transition(new_prototype);
  743. }
  744. void Object::define_old_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  745. {
  746. Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> completion_getter = {};
  747. if (getter) {
  748. completion_getter = [getter = move(getter)](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
  749. auto result = getter(vm, global_object);
  750. if (auto* exception = vm.exception())
  751. return throw_completion(exception->value());
  752. return result;
  753. };
  754. }
  755. Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> completion_setter = {};
  756. if (setter) {
  757. completion_setter = [setter = move(setter)](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
  758. auto result = setter(vm, global_object);
  759. if (auto* exception = vm.exception())
  760. return throw_completion(exception->value());
  761. return result;
  762. };
  763. }
  764. define_native_accessor(property_name, move(completion_getter), move(completion_setter), attribute);
  765. }
  766. void Object::define_native_accessor(PropertyName const& property_name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> getter, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  767. {
  768. auto& vm = this->vm();
  769. String formatted_property_name;
  770. if (property_name.is_number()) {
  771. formatted_property_name = property_name.to_string();
  772. } else if (property_name.is_string()) {
  773. formatted_property_name = property_name.as_string();
  774. } else {
  775. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  776. }
  777. FunctionObject* getter_function = nullptr;
  778. if (getter) {
  779. auto name = String::formatted("get {}", formatted_property_name);
  780. getter_function = NativeFunction::create(global_object(), name, move(getter));
  781. getter_function->define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  782. getter_function->define_direct_property(vm.names.name, js_string(vm, name), Attribute::Configurable);
  783. }
  784. FunctionObject* setter_function = nullptr;
  785. if (setter) {
  786. auto name = String::formatted("set {}", formatted_property_name);
  787. setter_function = NativeFunction::create(global_object(), name, move(setter));
  788. setter_function->define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  789. setter_function->define_direct_property(vm.names.name, js_string(vm, name), Attribute::Configurable);
  790. }
  791. return define_direct_accessor(property_name, getter_function, setter_function, attribute);
  792. }
  793. void Object::define_direct_accessor(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  794. {
  795. VERIFY(property_name.is_valid());
  796. auto existing_property = storage_get(property_name).value_or({}).value;
  797. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  798. if (!accessor) {
  799. accessor = Accessor::create(vm(), getter, setter);
  800. define_direct_property(property_name, accessor, attributes);
  801. } else {
  802. if (getter)
  803. accessor->set_getter(getter);
  804. if (setter)
  805. accessor->set_setter(setter);
  806. }
  807. }
  808. void Object::ensure_shape_is_unique()
  809. {
  810. if (shape().is_unique())
  811. return;
  812. m_shape = m_shape->create_unique_clone();
  813. }
  814. // Simple side-effect free property lookup, following the prototype chain. Non-standard.
  815. Value Object::get_without_side_effects(const PropertyName& property_name) const
  816. {
  817. auto* object = this;
  818. while (object) {
  819. auto value_and_attributes = object->storage_get(property_name);
  820. if (value_and_attributes.has_value())
  821. return value_and_attributes->value;
  822. object = object->prototype();
  823. }
  824. return {};
  825. }
  826. void Object::define_old_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  827. {
  828. auto completion_native_function = [native_function = move(native_function), property_name](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
  829. auto result = native_function(vm, global_object);
  830. if (auto* exception = vm.exception())
  831. return throw_completion(exception->value());
  832. return result;
  833. };
  834. define_native_function(property_name, move(completion_native_function), length, attribute);
  835. }
  836. void Object::define_native_function(PropertyName const& property_name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  837. {
  838. auto& vm = this->vm();
  839. String function_name;
  840. if (property_name.is_string()) {
  841. function_name = property_name.as_string();
  842. } else {
  843. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  844. }
  845. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  846. function->define_direct_property(vm.names.length, Value(length), Attribute::Configurable);
  847. function->define_direct_property(vm.names.name, js_string(vm, function_name), Attribute::Configurable);
  848. define_direct_property(property_name, function, attribute);
  849. }
  850. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  851. ThrowCompletionOr<Object*> Object::define_properties(Value properties)
  852. {
  853. auto& global_object = this->global_object();
  854. // 1. Assert: Type(O) is Object.
  855. // 2. Let props be ? ToObject(Properties).
  856. auto* props = TRY(properties.to_object(global_object));
  857. // 3. Let keys be ? props.[[OwnPropertyKeys]]().
  858. auto keys = TRY(props->internal_own_property_keys());
  859. struct NameAndDescriptor {
  860. PropertyName name;
  861. PropertyDescriptor descriptor;
  862. };
  863. // 4. Let descriptors be a new empty List.
  864. Vector<NameAndDescriptor> descriptors;
  865. // 5. For each element nextKey of keys, do
  866. for (auto& next_key : keys) {
  867. auto property_name = PropertyName::from_value(global_object, next_key);
  868. // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
  869. auto property_descriptor = TRY(props->internal_get_own_property(property_name));
  870. // b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  871. if (property_descriptor.has_value() && *property_descriptor->enumerable) {
  872. // i. Let descObj be ? Get(props, nextKey).
  873. auto descriptor_object = TRY(props->get(property_name));
  874. // ii. Let desc be ? ToPropertyDescriptor(descObj).
  875. auto descriptor = TRY(to_property_descriptor(global_object, descriptor_object));
  876. // iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
  877. descriptors.append({ property_name, descriptor });
  878. }
  879. }
  880. // 6. For each element pair of descriptors, do
  881. for (auto& [name, descriptor] : descriptors) {
  882. // a. Let P be the first element of pair.
  883. // b. Let desc be the second element of pair.
  884. // c. Perform ? DefinePropertyOrThrow(O, P, desc).
  885. TRY(define_property_or_throw(name, descriptor));
  886. }
  887. // 7. Return O.
  888. return this;
  889. }
  890. void Object::visit_edges(Cell::Visitor& visitor)
  891. {
  892. Cell::visit_edges(visitor);
  893. visitor.visit(m_shape);
  894. for (auto& value : m_storage)
  895. visitor.visit(value);
  896. m_indexed_properties.for_each_value([&visitor](auto& value) {
  897. visitor.visit(value);
  898. });
  899. }
  900. // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive
  901. ThrowCompletionOr<Value> Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  902. {
  903. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  904. auto& vm = this->vm();
  905. AK::Array<PropertyName, 2> method_names;
  906. // 1. If hint is string, then
  907. if (preferred_type == Value::PreferredType::String) {
  908. // a. Let methodNames be « "toString", "valueOf" ».
  909. method_names = { vm.names.toString, vm.names.valueOf };
  910. } else {
  911. // a. Let methodNames be « "valueOf", "toString" ».
  912. method_names = { vm.names.valueOf, vm.names.toString };
  913. }
  914. // 3. For each element name of methodNames, do
  915. for (auto& method_name : method_names) {
  916. // a. Let method be ? Get(O, name).
  917. auto method = TRY(get(method_name));
  918. // b. If IsCallable(method) is true, then
  919. if (method.is_function()) {
  920. // i. Let result be ? Call(method, O).
  921. auto result = TRY(vm.call(method.as_function(), const_cast<Object*>(this)));
  922. // ii. If Type(result) is not Object, return result.
  923. if (!result.is_object())
  924. return result;
  925. }
  926. }
  927. // 4. Throw a TypeError exception.
  928. return vm.throw_completion<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  929. }
  930. }