Object.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Debug.h>
  28. #include <AK/String.h>
  29. #include <AK/TemporaryChange.h>
  30. #include <LibJS/Heap/Heap.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Runtime/Accessor.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/NativeFunction.h>
  37. #include <LibJS/Runtime/NativeProperty.h>
  38. #include <LibJS/Runtime/Object.h>
  39. #include <LibJS/Runtime/Shape.h>
  40. #include <LibJS/Runtime/StringObject.h>
  41. #include <LibJS/Runtime/Value.h>
  42. namespace JS {
  43. PropertyDescriptor PropertyDescriptor::from_dictionary(VM& vm, const Object& object)
  44. {
  45. PropertyAttributes attributes;
  46. if (object.has_property(vm.names.configurable)) {
  47. attributes.set_has_configurable();
  48. if (object.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  49. attributes.set_configurable();
  50. if (vm.exception())
  51. return {};
  52. }
  53. if (object.has_property(vm.names.enumerable)) {
  54. attributes.set_has_enumerable();
  55. if (object.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  56. attributes.set_enumerable();
  57. if (vm.exception())
  58. return {};
  59. }
  60. if (object.has_property(vm.names.writable)) {
  61. attributes.set_has_writable();
  62. if (object.get(vm.names.writable).value_or(Value(false)).to_boolean())
  63. attributes.set_writable();
  64. if (vm.exception())
  65. return {};
  66. }
  67. PropertyDescriptor descriptor { attributes, object.get(vm.names.value), nullptr, nullptr };
  68. if (vm.exception())
  69. return {};
  70. auto getter = object.get(vm.names.get);
  71. if (vm.exception())
  72. return {};
  73. if (getter.is_function())
  74. descriptor.getter = &getter.as_function();
  75. auto setter = object.get(vm.names.set);
  76. if (vm.exception())
  77. return {};
  78. if (setter.is_function())
  79. descriptor.setter = &setter.as_function();
  80. return descriptor;
  81. }
  82. Object* Object::create_empty(GlobalObject& global_object)
  83. {
  84. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  85. }
  86. Object::Object(GlobalObjectTag)
  87. {
  88. // This is the global object
  89. m_shape = heap().allocate_without_global_object<Shape>(*this);
  90. }
  91. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  92. {
  93. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  94. }
  95. Object::Object(Object& prototype)
  96. {
  97. m_shape = prototype.global_object().empty_object_shape();
  98. set_prototype(&prototype);
  99. }
  100. Object::Object(Shape& shape)
  101. : m_shape(&shape)
  102. {
  103. m_storage.resize(shape.property_count());
  104. }
  105. void Object::initialize(GlobalObject&)
  106. {
  107. }
  108. Object::~Object()
  109. {
  110. }
  111. Object* Object::prototype()
  112. {
  113. return shape().prototype();
  114. }
  115. const Object* Object::prototype() const
  116. {
  117. return shape().prototype();
  118. }
  119. bool Object::set_prototype(Object* new_prototype)
  120. {
  121. if (prototype() == new_prototype)
  122. return true;
  123. if (!m_is_extensible)
  124. return false;
  125. if (shape().is_unique()) {
  126. shape().set_prototype_without_transition(new_prototype);
  127. return true;
  128. }
  129. m_shape = m_shape->create_prototype_transition(new_prototype);
  130. return true;
  131. }
  132. bool Object::has_prototype(const Object* prototype) const
  133. {
  134. for (auto* object = this->prototype(); object; object = object->prototype()) {
  135. if (vm().exception())
  136. return false;
  137. if (object == prototype)
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool Object::prevent_extensions()
  143. {
  144. m_is_extensible = false;
  145. return true;
  146. }
  147. Value Object::get_own_property(const PropertyName& property_name, Value receiver) const
  148. {
  149. VERIFY(property_name.is_valid());
  150. VERIFY(!receiver.is_empty());
  151. Value value_here;
  152. if (property_name.is_number()) {
  153. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  154. if (!existing_property.has_value())
  155. return {};
  156. value_here = existing_property.value().value.value_or(js_undefined());
  157. } else {
  158. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  159. if (!metadata.has_value())
  160. return {};
  161. value_here = m_storage[metadata.value().offset].value_or(js_undefined());
  162. }
  163. VERIFY(!value_here.is_empty());
  164. if (value_here.is_accessor())
  165. return value_here.as_accessor().call_getter(receiver);
  166. if (value_here.is_native_property())
  167. return call_native_property_getter(value_here.as_native_property(), receiver);
  168. return value_here;
  169. }
  170. Value Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
  171. {
  172. auto* properties_array = Array::create(global_object());
  173. // FIXME: Support generic iterables
  174. if (is<StringObject>(*this)) {
  175. auto str = static_cast<const StringObject&>(*this).primitive_string().string();
  176. for (size_t i = 0; i < str.length(); ++i) {
  177. if (kind == PropertyKind::Key) {
  178. properties_array->define_property(i, js_string(vm(), String::number(i)));
  179. } else if (kind == PropertyKind::Value) {
  180. properties_array->define_property(i, js_string(vm(), String::formatted("{:c}", str[i])));
  181. } else {
  182. auto* entry_array = Array::create(global_object());
  183. entry_array->define_property(0, js_string(vm(), String::number(i)));
  184. entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
  185. properties_array->define_property(i, entry_array);
  186. }
  187. if (vm().exception())
  188. return {};
  189. }
  190. return properties_array;
  191. }
  192. size_t property_index = 0;
  193. for (auto& entry : m_indexed_properties) {
  194. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  195. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  196. continue;
  197. if (kind == PropertyKind::Key) {
  198. properties_array->define_property(property_index, js_string(vm(), String::number(entry.index())));
  199. } else if (kind == PropertyKind::Value) {
  200. properties_array->define_property(property_index, value_and_attributes.value);
  201. } else {
  202. auto* entry_array = Array::create(global_object());
  203. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  204. entry_array->define_property(1, value_and_attributes.value);
  205. properties_array->define_property(property_index, entry_array);
  206. }
  207. if (vm().exception())
  208. return {};
  209. ++property_index;
  210. }
  211. auto add_property_to_results = [&](auto& property) {
  212. if (kind == PropertyKind::Key) {
  213. properties_array->define_property(property_index, property.key.to_value(vm()));
  214. } else if (kind == PropertyKind::Value) {
  215. properties_array->define_property(property_index, get(property.key));
  216. } else {
  217. auto* entry_array = Array::create(global_object());
  218. entry_array->define_property(0, property.key.to_value(vm()));
  219. entry_array->define_property(1, get(property.key));
  220. properties_array->define_property(property_index, entry_array);
  221. }
  222. };
  223. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  224. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  225. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  226. for (auto& it : shape().property_table_ordered()) {
  227. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  228. continue;
  229. if (it.key.is_symbol())
  230. continue;
  231. add_property_to_results(it);
  232. if (vm().exception())
  233. return {};
  234. ++property_index;
  235. }
  236. }
  237. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  238. for (auto& it : shape().property_table_ordered()) {
  239. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  240. continue;
  241. if (it.key.is_string())
  242. continue;
  243. add_property_to_results(it);
  244. if (vm().exception())
  245. return {};
  246. ++property_index;
  247. }
  248. }
  249. return properties_array;
  250. }
  251. // 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
  252. Value Object::get_enumerable_own_property_names(PropertyKind kind) const
  253. {
  254. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  255. }
  256. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  257. {
  258. VERIFY(property_name.is_valid());
  259. Value value;
  260. PropertyAttributes attributes;
  261. if (property_name.is_number()) {
  262. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  263. if (!existing_value.has_value())
  264. return {};
  265. value = existing_value.value().value;
  266. attributes = existing_value.value().attributes;
  267. attributes = default_attributes;
  268. } else {
  269. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  270. if (!metadata.has_value())
  271. return {};
  272. value = m_storage[metadata.value().offset];
  273. if (vm().exception())
  274. return {};
  275. attributes = metadata.value().attributes;
  276. }
  277. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  278. if (value.is_native_property()) {
  279. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  280. descriptor.value = result.value_or(js_undefined());
  281. } else if (value.is_accessor()) {
  282. auto& pair = value.as_accessor();
  283. if (pair.getter())
  284. descriptor.getter = pair.getter();
  285. if (pair.setter())
  286. descriptor.setter = pair.setter();
  287. } else {
  288. descriptor.value = value.value_or(js_undefined());
  289. }
  290. return descriptor;
  291. }
  292. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  293. {
  294. VERIFY(property_name.is_valid());
  295. auto& vm = this->vm();
  296. auto descriptor_opt = get_own_property_descriptor(property_name);
  297. if (!descriptor_opt.has_value())
  298. return js_undefined();
  299. auto descriptor = descriptor_opt.value();
  300. auto* descriptor_object = Object::create_empty(global_object());
  301. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  302. if (vm.exception())
  303. return {};
  304. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  305. if (vm.exception())
  306. return {};
  307. if (descriptor.is_data_descriptor()) {
  308. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  309. if (vm.exception())
  310. return {};
  311. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  312. if (vm.exception())
  313. return {};
  314. } else if (descriptor.is_accessor_descriptor()) {
  315. if (descriptor.getter) {
  316. descriptor_object->define_property(vm.names.get, Value(descriptor.getter));
  317. if (vm.exception())
  318. return {};
  319. }
  320. if (descriptor.setter) {
  321. descriptor_object->define_property(vm.names.set, Value(descriptor.setter));
  322. if (vm.exception())
  323. return {};
  324. }
  325. }
  326. return descriptor_object;
  327. }
  328. void Object::set_shape(Shape& new_shape)
  329. {
  330. m_storage.resize(new_shape.property_count());
  331. m_shape = &new_shape;
  332. }
  333. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  334. {
  335. auto& vm = this->vm();
  336. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  337. PropertyAttributes attributes;
  338. if (descriptor.has_property(vm.names.configurable)) {
  339. attributes.set_has_configurable();
  340. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  341. attributes.set_configurable();
  342. if (vm.exception())
  343. return false;
  344. }
  345. if (descriptor.has_property(vm.names.enumerable)) {
  346. attributes.set_has_enumerable();
  347. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  348. attributes.set_enumerable();
  349. if (vm.exception())
  350. return false;
  351. }
  352. if (is_accessor_property) {
  353. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  354. if (throw_exceptions)
  355. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  356. return false;
  357. }
  358. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  359. if (vm.exception())
  360. return {};
  361. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  362. if (vm.exception())
  363. return {};
  364. Function* getter_function { nullptr };
  365. Function* setter_function { nullptr };
  366. if (getter.is_function()) {
  367. getter_function = &getter.as_function();
  368. } else if (!getter.is_undefined()) {
  369. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  370. return false;
  371. }
  372. if (setter.is_function()) {
  373. setter_function = &setter.as_function();
  374. } else if (!setter.is_undefined()) {
  375. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  376. return false;
  377. }
  378. #if OBJECT_DEBUG
  379. dbgln("Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  380. #endif
  381. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  382. }
  383. auto value = descriptor.get(vm.names.value);
  384. if (vm.exception())
  385. return {};
  386. if (descriptor.has_property(vm.names.writable)) {
  387. attributes.set_has_writable();
  388. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  389. attributes.set_writable();
  390. if (vm.exception())
  391. return false;
  392. }
  393. if (vm.exception())
  394. return {};
  395. #if OBJECT_DEBUG
  396. dbgln("Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  397. #endif
  398. return define_property(property_name, value, attributes, throw_exceptions);
  399. }
  400. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  401. {
  402. TemporaryChange change(m_transitions_enabled, false);
  403. return define_property(property_name, value, attributes, throw_exceptions);
  404. }
  405. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  406. {
  407. VERIFY(property_name.is_valid());
  408. if (property_name.is_number())
  409. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  410. if (property_name.is_string()) {
  411. i32 property_index = property_name.as_string().to_int().value_or(-1);
  412. if (property_index >= 0)
  413. return put_own_property_by_index(property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  414. }
  415. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  416. }
  417. bool Object::define_accessor(const PropertyName& property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
  418. {
  419. VERIFY(property_name.is_valid());
  420. Accessor* accessor { nullptr };
  421. auto property_metadata = shape().lookup(property_name.to_string_or_symbol());
  422. if (property_metadata.has_value()) {
  423. auto existing_property = get_direct(property_metadata.value().offset);
  424. if (existing_property.is_accessor())
  425. accessor = &existing_property.as_accessor();
  426. }
  427. if (!accessor) {
  428. accessor = Accessor::create(vm(), nullptr, nullptr);
  429. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  430. if (vm().exception())
  431. return {};
  432. if (!definition_success)
  433. return false;
  434. }
  435. if (is_getter)
  436. accessor->set_getter(&getter_or_setter);
  437. else
  438. accessor->set_setter(&getter_or_setter);
  439. return true;
  440. }
  441. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  442. {
  443. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  444. if (value.is_accessor()) {
  445. auto& accessor = value.as_accessor();
  446. if (accessor.getter())
  447. attributes.set_has_getter();
  448. if (accessor.setter())
  449. attributes.set_has_setter();
  450. }
  451. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  452. // Transitions are primarily interesting when scripts add properties to objects.
  453. if (!m_transitions_enabled && !m_shape->is_unique()) {
  454. m_shape->add_property_without_transition(property_name, attributes);
  455. m_storage.resize(m_shape->property_count());
  456. m_storage[m_shape->property_count() - 1] = value;
  457. return true;
  458. }
  459. auto metadata = shape().lookup(property_name);
  460. bool new_property = !metadata.has_value();
  461. if (!is_extensible() && new_property) {
  462. #if OBJECT_DEBUG
  463. dbgln("Disallow define_property of non-extensible object");
  464. #endif
  465. if (throw_exceptions && vm().in_strict_mode())
  466. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  467. return false;
  468. }
  469. if (new_property) {
  470. if (!m_shape->is_unique() && shape().property_count() > 100) {
  471. // If you add more than 100 properties to an object, let's stop doing
  472. // transitions to avoid filling up the heap with shapes.
  473. ensure_shape_is_unique();
  474. }
  475. if (m_shape->is_unique()) {
  476. m_shape->add_property_to_unique_shape(property_name, attributes);
  477. m_storage.resize(m_shape->property_count());
  478. } else if (m_transitions_enabled) {
  479. set_shape(*m_shape->create_put_transition(property_name, attributes));
  480. } else {
  481. m_shape->add_property_without_transition(property_name, attributes);
  482. m_storage.resize(m_shape->property_count());
  483. }
  484. metadata = shape().lookup(property_name);
  485. VERIFY(metadata.has_value());
  486. }
  487. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
  488. #if OBJECT_DEBUG
  489. dbgln("Disallow reconfig of non-configurable property");
  490. #endif
  491. if (throw_exceptions)
  492. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  493. return false;
  494. }
  495. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  496. if (m_shape->is_unique()) {
  497. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  498. } else {
  499. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  500. }
  501. metadata = shape().lookup(property_name);
  502. #if OBJECT_DEBUG
  503. dbgln("Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  504. #endif
  505. }
  506. auto value_here = m_storage[metadata.value().offset];
  507. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  508. #if OBJECT_DEBUG
  509. dbgln("Disallow write to non-writable property");
  510. #endif
  511. return false;
  512. }
  513. if (value.is_empty())
  514. return true;
  515. if (value_here.is_native_property()) {
  516. call_native_property_setter(value_here.as_native_property(), this, value);
  517. } else {
  518. m_storage[metadata.value().offset] = value;
  519. }
  520. return true;
  521. }
  522. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  523. {
  524. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  525. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  526. auto new_property = !existing_property.has_value();
  527. if (!is_extensible() && new_property) {
  528. #if OBJECT_DEBUG
  529. dbgln("Disallow define_property of non-extensible object");
  530. #endif
  531. if (throw_exceptions && vm().in_strict_mode())
  532. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  533. return false;
  534. }
  535. if (value.is_accessor()) {
  536. auto& accessor = value.as_accessor();
  537. if (accessor.getter())
  538. attributes.set_has_getter();
  539. if (accessor.setter())
  540. attributes.set_has_setter();
  541. }
  542. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  543. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  544. #if OBJECT_DEBUG
  545. dbgln("Disallow reconfig of non-configurable property");
  546. #endif
  547. if (throw_exceptions)
  548. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  549. return false;
  550. }
  551. auto value_here = new_property ? Value() : existing_property.value().value;
  552. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  553. #if OBJECT_DEBUG
  554. dbgln("Disallow write to non-writable property");
  555. #endif
  556. return false;
  557. }
  558. if (value.is_empty())
  559. return true;
  560. if (value_here.is_native_property()) {
  561. call_native_property_setter(value_here.as_native_property(), this, value);
  562. } else {
  563. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  564. }
  565. return true;
  566. }
  567. Value Object::delete_property(const PropertyName& property_name)
  568. {
  569. VERIFY(property_name.is_valid());
  570. if (property_name.is_number())
  571. return Value(m_indexed_properties.remove(property_name.as_number()));
  572. if (property_name.is_string()) {
  573. i32 property_index = property_name.as_string().to_int().value_or(-1);
  574. if (property_index >= 0)
  575. return Value(m_indexed_properties.remove(property_index));
  576. }
  577. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  578. if (!metadata.has_value())
  579. return Value(true);
  580. if (!metadata.value().attributes.is_configurable())
  581. return Value(false);
  582. size_t deleted_offset = metadata.value().offset;
  583. ensure_shape_is_unique();
  584. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  585. m_storage.remove(deleted_offset);
  586. return Value(true);
  587. }
  588. void Object::ensure_shape_is_unique()
  589. {
  590. if (shape().is_unique())
  591. return;
  592. m_shape = m_shape->create_unique_clone();
  593. }
  594. Value Object::get_by_index(u32 property_index) const
  595. {
  596. const Object* object = this;
  597. while (object) {
  598. if (is<StringObject>(*this)) {
  599. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  600. if (property_index < string.length())
  601. return js_string(heap(), string.substring(property_index, 1));
  602. return js_undefined();
  603. }
  604. if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  605. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  606. if (vm().exception())
  607. return {};
  608. if (result.has_value() && !result.value().value.is_empty())
  609. return result.value().value;
  610. return {};
  611. }
  612. object = object->prototype();
  613. if (vm().exception())
  614. return {};
  615. }
  616. return {};
  617. }
  618. Value Object::get(const PropertyName& property_name, Value receiver) const
  619. {
  620. VERIFY(property_name.is_valid());
  621. if (property_name.is_number())
  622. return get_by_index(property_name.as_number());
  623. if (property_name.is_string()) {
  624. auto property_string = property_name.to_string();
  625. i32 property_index = property_string.to_int().value_or(-1);
  626. if (property_index >= 0)
  627. return get_by_index(property_index);
  628. }
  629. if (receiver.is_empty())
  630. receiver = Value(this);
  631. const Object* object = this;
  632. while (object) {
  633. auto value = object->get_own_property(property_name, receiver);
  634. if (vm().exception())
  635. return {};
  636. if (!value.is_empty())
  637. return value;
  638. object = object->prototype();
  639. if (vm().exception())
  640. return {};
  641. }
  642. return {};
  643. }
  644. bool Object::put_by_index(u32 property_index, Value value)
  645. {
  646. VERIFY(!value.is_empty());
  647. // If there's a setter in the prototype chain, we go to the setter.
  648. // Otherwise, it goes in the own property storage.
  649. Object* object = this;
  650. while (object) {
  651. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  652. if (existing_value.has_value()) {
  653. auto value_here = existing_value.value();
  654. if (value_here.value.is_accessor()) {
  655. value_here.value.as_accessor().call_setter(object, value);
  656. return true;
  657. }
  658. if (value_here.value.is_native_property()) {
  659. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  660. auto receiver = this;
  661. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  662. return true;
  663. }
  664. }
  665. object = object->prototype();
  666. if (vm().exception())
  667. return {};
  668. }
  669. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  670. }
  671. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  672. {
  673. VERIFY(property_name.is_valid());
  674. if (property_name.is_number())
  675. return put_by_index(property_name.as_number(), value);
  676. VERIFY(!value.is_empty());
  677. if (property_name.is_string()) {
  678. auto& property_string = property_name.as_string();
  679. i32 property_index = property_string.to_int().value_or(-1);
  680. if (property_index >= 0)
  681. return put_by_index(property_index, value);
  682. }
  683. auto string_or_symbol = property_name.to_string_or_symbol();
  684. if (receiver.is_empty())
  685. receiver = Value(this);
  686. // If there's a setter in the prototype chain, we go to the setter.
  687. // Otherwise, it goes in the own property storage.
  688. Object* object = this;
  689. while (object) {
  690. auto metadata = object->shape().lookup(string_or_symbol);
  691. if (metadata.has_value()) {
  692. auto value_here = object->m_storage[metadata.value().offset];
  693. if (value_here.is_accessor()) {
  694. value_here.as_accessor().call_setter(receiver, value);
  695. return true;
  696. }
  697. if (value_here.is_native_property()) {
  698. call_native_property_setter(value_here.as_native_property(), receiver, value);
  699. return true;
  700. }
  701. }
  702. object = object->prototype();
  703. if (vm().exception())
  704. return false;
  705. }
  706. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  707. }
  708. bool Object::define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  709. {
  710. auto& vm = this->vm();
  711. String function_name;
  712. if (property_name.is_string()) {
  713. function_name = property_name.as_string();
  714. } else {
  715. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  716. }
  717. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  718. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  719. if (vm.exception())
  720. return {};
  721. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  722. if (vm.exception())
  723. return {};
  724. return define_property(property_name, function, attribute);
  725. }
  726. bool Object::define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  727. {
  728. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  729. }
  730. void Object::visit_edges(Cell::Visitor& visitor)
  731. {
  732. Cell::visit_edges(visitor);
  733. visitor.visit(m_shape);
  734. for (auto& value : m_storage)
  735. visitor.visit(value);
  736. m_indexed_properties.for_each_value([&visitor](auto& value) {
  737. visitor.visit(value);
  738. });
  739. }
  740. bool Object::has_property(const PropertyName& property_name) const
  741. {
  742. const Object* object = this;
  743. while (object) {
  744. if (object->has_own_property(property_name))
  745. return true;
  746. object = object->prototype();
  747. if (vm().exception())
  748. return false;
  749. }
  750. return false;
  751. }
  752. bool Object::has_own_property(const PropertyName& property_name) const
  753. {
  754. VERIFY(property_name.is_valid());
  755. auto has_indexed_property = [&](u32 index) -> bool {
  756. if (is<StringObject>(*this))
  757. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  758. return m_indexed_properties.has_index(index);
  759. };
  760. if (property_name.is_number())
  761. return has_indexed_property(property_name.as_number());
  762. if (property_name.is_string()) {
  763. i32 property_index = property_name.as_string().to_int().value_or(-1);
  764. if (property_index >= 0)
  765. return has_indexed_property(property_index);
  766. }
  767. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  768. }
  769. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  770. {
  771. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  772. auto& vm = this->vm();
  773. Vector<FlyString, 2> method_names;
  774. if (preferred_type == Value::PreferredType::String)
  775. method_names = { vm.names.toString, vm.names.valueOf };
  776. else
  777. method_names = { vm.names.valueOf, vm.names.toString };
  778. for (auto& method_name : method_names) {
  779. auto method = get(method_name);
  780. if (vm.exception())
  781. return {};
  782. if (method.is_function()) {
  783. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  784. if (!result.is_object())
  785. return result;
  786. }
  787. }
  788. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  789. return {};
  790. }
  791. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  792. {
  793. auto& vm = this->vm();
  794. auto property = get(property_name).value_or(js_undefined());
  795. if (vm.exception())
  796. return {};
  797. if (!property.is_function()) {
  798. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  799. return {};
  800. }
  801. return vm.call(property.as_function(), this, move(arguments));
  802. }
  803. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  804. {
  805. auto& vm = this->vm();
  806. CallFrame call_frame;
  807. if (auto* interpreter = vm.interpreter_if_exists())
  808. call_frame.current_node = interpreter->current_node();
  809. call_frame.is_strict_mode = vm.in_strict_mode();
  810. call_frame.this_value = this_value;
  811. vm.push_call_frame(call_frame, global_object());
  812. if (vm.exception())
  813. return {};
  814. auto result = property.get(vm, global_object());
  815. vm.pop_call_frame();
  816. return result;
  817. }
  818. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  819. {
  820. auto& vm = this->vm();
  821. CallFrame call_frame;
  822. if (auto* interpreter = vm.interpreter_if_exists())
  823. call_frame.current_node = interpreter->current_node();
  824. call_frame.is_strict_mode = vm.in_strict_mode();
  825. call_frame.this_value = this_value;
  826. vm.push_call_frame(call_frame, global_object());
  827. if (vm.exception())
  828. return;
  829. property.set(vm, global_object(), setter_value);
  830. vm.pop_call_frame();
  831. }
  832. }