Object.cpp 42 KB

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