Object.cpp 11 KB

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