Object.cpp 41 KB

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