Object.cpp 44 KB

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