Object.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/Value.h>
  34. namespace JS {
  35. Object::Object()
  36. {
  37. set_prototype(interpreter().object_prototype());
  38. }
  39. Object::~Object()
  40. {
  41. }
  42. bool Object::has_prototype(const Object* prototype) const
  43. {
  44. for (auto* object = m_prototype; object; object = object->prototype()) {
  45. if (object == prototype)
  46. return true;
  47. }
  48. return false;
  49. }
  50. Optional<Value> Object::get_own_property(const Object& this_object, const FlyString& property_name) const
  51. {
  52. auto value_here = m_properties.get(property_name);
  53. if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object().is_native_property()) {
  54. auto& native_property = static_cast<const NativeProperty&>(value_here.value().as_object());
  55. auto& interpreter = const_cast<Object*>(this)->interpreter();
  56. auto& call_frame = interpreter.push_call_frame();
  57. call_frame.this_value = const_cast<Object*>(&this_object);
  58. auto result = native_property.get(interpreter);
  59. interpreter.pop_call_frame();
  60. return result;
  61. }
  62. return value_here;
  63. }
  64. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value)
  65. {
  66. auto value_here = m_properties.get(property_name);
  67. if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object().is_native_property()) {
  68. auto& native_property = static_cast<NativeProperty&>(value_here.value().as_object());
  69. auto& interpreter = const_cast<Object*>(this)->interpreter();
  70. auto& call_frame = interpreter.push_call_frame();
  71. call_frame.this_value = &this_object;
  72. dbg() << "put_own_property: " << &this_object << " . " << property_name << " = " << value;
  73. native_property.set(interpreter, value);
  74. interpreter.pop_call_frame();
  75. } else {
  76. m_properties.set(property_name, value);
  77. }
  78. return true;
  79. }
  80. Optional<Value> Object::get(const FlyString& property_name) const
  81. {
  82. const Object* object = this;
  83. while (object) {
  84. auto value = object->get_own_property(*this, property_name);
  85. if (value.has_value())
  86. return value.value();
  87. object = object->prototype();
  88. }
  89. return {};
  90. }
  91. void Object::put(const FlyString& property_name, Value value)
  92. {
  93. Object* object = this;
  94. while (object) {
  95. auto value_here = object->m_properties.get(property_name);
  96. if (value_here.has_value()) {
  97. if (value_here.value().is_object() && value_here.value().as_object().is_native_property()) {
  98. auto& native_property = static_cast<NativeProperty&>(value_here.value().as_object());
  99. auto& interpreter = const_cast<Object*>(this)->interpreter();
  100. auto& call_frame = interpreter.push_call_frame();
  101. call_frame.this_value = this;
  102. native_property.set(interpreter, value);
  103. interpreter.pop_call_frame();
  104. return;
  105. }
  106. if (object->put_own_property(*this, property_name, value))
  107. return;
  108. }
  109. object = object->prototype();
  110. }
  111. put_own_property(*this, property_name, value);
  112. }
  113. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function)
  114. {
  115. put(property_name, heap().allocate<NativeFunction>(move(native_function)));
  116. }
  117. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)
  118. {
  119. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  120. }
  121. void Object::visit_children(Cell::Visitor& visitor)
  122. {
  123. Cell::visit_children(visitor);
  124. if (m_prototype)
  125. visitor.visit(m_prototype);
  126. for (auto& it : m_properties)
  127. visitor.visit(it.value);
  128. }
  129. bool Object::has_own_property(const FlyString& property_name) const
  130. {
  131. return m_properties.get(property_name).has_value();
  132. }
  133. Value Object::to_primitive(PreferredType preferred_type) const
  134. {
  135. Value result = js_undefined();
  136. switch (preferred_type) {
  137. case PreferredType::Default:
  138. case PreferredType::Number: {
  139. result = value_of();
  140. if (result.is_object()) {
  141. result = to_string();
  142. }
  143. break;
  144. }
  145. case PreferredType::String: {
  146. result = to_string();
  147. if (result.is_object())
  148. result = value_of();
  149. break;
  150. }
  151. }
  152. ASSERT(!result.is_object());
  153. return result;
  154. }
  155. Value Object::to_string() const
  156. {
  157. return js_string(heap(), String::format("[object %s]", class_name()));
  158. }
  159. }