Object.cpp 32 KB

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