Object.cpp 28 KB

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