Object.cpp 26 KB

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