Serenity Operating System
at hosted 102 lines 4.0 kB view raw
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 27#pragma once 28 29#include <AK/HashMap.h> 30#include <AK/String.h> 31#include <LibJS/Forward.h> 32#include <LibJS/Runtime/Cell.h> 33#include <LibJS/Runtime/PrimitiveString.h> 34#include <LibJS/Runtime/PropertyName.h> 35#include <LibJS/Runtime/Value.h> 36 37namespace JS { 38 39class Object : public Cell { 40public: 41 Object(); 42 virtual ~Object(); 43 44 Shape& shape() { return *m_shape; } 45 const Shape& shape() const { return *m_shape; } 46 47 Optional<Value> get_by_index(i32 property_index) const; 48 Optional<Value> get(const FlyString& property_name) const; 49 Optional<Value> get(PropertyName) const; 50 51 void put_by_index(i32 property_index, Value); 52 void put(const FlyString& property_name, Value); 53 void put(PropertyName, Value); 54 55 virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const; 56 virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value); 57 58 void put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0); 59 void put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter); 60 61 virtual bool is_array() const { return false; } 62 virtual bool is_boolean() const { return false; } 63 virtual bool is_date() const { return false; } 64 virtual bool is_error() const { return false; } 65 virtual bool is_function() const { return false; } 66 virtual bool is_native_function() const { return false; } 67 virtual bool is_native_property() const { return false; } 68 virtual bool is_string_object() const { return false; } 69 70 virtual const char* class_name() const override { return "Object"; } 71 virtual void visit_children(Cell::Visitor&) override; 72 73 Object* prototype(); 74 const Object* prototype() const; 75 void set_prototype(Object*); 76 bool has_prototype(const Object* prototype) const; 77 78 bool has_own_property(const FlyString& property_name) const; 79 enum class PreferredType { 80 Default, 81 String, 82 Number, 83 }; 84 85 virtual Value value_of() const { return Value(const_cast<Object*>(this)); } 86 virtual Value to_primitive(PreferredType preferred_type = PreferredType::Default) const; 87 virtual Value to_string() const; 88 89 Value get_direct(size_t index) const { return m_storage[index]; } 90 91 const Vector<Value>& elements() const { return m_elements; } 92 Vector<Value>& elements() { return m_elements; } 93 94private: 95 void set_shape(Shape&); 96 97 Shape* m_shape { nullptr }; 98 Vector<Value> m_storage; 99 Vector<Value> m_elements; 100}; 101 102}