Object.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
  41. {
  42. return global_object.heap().allocate<Object>(global_object.object_prototype());
  43. }
  44. Object::Object(Object* prototype)
  45. {
  46. if (prototype) {
  47. m_shape = interpreter().global_object().empty_object_shape();
  48. set_prototype(prototype);
  49. } else {
  50. m_shape = interpreter().heap().allocate<Shape>();
  51. }
  52. }
  53. Object::~Object()
  54. {
  55. }
  56. Object* Object::prototype()
  57. {
  58. return shape().prototype();
  59. }
  60. const Object* Object::prototype() const
  61. {
  62. return shape().prototype();
  63. }
  64. void Object::set_prototype(Object* new_prototype)
  65. {
  66. if (prototype() == new_prototype)
  67. return;
  68. if (shape().is_unique()) {
  69. shape().set_prototype_without_transition(new_prototype);
  70. return;
  71. }
  72. m_shape = m_shape->create_prototype_transition(new_prototype);
  73. }
  74. bool Object::has_prototype(const Object* prototype) const
  75. {
  76. for (auto* object = this->prototype(); object; object = object->prototype()) {
  77. if (object == prototype)
  78. return true;
  79. }
  80. return false;
  81. }
  82. Value Object::get_own_property(const Object& this_object, PropertyName property_name) const
  83. {
  84. Value value_here;
  85. if (property_name.is_number()) {
  86. if (static_cast<size_t>(property_name.as_number()) >= m_elements.size())
  87. return {};
  88. value_here = m_elements[property_name.as_number()];
  89. } else {
  90. auto metadata = shape().lookup(property_name.as_string());
  91. if (!metadata.has_value())
  92. return {};
  93. value_here = m_storage[metadata.value().offset];
  94. }
  95. ASSERT(!value_here.is_empty());
  96. if (value_here.is_accessor()) {
  97. return value_here.as_accessor().call_getter(Value(const_cast<Object*>(this)));
  98. }
  99. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  100. auto& native_property = static_cast<const NativeProperty&>(value_here.as_object());
  101. auto& interpreter = const_cast<Object*>(this)->interpreter();
  102. auto& call_frame = interpreter.push_call_frame();
  103. call_frame.this_value = const_cast<Object*>(&this_object);
  104. auto result = native_property.get(interpreter);
  105. interpreter.pop_call_frame();
  106. return result;
  107. }
  108. return value_here;
  109. }
  110. Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, u8 attributes) const
  111. {
  112. auto* properties_array = Array::create(interpreter().global_object());
  113. // FIXME: Support generic iterables
  114. if (this_object.is_string_object()) {
  115. auto str = static_cast<const StringObject&>(this_object).primitive_string().string();
  116. for (size_t i = 0; i < str.length(); ++i) {
  117. if (kind == GetOwnPropertyMode::Key) {
  118. properties_array->define_property(i, js_string(interpreter(), String::number(i)));
  119. } else if (kind == GetOwnPropertyMode::Value) {
  120. properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i])));
  121. } else {
  122. auto* entry_array = Array::create(interpreter().global_object());
  123. entry_array->define_property(0, js_string(interpreter(), String::number(i)));
  124. entry_array->define_property(1, js_string(interpreter(), String::format("%c", str[i])));
  125. properties_array->define_property(i, entry_array);
  126. }
  127. }
  128. return properties_array;
  129. }
  130. size_t property_index = 0;
  131. for (size_t i = 0; i < m_elements.size(); ++i) {
  132. if (m_elements.at(i).is_empty())
  133. continue;
  134. if (kind == GetOwnPropertyMode::Key) {
  135. properties_array->put_by_index(property_index, js_string(interpreter(), String::number(i)));
  136. } else if (kind == GetOwnPropertyMode::Value) {
  137. properties_array->put_by_index(property_index, m_elements.at(i));
  138. } else {
  139. auto* entry_array = Array::create(interpreter().global_object());
  140. entry_array->put_by_index(0, js_string(interpreter(), String::number(i)));
  141. entry_array->put_by_index(1, m_elements.at(i));
  142. properties_array->put_by_index(property_index, entry_array);
  143. }
  144. ++property_index;
  145. }
  146. for (auto& it : this_object.shape().property_table_ordered()) {
  147. if (it.value.attributes & attributes) {
  148. size_t offset = it.value.offset + property_index;
  149. if (kind == GetOwnPropertyMode::Key) {
  150. properties_array->put_by_index(offset, js_string(interpreter(), it.key));
  151. } else if (kind == GetOwnPropertyMode::Value) {
  152. properties_array->put_by_index(offset, this_object.get(it.key));
  153. } else {
  154. auto* entry_array = Array::create(interpreter().global_object());
  155. entry_array->put_by_index(0, js_string(interpreter(), it.key));
  156. entry_array->put_by_index(1, this_object.get(it.key));
  157. properties_array->put_by_index(offset, entry_array);
  158. }
  159. }
  160. }
  161. return properties_array;
  162. }
  163. Value Object::get_own_property_descriptor(PropertyName property_name) const
  164. {
  165. Value value;
  166. u8 attributes;
  167. if (property_name.is_number()) {
  168. if (static_cast<size_t>(property_name.as_number()) >= m_elements.size())
  169. return {};
  170. value = m_elements[property_name.as_number()];
  171. attributes = default_attributes;
  172. } else {
  173. auto metadata = shape().lookup(property_name.as_string());
  174. if (!metadata.has_value())
  175. return js_undefined();
  176. value = get(property_name);
  177. if (interpreter().exception())
  178. return {};
  179. attributes = metadata.value().attributes;
  180. }
  181. auto* descriptor = Object::create_empty(interpreter(), interpreter().global_object());
  182. descriptor->define_property("enumerable", Value((attributes & Attribute::Enumerable) != 0));
  183. descriptor->define_property("configurable", Value((attributes & Attribute::Configurable) != 0));
  184. if (value.is_accessor()) {
  185. auto& pair = value.as_accessor();
  186. descriptor->define_property("get", pair.getter());
  187. descriptor->define_property("set", pair.setter());
  188. } else {
  189. descriptor->define_property("value", value.value_or(js_undefined()));
  190. descriptor->define_property("writable", Value((attributes & Attribute::Writable) != 0));
  191. }
  192. return descriptor;
  193. }
  194. void Object::set_shape(Shape& new_shape)
  195. {
  196. m_storage.resize(new_shape.property_count());
  197. m_shape = &new_shape;
  198. }
  199. bool Object::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
  200. {
  201. bool is_accessor_property = descriptor.has_property("get") || descriptor.has_property("set");
  202. u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
  203. if (interpreter().exception())
  204. return {};
  205. u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
  206. if (interpreter().exception())
  207. return {};
  208. u8 attributes = configurable | enumerable;
  209. if (is_accessor_property) {
  210. if (descriptor.has_property("value") || descriptor.has_property("writable")) {
  211. if (throw_exceptions)
  212. interpreter().throw_exception<TypeError>("Accessor property descriptors cannot specify a value or writable key");
  213. return false;
  214. }
  215. auto getter = descriptor.get("get").value_or(js_undefined());
  216. if (interpreter().exception())
  217. return {};
  218. auto setter = descriptor.get("set").value_or(js_undefined());
  219. if (interpreter().exception())
  220. return {};
  221. Function* getter_function { nullptr };
  222. Function* setter_function { nullptr };
  223. if (getter.is_function()) {
  224. getter_function = &getter.as_function();
  225. } else if (!getter.is_undefined()) {
  226. interpreter().throw_exception<TypeError>("Accessor descriptor's 'get' field must be a function or undefined");
  227. return false;
  228. }
  229. if (setter.is_function()) {
  230. setter_function = &setter.as_function();
  231. } else if (!setter.is_undefined()) {
  232. interpreter().throw_exception<TypeError>("Accessor descriptor's 'set' field must be a function or undefined");
  233. return false;
  234. }
  235. dbg() << "Defining new property " << property_name << " with accessor descriptor { attributes=" << attributes << ", "
  236. << "getter=" << getter.to_string_without_side_effects() << ", "
  237. << "setter=" << setter.to_string_without_side_effects() << "}";
  238. return define_property(property_name, Accessor::create(interpreter(), getter_function, setter_function), attributes, throw_exceptions);
  239. }
  240. auto value = descriptor.get("value");
  241. if (interpreter().exception())
  242. return {};
  243. u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
  244. if (interpreter().exception())
  245. return {};
  246. attributes |= writable;
  247. dbg() << "Defining new property " << property_name << " with data descriptor { attributes=" << attributes
  248. << ", value=" << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects()) << " }";
  249. return define_property(property_name, value, attributes, throw_exceptions);
  250. }
  251. bool Object::define_property(PropertyName property_name, Value value, u8 attributes, bool throw_exceptions)
  252. {
  253. if (property_name.is_number())
  254. return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  255. bool ok;
  256. i32 property_index = property_name.as_string().to_int(ok);
  257. if (ok && property_index >= 0)
  258. return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  259. return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  260. }
  261. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  262. {
  263. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  264. if (value.is_accessor()) {
  265. auto& accessor = value.as_accessor();
  266. if (accessor.getter())
  267. attributes |= Attribute::HasGet;
  268. if (accessor.setter())
  269. attributes |= Attribute::HasSet;
  270. }
  271. auto metadata = shape().lookup(property_name);
  272. bool new_property = !metadata.has_value();
  273. if (new_property) {
  274. if (!m_shape->is_unique() && shape().property_count() > 100) {
  275. // If you add more than 100 properties to an object, let's stop doing
  276. // transitions to avoid filling up the heap with shapes.
  277. ensure_shape_is_unique();
  278. }
  279. if (m_shape->is_unique()) {
  280. m_shape->add_property_to_unique_shape(property_name, attributes);
  281. m_storage.resize(m_shape->property_count());
  282. } else {
  283. set_shape(*m_shape->create_put_transition(property_name, attributes));
  284. }
  285. metadata = shape().lookup(property_name);
  286. ASSERT(metadata.has_value());
  287. }
  288. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
  289. dbg() << "Disallow reconfig of non-configurable property";
  290. if (throw_exceptions)
  291. interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property '%s'", property_name.characters()));
  292. return false;
  293. }
  294. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  295. if (m_shape->is_unique()) {
  296. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  297. } else {
  298. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  299. }
  300. metadata = shape().lookup(property_name);
  301. dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
  302. }
  303. auto value_here = m_storage[metadata.value().offset];
  304. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !(metadata.value().attributes & Attribute::Writable)) {
  305. dbg() << "Disallow write to non-writable property";
  306. return false;
  307. }
  308. if (value.is_empty())
  309. return true;
  310. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  311. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  312. auto& interpreter = const_cast<Object*>(this)->interpreter();
  313. auto& call_frame = interpreter.push_call_frame();
  314. call_frame.this_value = &this_object;
  315. native_property.set(interpreter, value);
  316. interpreter.pop_call_frame();
  317. } else {
  318. m_storage[metadata.value().offset] = value;
  319. }
  320. return true;
  321. }
  322. bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  323. {
  324. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  325. if (value.is_accessor()) {
  326. auto& accessor = value.as_accessor();
  327. if (accessor.getter())
  328. attributes |= Attribute::HasGet;
  329. if (accessor.setter())
  330. attributes |= Attribute::HasSet;
  331. }
  332. auto new_property = property_index >= m_elements.size();
  333. auto existing_property = new_property ? Value() : m_elements[property_index];
  334. auto existing_attributes = default_attributes;
  335. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(existing_attributes & Attribute::Configurable) && attributes != existing_attributes) {
  336. dbg() << "Disallow reconfig of non-configurable property";
  337. if (throw_exceptions)
  338. interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property %d", property_index));
  339. return false;
  340. }
  341. auto value_here = existing_property;
  342. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !(existing_attributes & Attribute::Writable)) {
  343. dbg() << "Disallow write to non-writable property";
  344. return false;
  345. }
  346. if (value.is_empty())
  347. return true;
  348. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  349. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  350. auto& interpreter = const_cast<Object*>(this)->interpreter();
  351. auto& call_frame = interpreter.push_call_frame();
  352. call_frame.this_value = &this_object;
  353. native_property.set(interpreter, value);
  354. interpreter.pop_call_frame();
  355. } else {
  356. if (new_property)
  357. m_elements.resize(property_index + 1);
  358. m_elements[property_index] = value;
  359. }
  360. return true;
  361. }
  362. Value Object::delete_property(PropertyName property_name)
  363. {
  364. ASSERT(property_name.is_valid());
  365. if (property_name.is_number()) {
  366. if (property_name.as_number() < static_cast<i32>(elements().size())) {
  367. elements()[property_name.as_number()] = {};
  368. return Value(true);
  369. }
  370. return Value(true);
  371. }
  372. auto metadata = shape().lookup(property_name.as_string());
  373. if (!metadata.has_value())
  374. return Value(true);
  375. if (!(metadata.value().attributes & Attribute::Configurable))
  376. return Value(false);
  377. size_t deleted_offset = metadata.value().offset;
  378. ensure_shape_is_unique();
  379. shape().remove_property_from_unique_shape(property_name.as_string(), deleted_offset);
  380. m_storage.remove(deleted_offset);
  381. return Value(true);
  382. }
  383. void Object::ensure_shape_is_unique()
  384. {
  385. if (shape().is_unique())
  386. return;
  387. m_shape = m_shape->create_unique_clone();
  388. }
  389. Value Object::get_by_index(u32 property_index) const
  390. {
  391. const Object* object = this;
  392. while (object) {
  393. if (is_string_object()) {
  394. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  395. if (property_index < string.length())
  396. return js_string(heap(), string.substring(property_index, 1));
  397. return js_undefined();
  398. }
  399. if (static_cast<size_t>(property_index) < object->m_elements.size()) {
  400. auto value = object->m_elements[property_index];
  401. if (value.is_empty())
  402. return {};
  403. return value;
  404. }
  405. object = object->prototype();
  406. }
  407. return {};
  408. }
  409. Value Object::get(PropertyName property_name) const
  410. {
  411. if (property_name.is_number())
  412. return get_by_index(property_name.as_number());
  413. auto property_string = property_name.to_string();
  414. bool ok;
  415. i32 property_index = property_string.to_int(ok);
  416. if (ok && property_index >= 0)
  417. return get_by_index(property_index);
  418. const Object* object = this;
  419. while (object) {
  420. auto value = object->get_own_property(*this, property_name);
  421. if (!value.is_empty())
  422. return value;
  423. object = object->prototype();
  424. }
  425. return {};
  426. }
  427. bool Object::put_by_index(u32 property_index, Value value)
  428. {
  429. ASSERT(!value.is_empty());
  430. // FIXME: Implement some kind of sparse storage for arrays with huge indices.
  431. // Also: Take attributes into account here
  432. if (static_cast<size_t>(property_index) >= m_elements.size())
  433. m_elements.resize(property_index + 1);
  434. m_elements[property_index] = value;
  435. return true;
  436. }
  437. bool Object::put(PropertyName property_name, Value value)
  438. {
  439. if (property_name.is_number())
  440. return put_by_index(property_name.as_number(), value);
  441. ASSERT(!value.is_empty());
  442. auto property_string = property_name.to_string();
  443. bool ok;
  444. i32 property_index = property_string.to_int(ok);
  445. if (ok && property_index >= 0)
  446. return put_by_index(property_index, value);
  447. // If there's a setter in the prototype chain, we go to the setter.
  448. // Otherwise, it goes in the own property storage.
  449. Object* object = this;
  450. while (object) {
  451. auto metadata = object->shape().lookup(property_string);
  452. if (metadata.has_value()) {
  453. auto value_here = object->m_storage[metadata.value().offset];
  454. if (value_here.is_accessor()) {
  455. value_here.as_accessor().call_setter(Value(this), value);
  456. return true;
  457. }
  458. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  459. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  460. auto& interpreter = const_cast<Object*>(this)->interpreter();
  461. auto& call_frame = interpreter.push_call_frame();
  462. call_frame.this_value = this;
  463. native_property.set(interpreter, value);
  464. interpreter.pop_call_frame();
  465. return true;
  466. }
  467. }
  468. object = object->prototype();
  469. }
  470. return put_own_property(*this, property_string, value, default_attributes, PutOwnPropertyMode::Put);
  471. }
  472. bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attribute)
  473. {
  474. auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
  475. function->define_property("length", Value(length), Attribute::Configurable);
  476. function->define_property("name", js_string(heap(), property_name), Attribute::Configurable);
  477. return define_property(property_name, function, attribute);
  478. }
  479. bool Object::define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute)
  480. {
  481. return define_property(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attribute);
  482. }
  483. void Object::visit_children(Cell::Visitor& visitor)
  484. {
  485. Cell::visit_children(visitor);
  486. visitor.visit(m_shape);
  487. for (auto& value : m_storage)
  488. visitor.visit(value);
  489. for (auto& value : m_elements)
  490. visitor.visit(value);
  491. }
  492. bool Object::has_property(PropertyName property_name) const
  493. {
  494. const Object* object = this;
  495. while (object) {
  496. if (object->has_own_property(property_name))
  497. return true;
  498. object = object->prototype();
  499. }
  500. return false;
  501. }
  502. bool Object::has_own_property(PropertyName property_name) const
  503. {
  504. auto has_indexed_property = [&](u32 index) -> bool {
  505. if (is_string_object())
  506. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  507. if (static_cast<size_t>(index) >= m_elements.size())
  508. return false;
  509. return !m_elements[index].is_empty();
  510. };
  511. if (property_name.is_number())
  512. return has_indexed_property(property_name.as_number());
  513. bool ok;
  514. i32 property_index = property_name.as_string().to_int(ok);
  515. if (ok && property_index >= 0)
  516. return has_indexed_property(property_index);
  517. return shape().lookup(property_name.as_string()).has_value();
  518. }
  519. Value Object::to_primitive(PreferredType preferred_type) const
  520. {
  521. Value result = js_undefined();
  522. switch (preferred_type) {
  523. case PreferredType::Default:
  524. case PreferredType::Number: {
  525. result = value_of();
  526. if (result.is_object()) {
  527. result = to_string();
  528. }
  529. break;
  530. }
  531. case PreferredType::String: {
  532. result = to_string();
  533. if (result.is_object())
  534. result = value_of();
  535. break;
  536. }
  537. }
  538. ASSERT(!result.is_object());
  539. return result;
  540. }
  541. Value Object::to_string() const
  542. {
  543. auto to_string_property = get("toString");
  544. if (to_string_property.is_function()) {
  545. auto& to_string_function = to_string_property.as_function();
  546. auto& interpreter = const_cast<Object*>(this)->interpreter();
  547. auto to_string_result = interpreter.call(to_string_function, const_cast<Object*>(this));
  548. if (to_string_result.is_object())
  549. interpreter.throw_exception<TypeError>("Cannot convert object to string");
  550. if (interpreter.exception())
  551. return {};
  552. auto* string = to_string_result.to_primitive_string(interpreter);
  553. if (interpreter.exception())
  554. return {};
  555. return string;
  556. }
  557. return js_string(heap(), String::format("[object %s]", class_name()));
  558. }
  559. }