Object.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Optional<Value> Object::get_own_property(const Object& this_object, const FlyString& property_name) const
  43. {
  44. auto value_here = m_properties.get(property_name);
  45. if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property())
  46. return static_cast<NativeProperty*>(value_here.value().as_object())->get(&const_cast<Object&>(this_object));
  47. return value_here;
  48. }
  49. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value)
  50. {
  51. auto value_here = m_properties.get(property_name);
  52. if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) {
  53. static_cast<NativeProperty*>(value_here.value().as_object())->set(&this_object, value);
  54. } else {
  55. m_properties.set(property_name, value);
  56. }
  57. return true;
  58. }
  59. Optional<Value> Object::get(const FlyString& property_name) const
  60. {
  61. const Object* object = this;
  62. while (object) {
  63. auto value = object->get_own_property(*this, property_name);
  64. if (value.has_value())
  65. return value.value();
  66. object = object->prototype();
  67. }
  68. return {};
  69. }
  70. void Object::put(const FlyString& property_name, Value value)
  71. {
  72. Object* object = this;
  73. while (object) {
  74. auto value_here = object->m_properties.get(property_name);
  75. if (value_here.has_value()) {
  76. if (value_here.value().is_object() && value_here.value().as_object()->is_native_property()) {
  77. static_cast<NativeProperty*>(value_here.value().as_object())->set(const_cast<Object*>(this), value);
  78. return;
  79. }
  80. if (object->put_own_property(*this, property_name, value))
  81. return;
  82. }
  83. object = object->prototype();
  84. }
  85. put_own_property(*this, property_name, value);
  86. }
  87. void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)> native_function)
  88. {
  89. put(property_name, heap().allocate<NativeFunction>(move(native_function)));
  90. }
  91. void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter)
  92. {
  93. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  94. }
  95. void Object::visit_children(Cell::Visitor& visitor)
  96. {
  97. Cell::visit_children(visitor);
  98. if (m_prototype)
  99. visitor.visit(m_prototype);
  100. for (auto& it : m_properties)
  101. visitor.visit(it.value);
  102. }
  103. bool Object::has_own_property(const FlyString& property_name) const
  104. {
  105. return m_properties.get(property_name).has_value();
  106. }
  107. Value Object::to_primitive(PreferredType preferred_type) const
  108. {
  109. Value result = js_undefined();
  110. switch (preferred_type) {
  111. case PreferredType::Default:
  112. case PreferredType::Number: {
  113. result = value_of();
  114. if (result.is_object()) {
  115. result = to_string();
  116. }
  117. break;
  118. }
  119. case PreferredType::String: {
  120. result = to_string();
  121. if (result.is_object())
  122. result = value_of();
  123. break;
  124. }
  125. }
  126. ASSERT(!result.is_object());
  127. return result;
  128. }
  129. Value Object::to_string() const
  130. {
  131. return js_string(heap(), String::format("[object %s]", class_name()));
  132. }
  133. }