Object.cpp 30 KB

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