Object.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/NativeFunction.h>
  31. #include <LibJS/Runtime/NativeProperty.h>
  32. #include <LibJS/Runtime/Object.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. Object::Object()
  37. {
  38. m_shape = interpreter().empty_object_shape();
  39. m_shape->set_prototype_without_transition(interpreter().object_prototype());
  40. }
  41. Object::~Object()
  42. {
  43. }
  44. Object* Object::prototype()
  45. {
  46. return shape().prototype();
  47. }
  48. const Object* Object::prototype() const
  49. {
  50. return shape().prototype();
  51. }
  52. void Object::set_prototype(Object* new_prototype)
  53. {
  54. m_shape = m_shape->create_prototype_transition(new_prototype);
  55. }
  56. bool Object::has_prototype(const Object* prototype) const
  57. {
  58. for (auto* object = this->prototype(); object; object = object->prototype()) {
  59. if (object == prototype)
  60. return true;
  61. }
  62. return false;
  63. }
  64. Optional<Value> Object::get_own_property(const Object& this_object, const FlyString& property_name) const
  65. {
  66. auto metadata = shape().lookup(property_name);
  67. if (!metadata.has_value())
  68. return {};
  69. auto value_here = m_storage[metadata.value().offset];
  70. ASSERT(!value_here.is_empty());
  71. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  72. auto& native_property = static_cast<const NativeProperty&>(value_here.as_object());
  73. auto& interpreter = const_cast<Object*>(this)->interpreter();
  74. auto& call_frame = interpreter.push_call_frame();
  75. call_frame.this_value = const_cast<Object*>(&this_object);
  76. auto result = native_property.get(interpreter);
  77. interpreter.pop_call_frame();
  78. return result;
  79. }
  80. return value_here;
  81. }
  82. void Object::set_shape(Shape& new_shape)
  83. {
  84. m_storage.resize(new_shape.property_count());
  85. m_shape = &new_shape;
  86. }
  87. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value)
  88. {
  89. auto metadata = shape().lookup(property_name);
  90. if (!metadata.has_value()) {
  91. auto* new_shape = m_shape->create_put_transition(property_name, 0);
  92. set_shape(*new_shape);
  93. metadata = shape().lookup(property_name);
  94. ASSERT(metadata.has_value());
  95. }
  96. auto value_here = m_storage[metadata.value().offset];
  97. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  98. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  99. auto& interpreter = const_cast<Object*>(this)->interpreter();
  100. auto& call_frame = interpreter.push_call_frame();
  101. call_frame.this_value = &this_object;
  102. native_property.set(interpreter, value);
  103. interpreter.pop_call_frame();
  104. } else {
  105. m_storage[metadata.value().offset] = value;
  106. }
  107. return true;
  108. }
  109. Optional<Value> Object::get_by_index(i32 property_index) const
  110. {
  111. if (property_index < 0)
  112. return get(String::number(property_index));
  113. const Object* object = this;
  114. while (object) {
  115. if (static_cast<size_t>(property_index) < object->m_elements.size()) {
  116. auto value = object->m_elements[property_index];
  117. if (value.is_empty())
  118. return {};
  119. return value;
  120. }
  121. object = object->prototype();
  122. }
  123. return {};
  124. }
  125. Optional<Value> Object::get(const FlyString& property_name) const
  126. {
  127. bool ok;
  128. i32 property_index = property_name.to_int(ok);
  129. if (ok && property_index >= 0)
  130. return get_by_index(property_index);
  131. const Object* object = this;
  132. while (object) {
  133. auto value = object->get_own_property(*this, property_name);
  134. if (value.has_value())
  135. return value.value();
  136. object = object->prototype();
  137. }
  138. return {};
  139. }
  140. Optional<Value> Object::get(PropertyName property_name) const
  141. {
  142. if (property_name.is_number())
  143. return get_by_index(property_name.as_number());
  144. return get(property_name.as_string());
  145. }
  146. void Object::put_by_index(i32 property_index, Value value)
  147. {
  148. ASSERT(!value.is_empty());
  149. if (property_index < 0)
  150. return put(String::number(property_index), value);
  151. // FIXME: Implement some kind of sparse storage for arrays with huge indices.
  152. if (static_cast<size_t>(property_index) >= m_elements.size())
  153. m_elements.resize(property_index + 1);
  154. m_elements[property_index] = value;
  155. }
  156. void Object::put(const FlyString& property_name, Value value)
  157. {
  158. ASSERT(!value.is_empty());
  159. bool ok;
  160. i32 property_index = property_name.to_int(ok);
  161. if (ok && property_index >= 0)
  162. return put_by_index(property_index, value);
  163. // If there's a setter in the prototype chain, we go to the setter.
  164. // Otherwise, it goes in the own property storage.
  165. Object* object = this;
  166. while (object) {
  167. auto metadata = object->shape().lookup(property_name);
  168. if (metadata.has_value()) {
  169. auto value_here = object->m_storage[metadata.value().offset];
  170. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  171. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  172. auto& interpreter = const_cast<Object*>(this)->interpreter();
  173. auto& call_frame = interpreter.push_call_frame();
  174. call_frame.this_value = this;
  175. native_property.set(interpreter, value);
  176. interpreter.pop_call_frame();
  177. return;
  178. }
  179. }
  180. object = object->prototype();
  181. }
  182. put_own_property(*this, property_name, value);
  183. }
  184. void Object::put(PropertyName property_name, Value value)
  185. {
  186. if (property_name.is_number())
  187. return put_by_index(property_name.as_number(), value);
  188. return put(property_name.as_string(), value);
  189. }
  190. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
  191. {
  192. auto* function = heap().allocate<NativeFunction>(move(native_function));
  193. function->put("length", Value(length));
  194. put(property_name, function);
  195. }
  196. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)
  197. {
  198. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  199. }
  200. void Object::visit_children(Cell::Visitor& visitor)
  201. {
  202. Cell::visit_children(visitor);
  203. visitor.visit(m_shape);
  204. for (auto& value : m_storage)
  205. visitor.visit(value);
  206. for (auto& value : m_elements)
  207. visitor.visit(value);
  208. }
  209. bool Object::has_own_property(const FlyString& property_name) const
  210. {
  211. bool ok;
  212. i32 property_index = property_name.to_int(ok);
  213. if (ok && property_index >= 0) {
  214. if (static_cast<size_t>(property_index) >= m_elements.size())
  215. return false;
  216. return !m_elements[property_index].is_empty();
  217. }
  218. return shape().lookup(property_name).has_value();
  219. }
  220. Value Object::to_primitive(PreferredType preferred_type) const
  221. {
  222. Value result = js_undefined();
  223. switch (preferred_type) {
  224. case PreferredType::Default:
  225. case PreferredType::Number: {
  226. result = value_of();
  227. if (result.is_object()) {
  228. result = to_string();
  229. }
  230. break;
  231. }
  232. case PreferredType::String: {
  233. result = to_string();
  234. if (result.is_object())
  235. result = value_of();
  236. break;
  237. }
  238. }
  239. ASSERT(!result.is_object());
  240. return result;
  241. }
  242. Value Object::to_string() const
  243. {
  244. auto to_string_property = get("toString");
  245. if (to_string_property.has_value()
  246. && to_string_property.value().is_object()
  247. && to_string_property.value().as_object().is_function()) {
  248. auto& to_string_function = static_cast<Function&>(to_string_property.value().as_object());
  249. return const_cast<Object*>(this)->interpreter().call(&to_string_function, const_cast<Object*>(this));
  250. }
  251. return js_string(heap(), String::format("[object %s]", class_name()));
  252. }
  253. }