Object.cpp 11 KB

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