Object.cpp 42 KB

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