Object.cpp 11 KB

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