Object.cpp 41 KB

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