Object.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/Array.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/NativeFunction.h>
  33. #include <LibJS/Runtime/NativeProperty.h>
  34. #include <LibJS/Runtime/Object.h>
  35. #include <LibJS/Runtime/Shape.h>
  36. #include <LibJS/Runtime/StringObject.h>
  37. #include <LibJS/Runtime/Value.h>
  38. namespace JS {
  39. Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
  40. {
  41. return global_object.heap().allocate<Object>(global_object.object_prototype());
  42. }
  43. Object::Object(Object* prototype)
  44. {
  45. if (prototype) {
  46. m_shape = interpreter().global_object().empty_object_shape();
  47. set_prototype(prototype);
  48. } else {
  49. m_shape = interpreter().heap().allocate<Shape>();
  50. }
  51. }
  52. Object::~Object()
  53. {
  54. }
  55. Object* Object::prototype()
  56. {
  57. return shape().prototype();
  58. }
  59. const Object* Object::prototype() const
  60. {
  61. return shape().prototype();
  62. }
  63. void Object::set_prototype(Object* new_prototype)
  64. {
  65. if (prototype() == new_prototype)
  66. return;
  67. if (shape().is_unique()) {
  68. shape().set_prototype_without_transition(new_prototype);
  69. return;
  70. }
  71. m_shape = m_shape->create_prototype_transition(new_prototype);
  72. }
  73. bool Object::has_prototype(const Object* prototype) const
  74. {
  75. for (auto* object = this->prototype(); object; object = object->prototype()) {
  76. if (object == prototype)
  77. return true;
  78. }
  79. return false;
  80. }
  81. Value Object::get_own_property(const Object& this_object, const FlyString& property_name) const
  82. {
  83. auto metadata = shape().lookup(property_name);
  84. if (!metadata.has_value())
  85. return {};
  86. auto value_here = m_storage[metadata.value().offset];
  87. ASSERT(!value_here.is_empty());
  88. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  89. auto& native_property = static_cast<const NativeProperty&>(value_here.as_object());
  90. auto& interpreter = const_cast<Object*>(this)->interpreter();
  91. auto& call_frame = interpreter.push_call_frame();
  92. call_frame.this_value = const_cast<Object*>(&this_object);
  93. auto result = native_property.get(interpreter);
  94. interpreter.pop_call_frame();
  95. return result;
  96. }
  97. return value_here;
  98. }
  99. Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, u8 attributes) const
  100. {
  101. auto* properties_array = Array::create(interpreter().global_object());
  102. // FIXME: Support generic iterables
  103. if (this_object.is_string_object()) {
  104. auto str = static_cast<const StringObject&>(this_object).primitive_string().string();
  105. for (size_t i = 0; i < str.length(); ++i) {
  106. if (kind == GetOwnPropertyMode::Key) {
  107. properties_array->put_by_index(i, js_string(interpreter(), String::number(i)));
  108. } else if (kind == GetOwnPropertyMode::Value) {
  109. properties_array->put_by_index(i, js_string(interpreter(), String::format("%c", str[i])));
  110. } else {
  111. auto* entry_array = Array::create(interpreter().global_object());
  112. entry_array->put_by_index(0, js_string(interpreter(), String::number(i)));
  113. entry_array->put_by_index(1, js_string(interpreter(), String::format("%c", str[i])));
  114. properties_array->put_by_index(i, entry_array);
  115. }
  116. }
  117. return properties_array;
  118. }
  119. size_t property_index = 0;
  120. for (size_t i = 0; i < m_elements.size(); ++i) {
  121. if (m_elements.at(i).is_empty())
  122. continue;
  123. if (kind == GetOwnPropertyMode::Key) {
  124. properties_array->put_by_index(property_index, js_string(interpreter(), String::number(i)));
  125. } else if (kind == GetOwnPropertyMode::Value) {
  126. properties_array->put_by_index(property_index, m_elements.at(i));
  127. } else {
  128. auto* entry_array = Array::create(interpreter().global_object());
  129. entry_array->put_by_index(0, js_string(interpreter(), String::number(i)));
  130. entry_array->put_by_index(1, m_elements.at(i));
  131. properties_array->put_by_index(property_index, entry_array);
  132. }
  133. ++property_index;
  134. }
  135. for (auto& it : this_object.shape().property_table_ordered()) {
  136. if (it.value.attributes & attributes) {
  137. size_t offset = it.value.offset + property_index;
  138. if (kind == GetOwnPropertyMode::Key) {
  139. properties_array->put_by_index(offset, js_string(interpreter(), it.key));
  140. } else if (kind == GetOwnPropertyMode::Value) {
  141. properties_array->put_by_index(offset, this_object.get(it.key));
  142. } else {
  143. auto* entry_array = Array::create(interpreter().global_object());
  144. entry_array->put_by_index(0, js_string(interpreter(), it.key));
  145. entry_array->put_by_index(1, this_object.get(it.key));
  146. properties_array->put_by_index(offset, entry_array);
  147. }
  148. }
  149. }
  150. return properties_array;
  151. }
  152. Value Object::get_own_property_descriptor(const FlyString& property_name) const
  153. {
  154. auto metadata = shape().lookup(property_name);
  155. if (!metadata.has_value())
  156. return js_undefined();
  157. auto value = get(property_name);
  158. if (interpreter().exception())
  159. return {};
  160. auto* descriptor = Object::create_empty(interpreter(), interpreter().global_object());
  161. descriptor->put("value", value.value_or(js_undefined()));
  162. descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable)));
  163. descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable)));
  164. descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable)));
  165. return descriptor;
  166. }
  167. void Object::set_shape(Shape& new_shape)
  168. {
  169. m_storage.resize(new_shape.property_count());
  170. m_shape = &new_shape;
  171. }
  172. bool Object::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
  173. {
  174. auto value = descriptor.get("value");
  175. u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
  176. u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
  177. u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
  178. u8 attributes = configurable | enumerable | writable;
  179. dbg() << "Defining new property " << property_name << " with descriptor { " << configurable << ", " << enumerable << ", " << writable << ", attributes=" << attributes << " }";
  180. return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  181. }
  182. bool Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode, bool throw_exceptions)
  183. {
  184. auto metadata = shape().lookup(property_name);
  185. bool new_property = !metadata.has_value();
  186. if (new_property) {
  187. if (!m_shape->is_unique() && shape().property_count() > 100) {
  188. // If you add more than 100 properties to an object, let's stop doing
  189. // transitions to avoid filling up the heap with shapes.
  190. ensure_shape_is_unique();
  191. }
  192. if (m_shape->is_unique()) {
  193. m_shape->add_property_to_unique_shape(property_name, attributes);
  194. m_storage.resize(m_shape->property_count());
  195. } else {
  196. set_shape(*m_shape->create_put_transition(property_name, attributes));
  197. }
  198. metadata = shape().lookup(property_name);
  199. ASSERT(metadata.has_value());
  200. }
  201. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
  202. dbg() << "Disallow reconfig of non-configurable property";
  203. if (throw_exceptions)
  204. interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
  205. return false;
  206. }
  207. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  208. if (m_shape->is_unique()) {
  209. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  210. } else {
  211. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  212. }
  213. metadata = shape().lookup(property_name);
  214. dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
  215. }
  216. if (!new_property && mode == PutOwnPropertyMode::Put && !(metadata.value().attributes & Attribute::Writable)) {
  217. dbg() << "Disallow write to non-writable property";
  218. return false;
  219. }
  220. if (value.is_empty())
  221. return true;
  222. auto value_here = m_storage[metadata.value().offset];
  223. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  224. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  225. auto& interpreter = const_cast<Object*>(this)->interpreter();
  226. auto& call_frame = interpreter.push_call_frame();
  227. call_frame.this_value = &this_object;
  228. native_property.set(interpreter, value);
  229. interpreter.pop_call_frame();
  230. } else {
  231. m_storage[metadata.value().offset] = value;
  232. }
  233. return true;
  234. }
  235. Value Object::delete_property(PropertyName property_name)
  236. {
  237. ASSERT(property_name.is_valid());
  238. if (property_name.is_number()) {
  239. if (property_name.as_number() < static_cast<i32>(elements().size())) {
  240. elements()[property_name.as_number()] = {};
  241. return Value(true);
  242. }
  243. return Value(true);
  244. }
  245. auto metadata = shape().lookup(property_name.as_string());
  246. if (!metadata.has_value())
  247. return Value(true);
  248. if (!(metadata.value().attributes & Attribute::Configurable))
  249. return Value(false);
  250. size_t deleted_offset = metadata.value().offset;
  251. ensure_shape_is_unique();
  252. shape().remove_property_from_unique_shape(property_name.as_string(), deleted_offset);
  253. m_storage.remove(deleted_offset);
  254. return Value(true);
  255. }
  256. void Object::ensure_shape_is_unique()
  257. {
  258. if (shape().is_unique())
  259. return;
  260. m_shape = m_shape->create_unique_clone();
  261. }
  262. Value Object::get_by_index(i32 property_index) const
  263. {
  264. if (property_index < 0)
  265. return get(String::number(property_index));
  266. const Object* object = this;
  267. while (object) {
  268. if (is_string_object()) {
  269. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  270. if (property_index < (i32)string.length())
  271. return js_string(heap(), string.substring(property_index, 1));
  272. return js_undefined();
  273. }
  274. if (static_cast<size_t>(property_index) < object->m_elements.size()) {
  275. auto value = object->m_elements[property_index];
  276. if (value.is_empty())
  277. return {};
  278. return value;
  279. }
  280. object = object->prototype();
  281. }
  282. return {};
  283. }
  284. Value Object::get(const FlyString& property_name) const
  285. {
  286. bool ok;
  287. i32 property_index = property_name.to_int(ok);
  288. if (ok && property_index >= 0)
  289. return get_by_index(property_index);
  290. const Object* object = this;
  291. while (object) {
  292. auto value = object->get_own_property(*this, property_name);
  293. if (!value.is_empty())
  294. return value;
  295. object = object->prototype();
  296. }
  297. return {};
  298. }
  299. Value Object::get(PropertyName property_name) const
  300. {
  301. if (property_name.is_number())
  302. return get_by_index(property_name.as_number());
  303. return get(property_name.as_string());
  304. }
  305. bool Object::put_by_index(i32 property_index, Value value, u8 attributes)
  306. {
  307. ASSERT(!value.is_empty());
  308. if (property_index < 0)
  309. return put(String::number(property_index), value, attributes);
  310. // FIXME: Implement some kind of sparse storage for arrays with huge indices.
  311. // Also: Take attributes into account here
  312. if (static_cast<size_t>(property_index) >= m_elements.size())
  313. m_elements.resize(property_index + 1);
  314. m_elements[property_index] = value;
  315. return true;
  316. }
  317. bool Object::put(const FlyString& property_name, Value value, u8 attributes)
  318. {
  319. ASSERT(!value.is_empty());
  320. bool ok;
  321. i32 property_index = property_name.to_int(ok);
  322. if (ok && property_index >= 0)
  323. return put_by_index(property_index, value, attributes);
  324. // If there's a setter in the prototype chain, we go to the setter.
  325. // Otherwise, it goes in the own property storage.
  326. Object* object = this;
  327. while (object) {
  328. auto metadata = object->shape().lookup(property_name);
  329. if (metadata.has_value()) {
  330. auto value_here = object->m_storage[metadata.value().offset];
  331. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  332. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  333. auto& interpreter = const_cast<Object*>(this)->interpreter();
  334. auto& call_frame = interpreter.push_call_frame();
  335. call_frame.this_value = this;
  336. native_property.set(interpreter, value);
  337. interpreter.pop_call_frame();
  338. return true;
  339. }
  340. }
  341. object = object->prototype();
  342. }
  343. return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put);
  344. }
  345. bool Object::put(PropertyName property_name, Value value, u8 attributes)
  346. {
  347. if (property_name.is_number())
  348. return put_by_index(property_name.as_number(), value, attributes);
  349. return put(property_name.as_string(), value, attributes);
  350. }
  351. bool Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attributes)
  352. {
  353. auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
  354. function->put("length", Value(length), Attribute::Configurable);
  355. function->put("name", js_string(heap(), property_name), Attribute::Configurable);
  356. return put(property_name, function, attributes);
  357. }
  358. bool Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attributes)
  359. {
  360. return put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attributes);
  361. }
  362. void Object::visit_children(Cell::Visitor& visitor)
  363. {
  364. Cell::visit_children(visitor);
  365. visitor.visit(m_shape);
  366. for (auto& value : m_storage)
  367. visitor.visit(value);
  368. for (auto& value : m_elements)
  369. visitor.visit(value);
  370. }
  371. bool Object::has_property(const FlyString& property_name) const
  372. {
  373. const Object* object = this;
  374. while (object) {
  375. if (object->has_own_property(property_name))
  376. return true;
  377. object = object->prototype();
  378. }
  379. return false;
  380. }
  381. bool Object::has_own_property(const FlyString& property_name) const
  382. {
  383. bool ok;
  384. i32 property_index = property_name.to_int(ok);
  385. if (ok && property_index >= 0) {
  386. if (is_string_object())
  387. return property_index < (i32) static_cast<const StringObject*>(this)->primitive_string().string().length();
  388. if (static_cast<size_t>(property_index) >= m_elements.size())
  389. return false;
  390. return !m_elements[property_index].is_empty();
  391. }
  392. return shape().lookup(property_name).has_value();
  393. }
  394. Value Object::to_primitive(PreferredType preferred_type) const
  395. {
  396. Value result = js_undefined();
  397. switch (preferred_type) {
  398. case PreferredType::Default:
  399. case PreferredType::Number: {
  400. result = value_of();
  401. if (result.is_object()) {
  402. result = to_string();
  403. }
  404. break;
  405. }
  406. case PreferredType::String: {
  407. result = to_string();
  408. if (result.is_object())
  409. result = value_of();
  410. break;
  411. }
  412. }
  413. ASSERT(!result.is_object());
  414. return result;
  415. }
  416. Value Object::to_string() const
  417. {
  418. auto to_string_property = get("toString");
  419. if (!to_string_property.is_empty() && to_string_property.is_function()) {
  420. auto& to_string_function = to_string_property.as_function();
  421. auto& interpreter = const_cast<Object*>(this)->interpreter();
  422. auto to_string_result = interpreter.call(to_string_function, const_cast<Object*>(this));
  423. if (to_string_result.is_object())
  424. interpreter.throw_exception<TypeError>("Cannot convert object to string");
  425. if (interpreter.exception())
  426. return {};
  427. return js_string(heap(), to_string_result.to_string());
  428. }
  429. return js_string(heap(), String::format("[object %s]", class_name()));
  430. }
  431. }