Object.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/Debug.h>
  8. #include <AK/String.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <LibJS/Heap/Heap.h>
  11. #include <LibJS/Interpreter.h>
  12. #include <LibJS/Runtime/Accessor.h>
  13. #include <LibJS/Runtime/Array.h>
  14. #include <LibJS/Runtime/Error.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/NativeFunction.h>
  17. #include <LibJS/Runtime/NativeProperty.h>
  18. #include <LibJS/Runtime/Object.h>
  19. #include <LibJS/Runtime/ProxyObject.h>
  20. #include <LibJS/Runtime/Shape.h>
  21. #include <LibJS/Runtime/StringObject.h>
  22. #include <LibJS/Runtime/TemporaryClearException.h>
  23. #include <LibJS/Runtime/Value.h>
  24. namespace JS {
  25. PropertyDescriptor PropertyDescriptor::from_dictionary(VM& vm, const Object& object)
  26. {
  27. PropertyAttributes attributes;
  28. if (object.has_property(vm.names.configurable)) {
  29. attributes.set_has_configurable();
  30. if (object.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  31. attributes.set_configurable();
  32. if (vm.exception())
  33. return {};
  34. }
  35. if (object.has_property(vm.names.enumerable)) {
  36. attributes.set_has_enumerable();
  37. if (object.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  38. attributes.set_enumerable();
  39. if (vm.exception())
  40. return {};
  41. }
  42. if (object.has_property(vm.names.writable)) {
  43. attributes.set_has_writable();
  44. if (object.get(vm.names.writable).value_or(Value(false)).to_boolean())
  45. attributes.set_writable();
  46. if (vm.exception())
  47. return {};
  48. }
  49. PropertyDescriptor descriptor { attributes, object.get(vm.names.value), nullptr, nullptr };
  50. if (vm.exception())
  51. return {};
  52. auto getter = object.get(vm.names.get);
  53. if (vm.exception())
  54. return {};
  55. if (getter.is_function())
  56. descriptor.getter = &getter.as_function();
  57. auto setter = object.get(vm.names.set);
  58. if (vm.exception())
  59. return {};
  60. if (setter.is_function())
  61. descriptor.setter = &setter.as_function();
  62. return descriptor;
  63. }
  64. // 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  65. Object* Object::create(GlobalObject& global_object, Object* prototype)
  66. {
  67. if (!prototype)
  68. return global_object.heap().allocate<Object>(global_object, *global_object.empty_object_shape());
  69. else if (prototype == global_object.object_prototype())
  70. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  71. else
  72. return global_object.heap().allocate<Object>(global_object, *prototype);
  73. }
  74. Object::Object(GlobalObjectTag)
  75. {
  76. // This is the global object
  77. m_shape = heap().allocate_without_global_object<Shape>(*this);
  78. }
  79. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  80. {
  81. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  82. }
  83. Object::Object(Object& prototype)
  84. {
  85. m_shape = prototype.global_object().empty_object_shape();
  86. set_prototype(&prototype);
  87. }
  88. Object::Object(Shape& shape)
  89. : m_shape(&shape)
  90. {
  91. m_storage.resize(shape.property_count());
  92. }
  93. void Object::initialize(GlobalObject&)
  94. {
  95. }
  96. Object::~Object()
  97. {
  98. }
  99. Object* Object::prototype()
  100. {
  101. return shape().prototype();
  102. }
  103. const Object* Object::prototype() const
  104. {
  105. return shape().prototype();
  106. }
  107. // 10.1.2.1 OrdinarySetPrototypeOf ( O, V ), https://tc39.es/ecma262/#sec-ordinarysetprototypeof
  108. bool Object::set_prototype(Object* new_prototype)
  109. {
  110. if (prototype() == new_prototype)
  111. return true;
  112. if (!m_is_extensible)
  113. return false;
  114. auto* prototype = new_prototype;
  115. while (prototype) {
  116. if (prototype == this)
  117. return false;
  118. // NOTE: This is a best-effort implementation of the following step:
  119. // "If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1,
  120. // set done to true."
  121. // We don't have a good way of detecting whether certain virtual Object methods have been
  122. // overridden by a given object, but as ProxyObject is the only one doing that, this check
  123. // does the trick.
  124. if (is<ProxyObject>(prototype))
  125. break;
  126. prototype = prototype->prototype();
  127. }
  128. auto& shape = this->shape();
  129. if (shape.is_unique())
  130. shape.set_prototype_without_transition(new_prototype);
  131. else
  132. m_shape = shape.create_prototype_transition(new_prototype);
  133. return true;
  134. }
  135. bool Object::has_prototype(const Object* prototype) const
  136. {
  137. for (auto* object = this->prototype(); object; object = object->prototype()) {
  138. if (vm().exception())
  139. return false;
  140. if (object == prototype)
  141. return true;
  142. }
  143. return false;
  144. }
  145. bool Object::prevent_extensions()
  146. {
  147. m_is_extensible = false;
  148. return true;
  149. }
  150. // 7.3.15 SetIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-setintegritylevel
  151. bool Object::set_integrity_level(IntegrityLevel level)
  152. {
  153. // FIXME: This feels clunky and should get nicer abstractions.
  154. auto update_property = [this](auto& property_name, auto new_attributes) {
  155. if (property_name.is_number()) {
  156. auto value_and_attributes = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No).value();
  157. auto value = value_and_attributes.value;
  158. auto attributes = value_and_attributes.attributes.bits() & new_attributes;
  159. m_indexed_properties.put(nullptr, property_name.as_number(), value, attributes, AllowSideEffects::No);
  160. } else {
  161. auto metadata = shape().lookup(property_name.to_string_or_symbol()).value();
  162. auto attributes = metadata.attributes.bits() & new_attributes;
  163. if (m_shape->is_unique())
  164. m_shape->reconfigure_property_in_unique_shape(property_name.to_string_or_symbol(), attributes);
  165. else
  166. set_shape(*m_shape->create_configure_transition(property_name.to_string_or_symbol(), attributes));
  167. }
  168. };
  169. auto& vm = this->vm();
  170. auto status = prevent_extensions();
  171. if (vm.exception())
  172. return false;
  173. if (!status)
  174. return false;
  175. auto keys = get_own_properties(PropertyKind::Key);
  176. if (vm.exception())
  177. return false;
  178. switch (level) {
  179. case IntegrityLevel::Sealed:
  180. for (auto& key : keys) {
  181. auto property_name = PropertyName::from_value(global_object(), key);
  182. update_property(property_name, ~Attribute::Configurable);
  183. if (vm.exception())
  184. return {};
  185. }
  186. break;
  187. case IntegrityLevel::Frozen:
  188. for (auto& key : keys) {
  189. auto property_name = PropertyName::from_value(global_object(), key);
  190. auto property_descriptor = get_own_property_descriptor(property_name);
  191. if (!property_descriptor.has_value())
  192. continue;
  193. u8 attributes = property_descriptor->is_accessor_descriptor()
  194. ? ~Attribute::Configurable
  195. : ~Attribute::Configurable & ~Attribute::Writable;
  196. update_property(property_name, attributes);
  197. if (vm.exception())
  198. return {};
  199. }
  200. break;
  201. default:
  202. VERIFY_NOT_REACHED();
  203. }
  204. return true;
  205. }
  206. // 7.3.16 TestIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-testintegritylevel
  207. bool Object::test_integrity_level(IntegrityLevel level)
  208. {
  209. auto& vm = this->vm();
  210. auto extensible = is_extensible();
  211. if (vm.exception())
  212. return false;
  213. if (extensible)
  214. return false;
  215. auto keys = get_own_properties(PropertyKind::Key);
  216. if (vm.exception())
  217. return false;
  218. for (auto& key : keys) {
  219. auto property_name = PropertyName::from_value(global_object(), key);
  220. auto property_descriptor = get_own_property_descriptor(property_name);
  221. if (!property_descriptor.has_value())
  222. continue;
  223. if (property_descriptor->attributes.is_configurable())
  224. return false;
  225. if (level == IntegrityLevel::Frozen && property_descriptor->is_data_descriptor()) {
  226. if (property_descriptor->attributes.is_writable())
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. Value Object::get_own_property(const PropertyName& property_name, Value receiver, AllowSideEffects allow_side_effects) const
  233. {
  234. VERIFY(property_name.is_valid());
  235. Value value_here;
  236. if (property_name.is_number()) {
  237. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No);
  238. if (!existing_property.has_value())
  239. return {};
  240. value_here = existing_property.value().value.value_or(js_undefined());
  241. } else {
  242. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  243. if (!metadata.has_value())
  244. return {};
  245. value_here = m_storage[metadata.value().offset].value_or(js_undefined());
  246. }
  247. VERIFY(!value_here.is_empty());
  248. if (allow_side_effects == AllowSideEffects::Yes) {
  249. VERIFY(!receiver.is_empty());
  250. if (value_here.is_accessor())
  251. return value_here.as_accessor().call_getter(receiver);
  252. if (value_here.is_native_property())
  253. return call_native_property_getter(value_here.as_native_property(), receiver);
  254. }
  255. return value_here;
  256. }
  257. MarkedValueList Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
  258. {
  259. MarkedValueList properties(heap());
  260. // FIXME: Support generic iterables
  261. if (is<StringObject>(*this)) {
  262. auto str = static_cast<const StringObject&>(*this).primitive_string().string();
  263. for (size_t i = 0; i < str.length(); ++i) {
  264. if (kind == PropertyKind::Key) {
  265. properties.append(js_string(vm(), String::number(i)));
  266. } else if (kind == PropertyKind::Value) {
  267. properties.append(js_string(vm(), String::formatted("{:c}", str[i])));
  268. } else {
  269. auto* entry_array = Array::create(global_object());
  270. entry_array->define_property(0, js_string(vm(), String::number(i)));
  271. entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
  272. properties.append(entry_array);
  273. }
  274. if (vm().exception())
  275. return MarkedValueList { heap() };
  276. }
  277. }
  278. if (return_type != GetOwnPropertyReturnType::SymbolOnly) {
  279. for (auto& entry : m_indexed_properties) {
  280. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  281. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  282. continue;
  283. if (kind == PropertyKind::Key) {
  284. properties.append(js_string(vm(), String::number(entry.index())));
  285. } else if (kind == PropertyKind::Value) {
  286. properties.append(value_and_attributes.value);
  287. } else {
  288. auto* entry_array = Array::create(global_object());
  289. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  290. entry_array->define_property(1, value_and_attributes.value);
  291. properties.append(entry_array);
  292. }
  293. if (vm().exception())
  294. return MarkedValueList { heap() };
  295. }
  296. }
  297. auto add_property_to_results = [&](auto& property) {
  298. if (kind == PropertyKind::Key) {
  299. properties.append(property.key.to_value(vm()));
  300. } else if (kind == PropertyKind::Value) {
  301. properties.append(get(property.key));
  302. } else {
  303. auto* entry_array = Array::create(global_object());
  304. entry_array->define_property(0, property.key.to_value(vm()));
  305. entry_array->define_property(1, get(property.key));
  306. properties.append(entry_array);
  307. }
  308. };
  309. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  310. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  311. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  312. for (auto& it : shape().property_table_ordered()) {
  313. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  314. continue;
  315. if (it.key.is_symbol())
  316. continue;
  317. add_property_to_results(it);
  318. if (vm().exception())
  319. return MarkedValueList { heap() };
  320. }
  321. }
  322. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  323. for (auto& it : shape().property_table_ordered()) {
  324. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  325. continue;
  326. if (it.key.is_string())
  327. continue;
  328. add_property_to_results(it);
  329. if (vm().exception())
  330. return MarkedValueList { heap() };
  331. }
  332. }
  333. return properties;
  334. }
  335. // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
  336. MarkedValueList Object::get_enumerable_own_property_names(PropertyKind kind) const
  337. {
  338. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  339. }
  340. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  341. {
  342. VERIFY(property_name.is_valid());
  343. Value value;
  344. PropertyAttributes attributes;
  345. if (property_name.is_number()) {
  346. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No);
  347. if (!existing_value.has_value())
  348. return {};
  349. value = existing_value.value().value;
  350. attributes = existing_value.value().attributes;
  351. } else {
  352. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  353. if (!metadata.has_value())
  354. return {};
  355. value = m_storage[metadata.value().offset];
  356. attributes = metadata.value().attributes;
  357. }
  358. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  359. if (value.is_native_property()) {
  360. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  361. descriptor.value = result.value_or(js_undefined());
  362. } else if (value.is_accessor()) {
  363. auto& pair = value.as_accessor();
  364. if (pair.getter())
  365. descriptor.getter = pair.getter();
  366. if (pair.setter())
  367. descriptor.setter = pair.setter();
  368. } else {
  369. descriptor.value = value.value_or(js_undefined());
  370. }
  371. return descriptor;
  372. }
  373. // Equivalent to:
  374. // 6.2.5.4 FromPropertyDescriptor ( Desc ), https://tc39.es/ecma262/#sec-frompropertydescriptor
  375. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  376. {
  377. VERIFY(property_name.is_valid());
  378. auto& vm = this->vm();
  379. auto& global_object = this->global_object();
  380. auto descriptor_opt = get_own_property_descriptor(property_name);
  381. if (!descriptor_opt.has_value())
  382. return js_undefined();
  383. auto descriptor = descriptor_opt.value();
  384. auto* descriptor_object = Object::create(global_object, global_object.object_prototype());
  385. if (descriptor.is_data_descriptor()) {
  386. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  387. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  388. } else {
  389. VERIFY(descriptor.is_accessor_descriptor());
  390. descriptor_object->define_property(vm.names.get, descriptor.getter ? Value(descriptor.getter) : js_undefined());
  391. descriptor_object->define_property(vm.names.set, descriptor.setter ? Value(descriptor.setter) : js_undefined());
  392. }
  393. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  394. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  395. return descriptor_object;
  396. }
  397. void Object::set_shape(Shape& new_shape)
  398. {
  399. m_storage.resize(new_shape.property_count());
  400. m_shape = &new_shape;
  401. }
  402. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  403. {
  404. auto& vm = this->vm();
  405. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  406. PropertyAttributes attributes;
  407. if (descriptor.has_property(vm.names.configurable)) {
  408. attributes.set_has_configurable();
  409. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  410. attributes.set_configurable();
  411. if (vm.exception())
  412. return false;
  413. }
  414. if (descriptor.has_property(vm.names.enumerable)) {
  415. attributes.set_has_enumerable();
  416. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  417. attributes.set_enumerable();
  418. if (vm.exception())
  419. return false;
  420. }
  421. if (is_accessor_property) {
  422. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  423. if (throw_exceptions)
  424. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  425. return false;
  426. }
  427. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  428. if (vm.exception())
  429. return {};
  430. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  431. if (vm.exception())
  432. return {};
  433. Function* getter_function { nullptr };
  434. Function* setter_function { nullptr };
  435. auto existing_property = get_without_side_effects(property_name).value_or(js_undefined());
  436. if (getter.is_function()) {
  437. getter_function = &getter.as_function();
  438. } else if (!getter.is_undefined()) {
  439. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  440. return false;
  441. } else if (existing_property.is_accessor()) {
  442. // FIXME: This is a hack, since we store Accessor as a getter & setter tuple value, instead of as separate entries in the property
  443. getter_function = existing_property.as_accessor().getter();
  444. }
  445. if (setter.is_function()) {
  446. setter_function = &setter.as_function();
  447. } else if (!setter.is_undefined()) {
  448. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  449. return false;
  450. } else if (existing_property.is_accessor()) {
  451. // FIXME: See above
  452. setter_function = existing_property.as_accessor().setter();
  453. }
  454. dbgln_if(OBJECT_DEBUG, "Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  455. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  456. }
  457. auto value = descriptor.get(vm.names.value);
  458. if (vm.exception())
  459. return {};
  460. if (descriptor.has_property(vm.names.writable)) {
  461. attributes.set_has_writable();
  462. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  463. attributes.set_writable();
  464. if (vm.exception())
  465. return false;
  466. }
  467. if (vm.exception())
  468. return {};
  469. dbgln_if(OBJECT_DEBUG, "Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  470. return define_property(property_name, value, attributes, throw_exceptions);
  471. }
  472. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  473. {
  474. TemporaryChange change(m_transitions_enabled, false);
  475. return define_property(property_name, value, attributes, throw_exceptions);
  476. }
  477. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  478. {
  479. VERIFY(property_name.is_valid());
  480. if (property_name.is_number())
  481. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  482. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  483. }
  484. bool Object::define_native_accessor(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  485. {
  486. auto& vm = this->vm();
  487. String formatted_property_name;
  488. if (property_name.is_string()) {
  489. formatted_property_name = property_name.as_string();
  490. } else {
  491. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  492. }
  493. Function* getter_function = nullptr;
  494. if (getter) {
  495. auto name = String::formatted("get {}", formatted_property_name);
  496. getter_function = NativeFunction::create(global_object(), name, move(getter));
  497. getter_function->define_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  498. if (vm.exception())
  499. return {};
  500. getter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  501. if (vm.exception())
  502. return {};
  503. }
  504. Function* setter_function = nullptr;
  505. if (setter) {
  506. auto name = String::formatted("set {}", formatted_property_name);
  507. setter_function = NativeFunction::create(global_object(), name, move(setter));
  508. setter_function->define_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  509. if (vm.exception())
  510. return {};
  511. setter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  512. if (vm.exception())
  513. return {};
  514. }
  515. return define_accessor(property_name, getter_function, setter_function, attribute);
  516. }
  517. bool Object::define_accessor(const PropertyName& property_name, Function* getter, Function* setter, PropertyAttributes attributes, bool throw_exceptions)
  518. {
  519. VERIFY(property_name.is_valid());
  520. auto existing_property = get_own_property(property_name, this, AllowSideEffects::No);
  521. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  522. if (!accessor) {
  523. accessor = Accessor::create(vm(), getter, setter);
  524. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  525. if (vm().exception())
  526. return {};
  527. if (!definition_success)
  528. return false;
  529. } else {
  530. if (getter)
  531. accessor->set_getter(getter);
  532. if (setter)
  533. accessor->set_setter(setter);
  534. }
  535. return true;
  536. }
  537. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  538. {
  539. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  540. if (value.is_accessor()) {
  541. auto& accessor = value.as_accessor();
  542. if (accessor.getter())
  543. attributes.set_has_getter();
  544. if (accessor.setter())
  545. attributes.set_has_setter();
  546. }
  547. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  548. // Transitions are primarily interesting when scripts add properties to objects.
  549. if (!m_transitions_enabled && !m_shape->is_unique()) {
  550. m_shape->add_property_without_transition(property_name, attributes);
  551. m_storage.resize(m_shape->property_count());
  552. m_storage[m_shape->property_count() - 1] = value;
  553. return true;
  554. }
  555. auto metadata = shape().lookup(property_name);
  556. bool new_property = !metadata.has_value();
  557. if (!is_extensible() && new_property) {
  558. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  559. if (throw_exceptions && vm().in_strict_mode())
  560. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  561. return false;
  562. }
  563. if (new_property) {
  564. if (!m_shape->is_unique() && shape().property_count() > 100) {
  565. // If you add more than 100 properties to an object, let's stop doing
  566. // transitions to avoid filling up the heap with shapes.
  567. ensure_shape_is_unique();
  568. }
  569. if (m_shape->is_unique()) {
  570. m_shape->add_property_to_unique_shape(property_name, attributes);
  571. m_storage.resize(m_shape->property_count());
  572. } else if (m_transitions_enabled) {
  573. set_shape(*m_shape->create_put_transition(property_name, attributes));
  574. } else {
  575. m_shape->add_property_without_transition(property_name, attributes);
  576. m_storage.resize(m_shape->property_count());
  577. }
  578. metadata = shape().lookup(property_name);
  579. VERIFY(metadata.has_value());
  580. }
  581. auto value_here = m_storage[metadata.value().offset];
  582. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable()) {
  583. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != metadata.value().attributes.is_enumerable())) {
  584. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  585. if (throw_exceptions)
  586. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  587. return false;
  588. }
  589. if (value_here.is_accessor() != value.is_accessor()) {
  590. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  591. if (throw_exceptions)
  592. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  593. return false;
  594. }
  595. if (!value_here.is_accessor() && !metadata.value().attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  596. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  597. if (throw_exceptions)
  598. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  599. return false;
  600. }
  601. if (value_here.is_accessor() && ((attributes.has_setter() && value.as_accessor().setter() != value_here.as_accessor().setter()) || (attributes.has_getter() && value.as_accessor().getter() != value_here.as_accessor().getter()))) {
  602. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  603. if (throw_exceptions)
  604. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  605. return false;
  606. }
  607. }
  608. // FIXME: Instead of adding back existing attributes we should just stop overwriting them
  609. if (!attributes.has_configurable() && metadata.value().attributes.is_configurable()) {
  610. attributes.set_has_configurable();
  611. attributes.set_configurable();
  612. }
  613. if (!attributes.has_enumerable() && metadata.value().attributes.is_enumerable()) {
  614. attributes.set_has_enumerable();
  615. attributes.set_enumerable();
  616. }
  617. if (!value.is_accessor() && !attributes.has_writable() && metadata.value().attributes.is_writable()) {
  618. attributes.set_has_writable();
  619. attributes.set_writable();
  620. }
  621. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  622. if (m_shape->is_unique()) {
  623. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  624. } else {
  625. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  626. }
  627. metadata = shape().lookup(property_name);
  628. dbgln_if(OBJECT_DEBUG, "Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  629. }
  630. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  631. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  632. if (throw_exceptions && vm().in_strict_mode())
  633. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  634. return false;
  635. }
  636. if (value.is_empty())
  637. return true;
  638. if (value_here.is_native_property()) {
  639. call_native_property_setter(value_here.as_native_property(), this, value);
  640. } else {
  641. m_storage[metadata.value().offset] = value;
  642. }
  643. return true;
  644. }
  645. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  646. {
  647. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  648. auto existing_property = m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  649. auto new_property = !existing_property.has_value();
  650. if (!is_extensible() && new_property) {
  651. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  652. if (throw_exceptions && vm().in_strict_mode())
  653. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  654. return false;
  655. }
  656. if (value.is_accessor()) {
  657. auto& accessor = value.as_accessor();
  658. if (accessor.getter())
  659. attributes.set_has_getter();
  660. if (accessor.setter())
  661. attributes.set_has_setter();
  662. }
  663. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  664. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  665. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  666. if (throw_exceptions)
  667. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  668. return false;
  669. }
  670. auto value_here = new_property ? Value() : existing_property.value().value;
  671. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  672. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  673. return false;
  674. }
  675. if (value.is_empty())
  676. return true;
  677. if (value_here.is_native_property()) {
  678. call_native_property_setter(value_here.as_native_property(), this, value);
  679. } else {
  680. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put ? AllowSideEffects::Yes : AllowSideEffects::No);
  681. }
  682. return true;
  683. }
  684. bool Object::delete_property(const PropertyName& property_name)
  685. {
  686. VERIFY(property_name.is_valid());
  687. if (property_name.is_number())
  688. return m_indexed_properties.remove(property_name.as_number());
  689. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  690. if (!metadata.has_value())
  691. return true;
  692. if (!metadata.value().attributes.is_configurable()) {
  693. if (vm().in_strict_mode())
  694. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_string_or_symbol().to_display_string());
  695. return false;
  696. }
  697. size_t deleted_offset = metadata.value().offset;
  698. ensure_shape_is_unique();
  699. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  700. m_storage.remove(deleted_offset);
  701. return true;
  702. }
  703. void Object::ensure_shape_is_unique()
  704. {
  705. if (shape().is_unique())
  706. return;
  707. m_shape = m_shape->create_unique_clone();
  708. }
  709. Value Object::get_by_index(u32 property_index, AllowSideEffects allow_side_effects) const
  710. {
  711. const Object* object = this;
  712. while (object) {
  713. if (is<StringObject>(*object)) {
  714. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  715. if (property_index < string.length())
  716. return js_string(heap(), string.substring(property_index, 1));
  717. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  718. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index, allow_side_effects);
  719. if (vm().exception())
  720. return {};
  721. if (result.has_value() && !result.value().value.is_empty())
  722. return result.value().value;
  723. }
  724. object = object->prototype();
  725. if (vm().exception())
  726. return {};
  727. }
  728. return {};
  729. }
  730. Value Object::get(const PropertyName& property_name, Value receiver, AllowSideEffects allow_side_effects) const
  731. {
  732. VERIFY(property_name.is_valid());
  733. if (property_name.is_number())
  734. return get_by_index(property_name.as_number(), allow_side_effects);
  735. if (receiver.is_empty())
  736. receiver = Value(this);
  737. const Object* object = this;
  738. while (object) {
  739. auto value = object->get_own_property(property_name, receiver, allow_side_effects);
  740. if (vm().exception())
  741. return {};
  742. if (!value.is_empty())
  743. return value;
  744. object = object->prototype();
  745. if (vm().exception())
  746. return {};
  747. }
  748. return {};
  749. }
  750. Value Object::get_without_side_effects(const PropertyName& property_name) const
  751. {
  752. TemporaryClearException clear_exception(vm());
  753. return get(property_name, {}, AllowSideEffects::No);
  754. }
  755. bool Object::put_by_index(u32 property_index, Value value)
  756. {
  757. VERIFY(!value.is_empty());
  758. // If there's a setter in the prototype chain, we go to the setter.
  759. // Otherwise, it goes in the own property storage.
  760. Object* object = this;
  761. while (object) {
  762. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  763. if (existing_value.has_value()) {
  764. auto value_here = existing_value.value();
  765. if (value_here.value.is_accessor()) {
  766. value_here.value.as_accessor().call_setter(object, value);
  767. return true;
  768. }
  769. if (value_here.value.is_native_property()) {
  770. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  771. auto receiver = this;
  772. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  773. return true;
  774. }
  775. }
  776. object = object->prototype();
  777. if (vm().exception())
  778. return {};
  779. }
  780. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  781. }
  782. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  783. {
  784. VERIFY(property_name.is_valid());
  785. if (property_name.is_number())
  786. return put_by_index(property_name.as_number(), value);
  787. VERIFY(!value.is_empty());
  788. auto string_or_symbol = property_name.to_string_or_symbol();
  789. if (receiver.is_empty())
  790. receiver = Value(this);
  791. // If there's a setter in the prototype chain, we go to the setter.
  792. // Otherwise, it goes in the own property storage.
  793. Object* object = this;
  794. while (object) {
  795. auto metadata = object->shape().lookup(string_or_symbol);
  796. if (metadata.has_value()) {
  797. auto value_here = object->m_storage[metadata.value().offset];
  798. if (value_here.is_accessor()) {
  799. value_here.as_accessor().call_setter(receiver, value);
  800. return true;
  801. }
  802. if (value_here.is_native_property()) {
  803. call_native_property_setter(value_here.as_native_property(), receiver, value);
  804. return true;
  805. }
  806. }
  807. object = object->prototype();
  808. if (vm().exception())
  809. return false;
  810. }
  811. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  812. }
  813. bool Object::define_native_function(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  814. {
  815. auto& vm = this->vm();
  816. String function_name;
  817. if (property_name.is_string()) {
  818. function_name = property_name.as_string();
  819. } else {
  820. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  821. }
  822. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  823. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  824. if (vm.exception())
  825. return {};
  826. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  827. if (vm.exception())
  828. return {};
  829. return define_property(property_name, function, attribute);
  830. }
  831. bool Object::define_native_property(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  832. {
  833. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  834. }
  835. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  836. void Object::define_properties(Value properties)
  837. {
  838. auto& vm = this->vm();
  839. auto* props = properties.to_object(global_object());
  840. if (!props)
  841. return;
  842. auto keys = props->get_own_properties(PropertyKind::Key);
  843. if (vm.exception())
  844. return;
  845. struct NameAndDescriptor {
  846. PropertyName name;
  847. PropertyDescriptor descriptor;
  848. };
  849. Vector<NameAndDescriptor> descriptors;
  850. for (auto& key : keys) {
  851. auto property_name = PropertyName::from_value(global_object(), key);
  852. auto property_descriptor = props->get_own_property_descriptor(property_name);
  853. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  854. auto descriptor_object = props->get(property_name);
  855. if (vm.exception())
  856. return;
  857. if (!descriptor_object.is_object()) {
  858. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  859. return;
  860. }
  861. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  862. if (vm.exception())
  863. return;
  864. descriptors.append({ property_name, descriptor });
  865. }
  866. }
  867. for (auto& [name, descriptor] : descriptors) {
  868. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  869. // We should invest some time in improving object property handling, it not being
  870. // super close to the spec makes this and other things unnecessarily complicated.
  871. if (descriptor.is_accessor_descriptor())
  872. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  873. else
  874. define_property(name, descriptor.value, descriptor.attributes);
  875. }
  876. }
  877. void Object::visit_edges(Cell::Visitor& visitor)
  878. {
  879. Cell::visit_edges(visitor);
  880. visitor.visit(m_shape);
  881. for (auto& value : m_storage)
  882. visitor.visit(value);
  883. m_indexed_properties.for_each_value([&visitor](auto& value) {
  884. visitor.visit(value);
  885. });
  886. }
  887. bool Object::has_property(const PropertyName& property_name) const
  888. {
  889. const Object* object = this;
  890. while (object) {
  891. if (object->has_own_property(property_name))
  892. return true;
  893. object = object->prototype();
  894. if (vm().exception())
  895. return false;
  896. }
  897. return false;
  898. }
  899. bool Object::has_own_property(const PropertyName& property_name) const
  900. {
  901. VERIFY(property_name.is_valid());
  902. auto has_indexed_property = [&](u32 index) -> bool {
  903. if (is<StringObject>(*this))
  904. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  905. return m_indexed_properties.has_index(index);
  906. };
  907. if (property_name.is_number())
  908. return has_indexed_property(property_name.as_number());
  909. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  910. }
  911. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  912. {
  913. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  914. auto& vm = this->vm();
  915. Vector<FlyString, 2> method_names;
  916. if (preferred_type == Value::PreferredType::String)
  917. method_names = { vm.names.toString.as_string(), vm.names.valueOf.as_string() };
  918. else
  919. method_names = { vm.names.valueOf.as_string(), vm.names.toString.as_string() };
  920. for (auto& method_name : method_names) {
  921. auto method = get(method_name);
  922. if (vm.exception())
  923. return {};
  924. if (method.is_function()) {
  925. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  926. if (!result.is_object())
  927. return result;
  928. }
  929. }
  930. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  931. return {};
  932. }
  933. // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/proposal-error-cause/#sec-errorobjects-install-error-cause
  934. void Object::install_error_cause(Value options)
  935. {
  936. auto& vm = this->vm();
  937. if (!options.is_object())
  938. return;
  939. auto& options_object = options.as_object();
  940. if (!options_object.has_property(vm.names.cause))
  941. return;
  942. auto cause = options_object.get(vm.names.cause).value_or(js_undefined());
  943. if (vm.exception())
  944. return;
  945. define_property(vm.names.cause, cause, Attribute::Writable | Attribute::Configurable);
  946. }
  947. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  948. {
  949. auto& vm = this->vm();
  950. auto property = get(property_name).value_or(js_undefined());
  951. if (vm.exception())
  952. return {};
  953. if (!property.is_function()) {
  954. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  955. return {};
  956. }
  957. return vm.call(property.as_function(), this, move(arguments));
  958. }
  959. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  960. {
  961. auto& vm = this->vm();
  962. CallFrame call_frame;
  963. if (auto* interpreter = vm.interpreter_if_exists())
  964. call_frame.current_node = interpreter->current_node();
  965. call_frame.is_strict_mode = vm.in_strict_mode();
  966. call_frame.this_value = this_value;
  967. vm.push_call_frame(call_frame, global_object());
  968. if (vm.exception())
  969. return {};
  970. auto result = property.get(vm, global_object());
  971. vm.pop_call_frame();
  972. return result;
  973. }
  974. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  975. {
  976. auto& vm = this->vm();
  977. CallFrame call_frame;
  978. if (auto* interpreter = vm.interpreter_if_exists())
  979. call_frame.current_node = interpreter->current_node();
  980. call_frame.is_strict_mode = vm.in_strict_mode();
  981. call_frame.this_value = this_value;
  982. vm.push_call_frame(call_frame, global_object());
  983. if (vm.exception())
  984. return;
  985. property.set(vm, global_object(), setter_value);
  986. vm.pop_call_frame();
  987. }
  988. }