Object.cpp 39 KB

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