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>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
class Object : public Cell {
|
2020-03-07 19:42:11 +01:00
|
|
|
public:
|
2020-03-08 19:23:58 +01:00
|
|
|
Object();
|
|
|
|
virtual ~Object();
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-27 12:54:18 +01:00
|
|
|
Optional<Value> get(const FlyString& property_name) const;
|
2020-03-22 11:07:55 +01:00
|
|
|
void put(const FlyString& property_name, Value);
|
2020-03-21 14:37:34 +01:00
|
|
|
|
2020-03-24 14:46:05 +01:00
|
|
|
virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const;
|
|
|
|
virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value);
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-28 22:48:35 +01:00
|
|
|
void put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>);
|
2020-03-29 00:37:33 +01:00
|
|
|
void put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter);
|
2020-03-13 11:06:32 +01:00
|
|
|
|
2020-03-20 20:29:57 +01:00
|
|
|
virtual bool is_array() 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-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-03-15 15:01:10 +01:00
|
|
|
Object* prototype() { return m_prototype; }
|
|
|
|
const Object* prototype() const { return m_prototype; }
|
|
|
|
void set_prototype(Object* prototype) { m_prototype = prototype; }
|
2020-03-28 19:48:12 +01:00
|
|
|
bool has_prototype(const Object* prototype) const;
|
2020-03-15 15:01:10 +01:00
|
|
|
|
2020-03-22 11:07:55 +01:00
|
|
|
bool has_own_property(const FlyString& property_name) const;
|
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-03-26 12:19:01 +01:00
|
|
|
const HashMap<FlyString, Value>& own_properties() const { return m_properties; }
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
private:
|
2020-03-22 11:07:55 +01:00
|
|
|
HashMap<FlyString, Value> m_properties;
|
2020-03-15 15:01:10 +01:00
|
|
|
Object* m_prototype { nullptr };
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|