Object.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/NativeFunction.h>
  30. #include <LibJS/Runtime/NativeProperty.h>
  31. #include <LibJS/Runtime/Object.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace JS {
  34. Object::Object()
  35. {
  36. set_prototype(interpreter().object_prototype());
  37. }
  38. Object::~Object()
  39. {
  40. }
  41. Value Object::get(String property_name) const
  42. {
  43. const Object* object = this;
  44. while (object) {
  45. auto value = object->m_properties.get(property_name);
  46. if (value.has_value()) {
  47. if (value.value().is_object() && value.value().as_object()->is_native_property())
  48. return static_cast<const NativeProperty*>(value.value().as_object())->get(const_cast<Object*>(this));
  49. return value.value();
  50. }
  51. object = object->prototype();
  52. }
  53. return js_undefined();
  54. }
  55. void Object::put(String property_name, Value value)
  56. {
  57. m_properties.set(property_name, move(value));
  58. }
  59. void Object::put_native_function(String property_name, AK::Function<Value(Object*, Vector<Value>)> native_function)
  60. {
  61. put(property_name, heap().allocate<NativeFunction>(move(native_function)));
  62. }
  63. void Object::put_native_property(String property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter)
  64. {
  65. put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
  66. }
  67. void Object::visit_children(Cell::Visitor& visitor)
  68. {
  69. Cell::visit_children(visitor);
  70. if (m_prototype)
  71. visitor.visit(m_prototype);
  72. for (auto& it : m_properties)
  73. visitor.visit(it.value);
  74. }
  75. bool Object::has_own_property(const String& property_name) const
  76. {
  77. return m_properties.get(property_name).has_value();
  78. }
  79. Value Object::to_primitive(PreferredType preferred_type) const
  80. {
  81. Value result = js_undefined();
  82. switch (preferred_type) {
  83. case PreferredType::Default:
  84. case PreferredType::Number: {
  85. result = value_of();
  86. if (result.is_object()) {
  87. result = to_string();
  88. }
  89. break;
  90. }
  91. case PreferredType::String: {
  92. result = to_string();
  93. if (result.is_object())
  94. result = value_of();
  95. break;
  96. }
  97. }
  98. ASSERT(!result.is_object());
  99. return result;
  100. }
  101. Value Object::to_string() const
  102. {
  103. return js_string(heap(), String::format("[object %s]", class_name()));
  104. }
  105. }