Object.cpp 31 KB

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