Object.cpp 42 KB

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