Object.cpp 41 KB

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