Object.cpp 33 KB

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