Object.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/Value.h>
  37. namespace JS {
  38. Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
  39. {
  40. return global_object.heap().allocate<Object>(global_object.object_prototype());
  41. }
  42. Object::Object(Object* prototype)
  43. {
  44. if (prototype) {
  45. m_shape = interpreter().global_object().empty_object_shape();
  46. set_prototype(prototype);
  47. } else {
  48. m_shape = interpreter().heap().allocate<Shape>();
  49. }
  50. }
  51. Object::~Object()
  52. {
  53. }
  54. Object* Object::prototype()
  55. {
  56. return shape().prototype();
  57. }
  58. const Object* Object::prototype() const
  59. {
  60. return shape().prototype();
  61. }
  62. void Object::set_prototype(Object* new_prototype)
  63. {
  64. if (prototype() == new_prototype)
  65. return;
  66. if (shape().is_unique()) {
  67. shape().set_prototype_without_transition(new_prototype);
  68. return;
  69. }
  70. m_shape = m_shape->create_prototype_transition(new_prototype);
  71. }
  72. bool Object::has_prototype(const Object* prototype) const
  73. {
  74. for (auto* object = this->prototype(); object; object = object->prototype()) {
  75. if (object == prototype)
  76. return true;
  77. }
  78. return false;
  79. }
  80. Value Object::get_own_property(const Object& this_object, const FlyString& property_name) const
  81. {
  82. auto metadata = shape().lookup(property_name);
  83. if (!metadata.has_value())
  84. return {};
  85. auto value_here = m_storage[metadata.value().offset];
  86. ASSERT(!value_here.is_empty());
  87. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  88. auto& native_property = static_cast<const NativeProperty&>(value_here.as_object());
  89. auto& interpreter = const_cast<Object*>(this)->interpreter();
  90. auto& call_frame = interpreter.push_call_frame();
  91. call_frame.this_value = const_cast<Object*>(&this_object);
  92. auto result = native_property.get(interpreter);
  93. interpreter.pop_call_frame();
  94. return result;
  95. }
  96. return value_here;
  97. }
  98. void Object::set_shape(Shape& new_shape)
  99. {
  100. m_storage.resize(new_shape.property_count());
  101. m_shape = &new_shape;
  102. }
  103. void Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode)
  104. {
  105. auto metadata = shape().lookup(property_name);
  106. bool new_property = !metadata.has_value();
  107. if (new_property) {
  108. if (m_shape->is_unique()) {
  109. m_shape->add_property_to_unique_shape(property_name, attributes);
  110. m_storage.resize(m_shape->property_count());
  111. } else {
  112. set_shape(*m_shape->create_put_transition(property_name, attributes));
  113. }
  114. metadata = shape().lookup(property_name);
  115. ASSERT(metadata.has_value());
  116. }
  117. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
  118. dbg() << "Disallow reconfig of non-configurable property";
  119. interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
  120. return;
  121. }
  122. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  123. if (m_shape->is_unique()) {
  124. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  125. } else {
  126. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  127. }
  128. metadata = shape().lookup(property_name);
  129. dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
  130. }
  131. if (!new_property && mode == PutOwnPropertyMode::Put && !(metadata.value().attributes & Attribute::Writable)) {
  132. dbg() << "Disallow write to non-writable property";
  133. return;
  134. }
  135. if (value.is_empty())
  136. return;
  137. auto value_here = m_storage[metadata.value().offset];
  138. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  139. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  140. auto& interpreter = const_cast<Object*>(this)->interpreter();
  141. auto& call_frame = interpreter.push_call_frame();
  142. call_frame.this_value = &this_object;
  143. native_property.set(interpreter, value);
  144. interpreter.pop_call_frame();
  145. } else {
  146. m_storage[metadata.value().offset] = value;
  147. }
  148. }
  149. Value Object::delete_property(PropertyName property_name)
  150. {
  151. ASSERT(property_name.is_valid());
  152. if (property_name.is_number()) {
  153. if (property_name.as_number() < static_cast<i32>(elements().size())) {
  154. elements()[property_name.as_number()] = {};
  155. return Value(true);
  156. }
  157. return Value(true);
  158. }
  159. auto metadata = shape().lookup(property_name.as_string());
  160. if (!metadata.has_value())
  161. return Value(true);
  162. if (!(metadata.value().attributes & Attribute::Configurable))
  163. return Value(false);
  164. size_t deleted_offset = metadata.value().offset;
  165. ensure_shape_is_unique();
  166. shape().remove_property_from_unique_shape(property_name.as_string(), deleted_offset);
  167. m_storage.remove(deleted_offset);
  168. return Value(true);
  169. }
  170. void Object::ensure_shape_is_unique()
  171. {
  172. if (shape().is_unique())
  173. return;
  174. m_shape = m_shape->create_unique_clone();
  175. }
  176. Value Object::get_by_index(i32 property_index) const
  177. {
  178. if (property_index < 0)
  179. return get(String::number(property_index));
  180. const Object* object = this;
  181. while (object) {
  182. if (static_cast<size_t>(property_index) < object->m_elements.size()) {
  183. auto value = object->m_elements[property_index];
  184. if (value.is_empty())
  185. return {};
  186. return value;
  187. }
  188. object = object->prototype();
  189. }
  190. return {};
  191. }
  192. Value Object::get(const FlyString& property_name) const
  193. {
  194. bool ok;
  195. i32 property_index = property_name.to_int(ok);
  196. if (ok && property_index >= 0)
  197. return get_by_index(property_index);
  198. const Object* object = this;
  199. while (object) {
  200. auto value = object->get_own_property(*this, property_name);
  201. if (!value.is_empty())
  202. return value;
  203. object = object->prototype();
  204. }
  205. return {};
  206. }
  207. Value Object::get(PropertyName property_name) const
  208. {
  209. if (property_name.is_number())
  210. return get_by_index(property_name.as_number());
  211. return get(property_name.as_string());
  212. }
  213. void Object::put_by_index(i32 property_index, Value value, u8 attributes)
  214. {
  215. ASSERT(!value.is_empty());
  216. if (property_index < 0)
  217. return put(String::number(property_index), value, attributes);
  218. // FIXME: Implement some kind of sparse storage for arrays with huge indices.
  219. // Also: Take attributes into account here
  220. if (static_cast<size_t>(property_index) >= m_elements.size())
  221. m_elements.resize(property_index + 1);
  222. m_elements[property_index] = value;
  223. }
  224. void Object::put(const FlyString& property_name, Value value, u8 attributes)
  225. {
  226. ASSERT(!value.is_empty());
  227. bool ok;
  228. i32 property_index = property_name.to_int(ok);
  229. if (ok && property_index >= 0)
  230. return put_by_index(property_index, value, attributes);
  231. // If there's a setter in the prototype chain, we go to the setter.
  232. // Otherwise, it goes in the own property storage.
  233. Object* object = this;
  234. while (object) {
  235. auto metadata = object->shape().lookup(property_name);
  236. if (metadata.has_value()) {
  237. auto value_here = object->m_storage[metadata.value().offset];
  238. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  239. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  240. auto& interpreter = const_cast<Object*>(this)->interpreter();
  241. auto& call_frame = interpreter.push_call_frame();
  242. call_frame.this_value = this;
  243. native_property.set(interpreter, value);
  244. interpreter.pop_call_frame();
  245. return;
  246. }
  247. }
  248. object = object->prototype();
  249. }
  250. put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put);
  251. }
  252. void Object::put(PropertyName property_name, Value value, u8 attributes)
  253. {
  254. if (property_name.is_number())
  255. return put_by_index(property_name.as_number(), value, attributes);
  256. return put(property_name.as_string(), value, attributes);
  257. }
  258. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attributes)
  259. {
  260. auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
  261. function->put("length", Value(length), Attribute::Configurable);
  262. put(property_name, function, attributes);
  263. }
  264. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attributes)
  265. {
  266. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attributes);
  267. }
  268. void Object::visit_children(Cell::Visitor& visitor)
  269. {
  270. Cell::visit_children(visitor);
  271. visitor.visit(m_shape);
  272. for (auto& value : m_storage)
  273. visitor.visit(value);
  274. for (auto& value : m_elements)
  275. visitor.visit(value);
  276. }
  277. bool Object::has_own_property(const FlyString& property_name) const
  278. {
  279. bool ok;
  280. i32 property_index = property_name.to_int(ok);
  281. if (ok && property_index >= 0) {
  282. if (static_cast<size_t>(property_index) >= m_elements.size())
  283. return false;
  284. return !m_elements[property_index].is_empty();
  285. }
  286. return shape().lookup(property_name).has_value();
  287. }
  288. Value Object::to_primitive(PreferredType preferred_type) const
  289. {
  290. Value result = js_undefined();
  291. switch (preferred_type) {
  292. case PreferredType::Default:
  293. case PreferredType::Number: {
  294. result = value_of();
  295. if (result.is_object()) {
  296. result = to_string();
  297. }
  298. break;
  299. }
  300. case PreferredType::String: {
  301. result = to_string();
  302. if (result.is_object())
  303. result = value_of();
  304. break;
  305. }
  306. }
  307. ASSERT(!result.is_object());
  308. return result;
  309. }
  310. Value Object::to_string() const
  311. {
  312. auto to_string_property = get("toString");
  313. if (!to_string_property.is_empty()
  314. && to_string_property.is_object()
  315. && to_string_property.as_object().is_function()) {
  316. auto& to_string_function = static_cast<Function&>(to_string_property.as_object());
  317. return const_cast<Object*>(this)->interpreter().call(&to_string_function, const_cast<Object*>(this));
  318. }
  319. return js_string(heap(), String::format("[object %s]", class_name()));
  320. }
  321. }