Object.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(const FlyString& property_name) const
  109. {
  110. const Object* object = this;
  111. while (object) {
  112. auto value = object->get_own_property(*this, property_name);
  113. if (value.has_value())
  114. return value.value();
  115. object = object->prototype();
  116. }
  117. return {};
  118. }
  119. void Object::put(const FlyString& property_name, Value value)
  120. {
  121. // If there's a setter in the prototype chain, we go to the setter.
  122. // Otherwise, it goes in the own property storage.
  123. Object* object = this;
  124. while (object) {
  125. auto metadata = object->shape().lookup(property_name);
  126. if (metadata.has_value()) {
  127. auto value_here = object->m_storage[metadata.value().offset];
  128. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  129. auto& native_property = static_cast<NativeProperty&>(value_here.as_object());
  130. auto& interpreter = const_cast<Object*>(this)->interpreter();
  131. auto& call_frame = interpreter.push_call_frame();
  132. call_frame.this_value = this;
  133. native_property.set(interpreter, value);
  134. interpreter.pop_call_frame();
  135. return;
  136. }
  137. }
  138. object = object->prototype();
  139. }
  140. put_own_property(*this, property_name, value);
  141. }
  142. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
  143. {
  144. auto* function = heap().allocate<NativeFunction>(move(native_function));
  145. function->put("length", Value(length));
  146. put(property_name, function);
  147. }
  148. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)
  149. {
  150. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  151. }
  152. void Object::visit_children(Cell::Visitor& visitor)
  153. {
  154. Cell::visit_children(visitor);
  155. visitor.visit(m_shape);
  156. }
  157. bool Object::has_own_property(const FlyString& property_name) const
  158. {
  159. return shape().lookup(property_name).has_value();
  160. }
  161. Value Object::to_primitive(PreferredType preferred_type) const
  162. {
  163. Value result = js_undefined();
  164. switch (preferred_type) {
  165. case PreferredType::Default:
  166. case PreferredType::Number: {
  167. result = value_of();
  168. if (result.is_object()) {
  169. result = to_string();
  170. }
  171. break;
  172. }
  173. case PreferredType::String: {
  174. result = to_string();
  175. if (result.is_object())
  176. result = value_of();
  177. break;
  178. }
  179. }
  180. ASSERT(!result.is_object());
  181. return result;
  182. }
  183. Value Object::to_string() const
  184. {
  185. return js_string(heap(), String::format("[object %s]", class_name()));
  186. }
  187. }