2020-03-07 19:42:11 +01: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-16 00:19:41 +02:00
|
|
|
#include <AK/String.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Cell.h>
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2020-04-06 17:08:23 +02:00
|
|
|
#include <LibJS/Runtime/PropertyName.h>
|
2020-04-27 23:05:02 -07:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-04-27 23:05:02 -07:00
|
|
|
const u8 default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
class Object : public Cell {
|
2020-03-07 19:42:11 +01:00
|
|
|
public:
|
2020-04-18 10:27:57 +02:00
|
|
|
static Object* create_empty(Interpreter&, GlobalObject&);
|
|
|
|
|
|
|
|
explicit Object(Object* prototype);
|
2020-03-08 19:23:58 +01:00
|
|
|
virtual ~Object();
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-05-01 11:06:27 +01:00
|
|
|
enum class GetOwnPropertyMode {
|
|
|
|
Key,
|
|
|
|
Value,
|
|
|
|
KeyAndValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class PutOwnPropertyMode {
|
|
|
|
Put,
|
|
|
|
DefineProperty,
|
|
|
|
};
|
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape& shape() { return *m_shape; }
|
|
|
|
const Shape& shape() const { return *m_shape; }
|
|
|
|
|
2020-04-26 13:53:40 +02:00
|
|
|
Value delete_property(PropertyName);
|
|
|
|
|
2020-04-25 18:43:34 +02:00
|
|
|
virtual Value get_by_index(i32 property_index) const;
|
|
|
|
Value get(const FlyString& property_name) const;
|
|
|
|
Value get(PropertyName) const;
|
2020-04-06 16:53:02 +02:00
|
|
|
|
2020-04-30 20:01:54 +01: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 14:37:34 +01:00
|
|
|
|
2020-04-25 18:43:34 +02:00
|
|
|
Value get_own_property(const Object& this_object, const FlyString& property_name) const;
|
2020-05-01 11:06:27 +01: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 22:15:26 +02:00
|
|
|
|
2020-05-01 11:06:27 +01: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 19:42:11 +01:00
|
|
|
|
2020-04-30 20:01:54 +01: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 11:06:32 +01:00
|
|
|
|
2020-03-20 20:29:57 +01:00
|
|
|
virtual bool is_array() const { return false; }
|
2020-04-06 22:51:16 -05:00
|
|
|
virtual bool is_boolean() const { return false; }
|
2020-03-30 00:21:56 +01:00
|
|
|
virtual bool is_date() const { return false; }
|
|
|
|
virtual bool is_error() const { return false; }
|
2020-03-07 19:42:11 +01:00
|
|
|
virtual bool is_function() const { return false; }
|
2020-03-12 19:53:31 +01:00
|
|
|
virtual bool is_native_function() const { return false; }
|
2020-04-19 15:03:02 -05:00
|
|
|
virtual bool is_bound_function() const { return false; }
|
2020-03-15 18:15:44 +01:00
|
|
|
virtual bool is_native_property() const { return false; }
|
2020-03-30 00:21:56 +01:00
|
|
|
virtual bool is_string_object() const { return false; }
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
virtual const char* class_name() const override { return "Object"; }
|
2020-03-09 22:11:22 +01:00
|
|
|
virtual void visit_children(Cell::Visitor&) override;
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Object* prototype();
|
|
|
|
const Object* prototype() const;
|
|
|
|
void set_prototype(Object*);
|
2020-03-28 19:48:12 +01:00
|
|
|
bool has_prototype(const Object* prototype) const;
|
2020-03-15 15:01:10 +01:00
|
|
|
|
2020-04-30 20:03:40 +01:00
|
|
|
bool has_property(const FlyString& property_name) const;
|
2020-03-22 11:07:55 +01:00
|
|
|
bool has_own_property(const FlyString& property_name) const;
|
2020-04-30 20:03:40 +01:00
|
|
|
|
2020-03-16 00:19:41 +02: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 15:25:43 +01:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Value get_direct(size_t index) const { return m_storage[index]; }
|
2020-03-26 12:19:01 +01:00
|
|
|
|
2020-04-06 16:53:02 +02:00
|
|
|
const Vector<Value>& elements() const { return m_elements; }
|
|
|
|
Vector<Value>& elements() { return m_elements; }
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
private:
|
2020-04-02 19:32:21 +02:00
|
|
|
void set_shape(Shape&);
|
2020-04-26 13:53:40 +02:00
|
|
|
void ensure_shape_is_unique();
|
2020-04-02 19:32:21 +02:00
|
|
|
|
|
|
|
Shape* m_shape { nullptr };
|
|
|
|
Vector<Value> m_storage;
|
2020-04-06 16:53:02 +02:00
|
|
|
Vector<Value> m_elements;
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|