Object.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. 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 = internal_get_prototype_of();
  512. if (vm.exception())
  513. return {};
  514. // 5. If parent is not null, then
  515. if (parent) {
  516. // a. Return ? parent.[[HasProperty]](P).
  517. return parent->internal_has_property(property_name);
  518. }
  519. // 6. Return false.
  520. return false;
  521. }
  522. // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
  523. Value Object::internal_get(PropertyName const& property_name, Value receiver) const
  524. {
  525. VERIFY(!receiver.is_empty());
  526. auto& vm = this->vm();
  527. // 1. Assert: IsPropertyKey(P) is true.
  528. VERIFY(property_name.is_valid());
  529. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  530. auto descriptor = internal_get_own_property(property_name);
  531. if (vm.exception())
  532. return {};
  533. // 3. If desc is undefined, then
  534. if (!descriptor.has_value()) {
  535. // a. Let parent be ? O.[[GetPrototypeOf]]().
  536. auto parent = internal_get_prototype_of();
  537. if (vm.exception())
  538. return {};
  539. // b. If parent is null, return undefined.
  540. if (!parent)
  541. return js_undefined();
  542. // c. Return ? parent.[[Get]](P, Receiver).
  543. return parent->internal_get(property_name, receiver);
  544. }
  545. // 4. If IsDataDescriptor(desc) is true, return desc.[[Value]].
  546. if (descriptor->is_data_descriptor())
  547. return *descriptor->value;
  548. // 5. Assert: IsAccessorDescriptor(desc) is true.
  549. VERIFY(descriptor->is_accessor_descriptor());
  550. // 6. Let getter be desc.[[Get]].
  551. auto* getter = *descriptor->get;
  552. // 7. If getter is undefined, return undefined.
  553. if (!getter)
  554. return js_undefined();
  555. // 8. Return ? Call(getter, Receiver).
  556. return vm.call(*getter, receiver);
  557. }
  558. static bool ordinary_set_with_own_descriptor(Object&, PropertyName const&, Value, Value, Optional<PropertyDescriptor>);
  559. // 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
  560. bool Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
  561. {
  562. VERIFY(!value.is_empty());
  563. VERIFY(!receiver.is_empty());
  564. auto& vm = this->vm();
  565. // 1. Assert: IsPropertyKey(P) is true.
  566. VERIFY(property_name.is_valid());
  567. // 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
  568. auto own_descriptor = internal_get_own_property(property_name);
  569. if (vm.exception())
  570. return {};
  571. // 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  572. return ordinary_set_with_own_descriptor(*this, property_name, value, receiver, own_descriptor);
  573. }
  574. // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  575. bool ordinary_set_with_own_descriptor(Object& object, PropertyName const& property_name, Value value, Value receiver, Optional<PropertyDescriptor> own_descriptor)
  576. {
  577. auto& vm = object.vm();
  578. // 1. Assert: IsPropertyKey(P) is true.
  579. VERIFY(property_name.is_valid());
  580. // 2. If ownDesc is undefined, then
  581. if (!own_descriptor.has_value()) {
  582. // a. Let parent be ? O.[[GetPrototypeOf]]().
  583. auto parent = object.internal_get_prototype_of();
  584. if (vm.exception())
  585. return {};
  586. // b. If parent is not null, then
  587. if (parent) {
  588. // i. Return ? parent.[[Set]](P, V, Receiver).
  589. return parent->internal_set(property_name, value, receiver);
  590. }
  591. // c. Else,
  592. else {
  593. // i. Set ownDesc to the PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  594. own_descriptor = PropertyDescriptor {
  595. .value = js_undefined(),
  596. .writable = true,
  597. .enumerable = true,
  598. .configurable = true,
  599. };
  600. }
  601. }
  602. // 3. If IsDataDescriptor(ownDesc) is true, then
  603. if (own_descriptor->is_data_descriptor()) {
  604. // a. If ownDesc.[[Writable]] is false, return false.
  605. if (!*own_descriptor->writable)
  606. return false;
  607. // b. If Type(Receiver) is not Object, return false.
  608. if (!receiver.is_object())
  609. return false;
  610. // c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P).
  611. auto existing_descriptor = receiver.as_object().internal_get_own_property(property_name);
  612. if (vm.exception())
  613. return {};
  614. // d. If existingDescriptor is not undefined, then
  615. if (existing_descriptor.has_value()) {
  616. // i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
  617. if (existing_descriptor->is_accessor_descriptor())
  618. return false;
  619. // ii. If existingDescriptor.[[Writable]] is false, return false.
  620. if (!*existing_descriptor->writable)
  621. return false;
  622. // iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }.
  623. auto value_descriptor = PropertyDescriptor { .value = value };
  624. // iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
  625. return receiver.as_object().internal_define_own_property(property_name, value_descriptor);
  626. }
  627. // e. Else,
  628. else {
  629. // i. Assert: Receiver does not currently have a property P.
  630. VERIFY(!receiver.as_object().storage_has(property_name));
  631. // ii. Return ? CreateDataProperty(Receiver, P, V).
  632. return receiver.as_object().create_data_property(property_name, value);
  633. }
  634. }
  635. // 4. Assert: IsAccessorDescriptor(ownDesc) is true.
  636. VERIFY(own_descriptor->is_accessor_descriptor());
  637. // 5. Let setter be ownDesc.[[Set]].
  638. auto* setter = *own_descriptor->set;
  639. // 6. If setter is undefined, return false.
  640. if (!setter)
  641. return false;
  642. // 7. Perform ? Call(setter, Receiver, « V »).
  643. (void)vm.call(*setter, receiver, value);
  644. // 8. Return true.
  645. return true;
  646. }
  647. // 10.1.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-delete-p
  648. bool Object::internal_delete(PropertyName const& property_name)
  649. {
  650. auto& vm = this->vm();
  651. // 1. Assert: IsPropertyKey(P) is true.
  652. VERIFY(property_name.is_valid());
  653. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  654. auto descriptor = internal_get_own_property(property_name);
  655. if (vm.exception())
  656. return {};
  657. // 3. If desc is undefined, return true.
  658. if (!descriptor.has_value())
  659. return true;
  660. // 4. If desc.[[Configurable]] is true, then
  661. if (*descriptor->configurable) {
  662. // a. Remove the own property with name P from O.
  663. storage_delete(property_name);
  664. // b. Return true.
  665. return true;
  666. }
  667. // 5. Return false.
  668. return false;
  669. }
  670. // 10.1.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
  671. MarkedValueList Object::internal_own_property_keys() const
  672. {
  673. auto& vm = this->vm();
  674. // 1. Let keys be a new empty List.
  675. MarkedValueList keys { heap() };
  676. // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
  677. for (auto& entry : m_indexed_properties) {
  678. // a. Add P as the last element of keys.
  679. keys.append(js_string(vm, String::number(entry.index())));
  680. }
  681. // 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
  682. for (auto& it : shape().property_table_ordered()) {
  683. if (it.key.is_string()) {
  684. // a. Add P as the last element of keys.
  685. keys.append(it.key.to_value(vm));
  686. }
  687. }
  688. // 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  689. for (auto& it : shape().property_table_ordered()) {
  690. if (it.key.is_symbol()) {
  691. // a. Add P as the last element of keys.
  692. keys.append(it.key.to_value(vm));
  693. }
  694. }
  695. // 5. Return keys.
  696. return keys;
  697. }
  698. // 10.4.7.2 SetImmutablePrototype ( O, V ), https://tc39.es/ecma262/#sec-set-immutable-prototype
  699. bool Object::set_immutable_prototype(Object* prototype)
  700. {
  701. auto& vm = this->vm();
  702. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  703. // 2. Let current be ? O.[[GetPrototypeOf]]().
  704. auto* current = internal_get_prototype_of();
  705. if (vm.exception())
  706. return {};
  707. // 3. If SameValue(V, current) is true, return true.
  708. if (prototype == current)
  709. return true;
  710. // 4. Return false.
  711. return false;
  712. }
  713. Optional<ValueAndAttributes> Object::storage_get(PropertyName const& property_name) const
  714. {
  715. VERIFY(property_name.is_valid());
  716. Value value;
  717. PropertyAttributes attributes;
  718. if (property_name.is_number()) {
  719. auto value_and_attributes = m_indexed_properties.get(property_name.as_number());
  720. if (!value_and_attributes.has_value())
  721. return {};
  722. value = value_and_attributes->value;
  723. attributes = value_and_attributes->attributes;
  724. } else {
  725. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  726. if (!metadata.has_value())
  727. return {};
  728. value = m_storage[metadata->offset];
  729. attributes = metadata->attributes;
  730. }
  731. return ValueAndAttributes { .value = value, .attributes = attributes };
  732. }
  733. bool Object::storage_has(PropertyName const& property_name) const
  734. {
  735. VERIFY(property_name.is_valid());
  736. if (property_name.is_number())
  737. return m_indexed_properties.has_index(property_name.as_number());
  738. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  739. }
  740. void Object::storage_set(PropertyName const& property_name, ValueAndAttributes const& value_and_attributes)
  741. {
  742. VERIFY(property_name.is_valid());
  743. auto [value, attributes] = value_and_attributes;
  744. if (property_name.is_number()) {
  745. auto index = property_name.as_number();
  746. m_indexed_properties.put(index, value, attributes);
  747. return;
  748. }
  749. auto property_name_string_or_symbol = property_name.to_string_or_symbol();
  750. // NOTE: We don't do transitions or check for attribute changes during object initialization,
  751. // which makes building common runtime objects significantly faster. Transitions are primarily
  752. // interesting when scripts add properties to objects.
  753. if (!m_initialized) {
  754. if (m_shape->is_unique())
  755. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  756. else
  757. m_shape->add_property_without_transition(property_name_string_or_symbol, attributes);
  758. m_storage.append(value);
  759. return;
  760. }
  761. auto metadata = shape().lookup(property_name_string_or_symbol);
  762. if (!metadata.has_value()) {
  763. if (!m_shape->is_unique() && shape().property_count() > 100) {
  764. // If you add more than 100 properties to an object, let's stop doing
  765. // transitions to avoid filling up the heap with shapes.
  766. ensure_shape_is_unique();
  767. }
  768. if (m_shape->is_unique())
  769. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  770. else if (!m_transitions_enabled)
  771. m_shape->add_property_without_transition(property_name_string_or_symbol, attributes);
  772. else
  773. set_shape(*m_shape->create_put_transition(property_name_string_or_symbol, attributes));
  774. m_storage.append(value);
  775. return;
  776. }
  777. if (attributes != metadata->attributes) {
  778. if (m_shape->is_unique())
  779. m_shape->reconfigure_property_in_unique_shape(property_name_string_or_symbol, attributes);
  780. else if (!m_transitions_enabled)
  781. VERIFY_NOT_REACHED(); // We currently don't have a way of doing this, and it's not used anywhere either.
  782. else
  783. set_shape(*m_shape->create_configure_transition(property_name_string_or_symbol, attributes));
  784. }
  785. m_storage[metadata->offset] = value;
  786. }
  787. void Object::storage_delete(PropertyName const& property_name)
  788. {
  789. VERIFY(property_name.is_valid());
  790. VERIFY(storage_has(property_name));
  791. if (property_name.is_number())
  792. return m_indexed_properties.remove(property_name.as_number());
  793. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  794. VERIFY(metadata.has_value());
  795. ensure_shape_is_unique();
  796. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), metadata->offset);
  797. m_storage.remove(metadata->offset);
  798. }
  799. void Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  800. {
  801. auto& vm = this->vm();
  802. String formatted_property_name;
  803. if (property_name.is_number()) {
  804. formatted_property_name = property_name.to_string();
  805. } else if (property_name.is_string()) {
  806. formatted_property_name = property_name.as_string();
  807. } else {
  808. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  809. }
  810. FunctionObject* getter_function = nullptr;
  811. if (getter) {
  812. auto name = String::formatted("get {}", formatted_property_name);
  813. getter_function = NativeFunction::create(global_object(), name, move(getter));
  814. getter_function->define_direct_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  815. getter_function->define_direct_property_without_transition(vm.names.name, js_string(vm, name), Attribute::Configurable);
  816. }
  817. FunctionObject* setter_function = nullptr;
  818. if (setter) {
  819. auto name = String::formatted("set {}", formatted_property_name);
  820. setter_function = NativeFunction::create(global_object(), name, move(setter));
  821. setter_function->define_direct_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  822. setter_function->define_direct_property_without_transition(vm.names.name, js_string(vm, name), Attribute::Configurable);
  823. }
  824. return define_direct_accessor(property_name, getter_function, setter_function, attribute);
  825. }
  826. void Object::define_direct_accessor(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  827. {
  828. VERIFY(property_name.is_valid());
  829. auto existing_property = storage_get(property_name).value_or({}).value;
  830. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  831. if (!accessor) {
  832. accessor = Accessor::create(vm(), getter, setter);
  833. define_direct_property(property_name, accessor, attributes);
  834. } else {
  835. if (getter)
  836. accessor->set_getter(getter);
  837. if (setter)
  838. accessor->set_setter(setter);
  839. }
  840. }
  841. void Object::define_direct_property_without_transition(PropertyName const& property_name, Value value, PropertyAttributes attributes)
  842. {
  843. TemporaryChange disable_transitions(m_transitions_enabled, false);
  844. define_direct_property(property_name, value, attributes);
  845. }
  846. void Object::define_direct_accessor_without_transition(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes)
  847. {
  848. TemporaryChange disable_transitions(m_transitions_enabled, false);
  849. define_direct_accessor(property_name, getter, setter, attributes);
  850. }
  851. void Object::ensure_shape_is_unique()
  852. {
  853. if (shape().is_unique())
  854. return;
  855. m_shape = m_shape->create_unique_clone();
  856. }
  857. // Simple side-effect free property lookup, following the prototype chain. Non-standard.
  858. Value Object::get_without_side_effects(const PropertyName& property_name) const
  859. {
  860. auto* object = this;
  861. while (object) {
  862. auto value_and_attributes = object->storage_get(property_name);
  863. if (value_and_attributes.has_value())
  864. return value_and_attributes->value;
  865. object = object->prototype();
  866. }
  867. return {};
  868. }
  869. void Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  870. {
  871. auto& vm = this->vm();
  872. String function_name;
  873. if (property_name.is_string()) {
  874. function_name = property_name.as_string();
  875. } else {
  876. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  877. }
  878. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  879. function->define_direct_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  880. function->define_direct_property_without_transition(vm.names.name, js_string(vm, function_name), Attribute::Configurable);
  881. define_direct_property(property_name, function, attribute);
  882. }
  883. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  884. Object* Object::define_properties(Value properties)
  885. {
  886. auto& vm = this->vm();
  887. auto& global_object = this->global_object();
  888. // 1. Assert: Type(O) is Object.
  889. // 2. Let props be ? ToObject(Properties).
  890. auto* props = properties.to_object(global_object);
  891. if (vm.exception())
  892. return {};
  893. // 3. Let keys be ? props.[[OwnPropertyKeys]]().
  894. auto keys = props->internal_own_property_keys();
  895. if (vm.exception())
  896. return {};
  897. struct NameAndDescriptor {
  898. PropertyName name;
  899. PropertyDescriptor descriptor;
  900. };
  901. // 4. Let descriptors be a new empty List.
  902. Vector<NameAndDescriptor> descriptors;
  903. // 5. For each element nextKey of keys, do
  904. for (auto& next_key : keys) {
  905. auto property_name = PropertyName::from_value(global_object, next_key);
  906. // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
  907. auto property_descriptor = props->internal_get_own_property(property_name);
  908. if (vm.exception())
  909. return {};
  910. // b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  911. if (property_descriptor.has_value() && *property_descriptor->enumerable) {
  912. // i. Let descObj be ? Get(props, nextKey).
  913. auto descriptor_object = props->get(property_name);
  914. if (vm.exception())
  915. return {};
  916. // ii. Let desc be ? ToPropertyDescriptor(descObj).
  917. auto descriptor = to_property_descriptor(global_object, descriptor_object);
  918. if (vm.exception())
  919. return {};
  920. // iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
  921. descriptors.append({ property_name, descriptor });
  922. }
  923. }
  924. // 6. For each element pair of descriptors, do
  925. for (auto& [name, descriptor] : descriptors) {
  926. // a. Let P be the first element of pair.
  927. // b. Let desc be the second element of pair.
  928. // c. Perform ? DefinePropertyOrThrow(O, P, desc).
  929. define_property_or_throw(name, descriptor);
  930. if (vm.exception())
  931. return {};
  932. }
  933. // 7. Return O.
  934. return this;
  935. }
  936. void Object::visit_edges(Cell::Visitor& visitor)
  937. {
  938. Cell::visit_edges(visitor);
  939. visitor.visit(m_shape);
  940. for (auto& value : m_storage)
  941. visitor.visit(value);
  942. m_indexed_properties.for_each_value([&visitor](auto& value) {
  943. visitor.visit(value);
  944. });
  945. }
  946. // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive
  947. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  948. {
  949. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  950. auto& vm = this->vm();
  951. AK::Array<PropertyName, 2> method_names;
  952. if (preferred_type == Value::PreferredType::String)
  953. method_names = { vm.names.toString, vm.names.valueOf };
  954. else
  955. method_names = { vm.names.valueOf, vm.names.toString };
  956. for (auto& method_name : method_names) {
  957. auto method = get(method_name);
  958. if (vm.exception())
  959. return {};
  960. if (method.is_function()) {
  961. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  962. if (!result.is_object())
  963. return result;
  964. }
  965. }
  966. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  967. return {};
  968. }
  969. }