Object.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  71. auto& native_property = static_cast<const NativeProperty&>(value_here.as_object());
  72. auto& interpreter = const_cast<Object*>(this)->interpreter();
  73. auto& call_frame = interpreter.push_call_frame();
  74. call_frame.this_value = const_cast<Object*>(&this_object);
  75. auto result = native_property.get(interpreter);
  76. interpreter.pop_call_frame();
  77. return result;
  78. }
  79. return value_here;
  80. }
  81. void Object::set_shape(Shape& new_shape)
  82. {
  83. m_storage.resize(new_shape.property_count());
  84. m_shape = &new_shape;
  85. }
  86. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value)
  87. {
  88. auto metadata = shape().lookup(property_name);
  89. if (!metadata.has_value()) {
  90. auto* new_shape = m_shape->create_put_transition(property_name, 0);
  91. set_shape(*new_shape);
  92. metadata = shape().lookup(property_name);
  93. ASSERT(metadata.has_value());
  94. }
  95. auto value_here = m_storage[metadata.value().offset];
  96. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  97. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  98. auto& interpreter = const_cast<Object*>(this)->interpreter();
  99. auto& call_frame = interpreter.push_call_frame();
  100. call_frame.this_value = &this_object;
  101. native_property.set(interpreter, value);
  102. interpreter.pop_call_frame();
  103. } else {
  104. m_storage[metadata.value().offset] = value;
  105. }
  106. return true;
  107. }
  108. Optional<Value> Object::get_by_index(i32 property_index) const
  109. {
  110. if (property_index < 0)
  111. return get(String::number(property_index));
  112. const Object* object = this;
  113. while (object) {
  114. if (static_cast<size_t>(property_index) < object->m_elements.size())
  115. return object->m_elements[property_index];
  116. object = object->prototype();
  117. }
  118. return {};
  119. }
  120. Optional<Value> Object::get(const FlyString& property_name) const
  121. {
  122. bool ok;
  123. i32 property_index = property_name.to_int(ok);
  124. if (ok && property_index >= 0)
  125. return get_by_index(property_index);
  126. const Object* object = this;
  127. while (object) {
  128. auto value = object->get_own_property(*this, property_name);
  129. if (value.has_value())
  130. return value.value();
  131. object = object->prototype();
  132. }
  133. return {};
  134. }
  135. void Object::put_by_index(i32 property_index, Value value)
  136. {
  137. if (property_index < 0)
  138. return put(String::number(property_index), value);
  139. // FIXME: Implement some kind of sparse storage for arrays with huge indices.
  140. if (static_cast<size_t>(property_index) >= m_elements.size())
  141. m_elements.resize(property_index + 1);
  142. m_elements[property_index] = value;
  143. }
  144. void Object::put(const FlyString& property_name, Value value)
  145. {
  146. bool ok;
  147. i32 property_index = property_name.to_int(ok);
  148. if (ok && property_index >= 0)
  149. return put_by_index(property_index, value);
  150. // If there's a setter in the prototype chain, we go to the setter.
  151. // Otherwise, it goes in the own property storage.
  152. Object* object = this;
  153. while (object) {
  154. auto metadata = object->shape().lookup(property_name);
  155. if (metadata.has_value()) {
  156. auto value_here = object->m_storage[metadata.value().offset];
  157. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  158. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  159. auto& interpreter = const_cast<Object*>(this)->interpreter();
  160. auto& call_frame = interpreter.push_call_frame();
  161. call_frame.this_value = this;
  162. native_property.set(interpreter, value);
  163. interpreter.pop_call_frame();
  164. return;
  165. }
  166. }
  167. object = object->prototype();
  168. }
  169. put_own_property(*this, property_name, value);
  170. }
  171. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
  172. {
  173. auto* function = heap().allocate<NativeFunction>(move(native_function));
  174. function->put("length", Value(length));
  175. put(property_name, function);
  176. }
  177. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)
  178. {
  179. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  180. }
  181. void Object::visit_children(Cell::Visitor& visitor)
  182. {
  183. Cell::visit_children(visitor);
  184. visitor.visit(m_shape);
  185. for (auto& value : m_storage)
  186. visitor.visit(value);
  187. for (auto& value : m_elements)
  188. visitor.visit(value);
  189. }
  190. bool Object::has_own_property(const FlyString& property_name) const
  191. {
  192. bool ok;
  193. i32 property_index = property_name.to_int(ok);
  194. if (ok && property_index >= 0)
  195. return static_cast<size_t>(property_index) < m_elements.size();
  196. return shape().lookup(property_name).has_value();
  197. }
  198. Value Object::to_primitive(PreferredType preferred_type) const
  199. {
  200. Value result = js_undefined();
  201. switch (preferred_type) {
  202. case PreferredType::Default:
  203. case PreferredType::Number: {
  204. result = value_of();
  205. if (result.is_object()) {
  206. result = to_string();
  207. }
  208. break;
  209. }
  210. case PreferredType::String: {
  211. result = to_string();
  212. if (result.is_object())
  213. result = value_of();
  214. break;
  215. }
  216. }
  217. ASSERT(!result.is_object());
  218. return result;
  219. }
  220. Value Object::to_string() const
  221. {
  222. auto to_string_property = get("toString");
  223. if (to_string_property.has_value()
  224. && to_string_property.value().is_object()
  225. && to_string_property.value().as_object().is_function()) {
  226. auto& to_string_function = static_cast<Function&>(to_string_property.value().as_object());
  227. return const_cast<Object*>(this)->interpreter().call(&to_string_function, const_cast<Object*>(this));
  228. }
  229. return js_string(heap(), String::format("[object %s]", class_name()));
  230. }
  231. }