Object.cpp 33 KB

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