2020-03-07 15:42:11 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2020-03-15 19:19:41 -03:00
|
|
|
#include <AK/String.h>
|
2020-03-07 15:42:11 -03:00
|
|
|
#include <LibJS/Forward.h>
|
2020-03-16 10:20:30 -03:00
|
|
|
#include <LibJS/Runtime/Cell.h>
|
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2020-04-06 12:08:23 -03:00
|
|
|
#include <LibJS/Runtime/PropertyName.h>
|
2020-04-28 03:05:02 -03:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-03-16 10:20:30 -03:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 15:42:11 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2020-04-28 03:05:02 -03:00
|
|
|
const u8 default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
|
|
|
|
|
|
2020-03-08 15:23:58 -03:00
|
|
|
class Object : public Cell {
|
2020-03-07 15:42:11 -03:00
|
|
|
public:
|
2020-04-18 05:27:57 -03:00
|
|
|
static Object* create_empty(Interpreter&, GlobalObject&);
|
|
|
|
|
|
|
|
|
|
explicit Object(Object* prototype);
|
2020-03-08 15:23:58 -03:00
|
|
|
virtual ~Object();
|
2020-03-07 15:42:11 -03:00
|
|
|
|
2020-05-01 07:06:27 -03:00
|
|
|
enum class GetOwnPropertyMode {
|
|
|
|
|
Key,
|
|
|
|
|
Value,
|
|
|
|
|
KeyAndValue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class PutOwnPropertyMode {
|
|
|
|
|
Put,
|
|
|
|
|
DefineProperty,
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
Shape& shape() { return *m_shape; }
|
|
|
|
|
const Shape& shape() const { return *m_shape; }
|
|
|
|
|
|
2020-04-26 08:53:40 -03:00
|
|
|
Value delete_property(PropertyName);
|
|
|
|
|
|
2020-04-25 13:43:34 -03:00
|
|
|
virtual Value get_by_index(i32 property_index) const;
|
|
|
|
|
Value get(const FlyString& property_name) const;
|
|
|
|
|
Value get(PropertyName) const;
|
2020-04-06 11:53:02 -03:00
|
|
|
|
2020-04-30 16:01:54 -03:00
|
|
|
virtual bool put_by_index(i32 property_index, Value, u8 attributes = default_attributes);
|
|
|
|
|
bool put(const FlyString& property_name, Value, u8 attributes = default_attributes);
|
|
|
|
|
bool put(PropertyName, Value, u8 attributes = default_attributes);
|
2020-03-21 10:37:34 -03:00
|
|
|
|
2020-04-25 13:43:34 -03:00
|
|
|
Value get_own_property(const Object& this_object, const FlyString& property_name) const;
|
2020-05-01 07:06:27 -03:00
|
|
|
Value get_own_properties(const Object& this_object, GetOwnPropertyMode, u8 attributes = Attribute::Configurable | Attribute::Enumerable | Attribute::Writable) const;
|
|
|
|
|
Value get_own_property_descriptor(const FlyString& property_name) const;
|
2020-04-09 17:15:26 -03:00
|
|
|
|
2020-05-01 07:06:27 -03:00
|
|
|
bool define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions = true);
|
|
|
|
|
bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode, bool throw_exceptions = true);
|
2020-03-07 15:42:11 -03:00
|
|
|
|
2020-04-30 16:01:54 -03:00
|
|
|
bool put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes);
|
|
|
|
|
bool put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes);
|
2020-03-13 07:06:32 -03:00
|
|
|
|
2020-03-20 16:29:57 -03:00
|
|
|
virtual bool is_array() const { return false; }
|
2020-04-07 00:51:16 -03:00
|
|
|
virtual bool is_boolean() const { return false; }
|
2020-03-29 20:21:56 -03:00
|
|
|
virtual bool is_date() const { return false; }
|
|
|
|
|
virtual bool is_error() const { return false; }
|
2020-03-07 15:42:11 -03:00
|
|
|
virtual bool is_function() const { return false; }
|
2020-03-12 15:53:31 -03:00
|
|
|
virtual bool is_native_function() const { return false; }
|
2020-04-19 17:03:02 -03:00
|
|
|
virtual bool is_bound_function() const { return false; }
|
2020-03-15 14:15:44 -03:00
|
|
|
virtual bool is_native_property() const { return false; }
|
2020-03-29 20:21:56 -03:00
|
|
|
virtual bool is_string_object() const { return false; }
|
2020-03-07 15:42:11 -03:00
|
|
|
|
2020-03-08 15:23:58 -03:00
|
|
|
virtual const char* class_name() const override { return "Object"; }
|
2020-03-09 18:11:22 -03:00
|
|
|
virtual void visit_children(Cell::Visitor&) override;
|
2020-03-07 15:42:11 -03:00
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
Object* prototype();
|
|
|
|
|
const Object* prototype() const;
|
|
|
|
|
void set_prototype(Object*);
|
2020-03-28 15:48:12 -03:00
|
|
|
bool has_prototype(const Object* prototype) const;
|
2020-03-15 11:01:10 -03:00
|
|
|
|
2020-04-30 16:03:40 -03:00
|
|
|
bool has_property(const FlyString& property_name) const;
|
2020-03-22 07:07:55 -03:00
|
|
|
bool has_own_property(const FlyString& property_name) const;
|
2020-04-30 16:03:40 -03:00
|
|
|
|
2020-03-15 19:19:41 -03:00
|
|
|
enum class PreferredType {
|
|
|
|
|
Default,
|
|
|
|
|
String,
|
|
|
|
|
Number,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
|
|
|
|
|
virtual Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
|
|
|
|
|
virtual Value to_string() const;
|
2020-03-15 11:25:43 -03:00
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
Value get_direct(size_t index) const { return m_storage[index]; }
|
2020-03-26 08:19:01 -03:00
|
|
|
|
2020-04-06 11:53:02 -03:00
|
|
|
const Vector<Value>& elements() const { return m_elements; }
|
|
|
|
|
Vector<Value>& elements() { return m_elements; }
|
|
|
|
|
|
2020-03-07 15:42:11 -03:00
|
|
|
private:
|
2020-04-02 14:32:21 -03:00
|
|
|
void set_shape(Shape&);
|
2020-04-26 08:53:40 -03:00
|
|
|
void ensure_shape_is_unique();
|
2020-04-02 14:32:21 -03:00
|
|
|
|
|
|
|
|
Shape* m_shape { nullptr };
|
|
|
|
|
Vector<Value> m_storage;
|
2020-04-06 11:53:02 -03:00
|
|
|
Vector<Value> m_elements;
|
2020-03-07 15:42:11 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|