Object.cpp 31 KB

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