Object.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <LibJS/Forward.h>
  30. #include <LibJS/Runtime/Cell.h>
  31. #include <LibJS/Runtime/PrimitiveString.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace JS {
  34. class Object : public Cell {
  35. public:
  36. Object();
  37. virtual ~Object();
  38. Value get(const FlyString& property_name) const;
  39. void put(const FlyString& property_name, Value);
  40. virtual Optional<Value> get_own_property(const FlyString& property_name) const;
  41. virtual bool put_own_property(const FlyString& property_name, Value);
  42. void put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)>);
  43. void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter);
  44. virtual bool is_error() const { return false; }
  45. virtual bool is_array() const { return false; }
  46. virtual bool is_function() const { return false; }
  47. virtual bool is_native_function() const { return false; }
  48. virtual bool is_string_object() const { return false; }
  49. virtual bool is_native_property() const { return false; }
  50. virtual const char* class_name() const override { return "Object"; }
  51. virtual void visit_children(Cell::Visitor&) override;
  52. Object* prototype() { return m_prototype; }
  53. const Object* prototype() const { return m_prototype; }
  54. void set_prototype(Object* prototype) { m_prototype = prototype; }
  55. bool has_own_property(const FlyString& property_name) const;
  56. enum class PreferredType {
  57. Default,
  58. String,
  59. Number,
  60. };
  61. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  62. virtual Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
  63. virtual Value to_string() const;
  64. private:
  65. HashMap<FlyString, Value> m_properties;
  66. Object* m_prototype { nullptr };
  67. };
  68. }