summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h
blob: 161cd8522bc18af62fa347dc6cadc5aac73db4a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Format.h>
#include <AK/Types.h>

namespace JS {

struct Attribute {
    enum {
        Configurable = 1 << 0,
        Enumerable = 1 << 1,
        Writable = 1 << 2,
        HasGetter = 1 << 3,
        HasSetter = 1 << 4,
        HasConfigurable = 1 << 5,
        HasEnumerable = 1 << 6,
        HasWritable = 1 << 7,
    };
};

class PropertyAttributes {
public:
    PropertyAttributes(u8 bits = 0)
    {
        m_bits = bits;
        if (bits & Attribute::Configurable)
            m_bits |= Attribute::HasConfigurable;
        if (bits & Attribute::Enumerable)
            m_bits |= Attribute::HasEnumerable;
        if (bits & Attribute::Writable)
            m_bits |= Attribute::HasWritable;
    }

    bool is_empty() const { return !m_bits; }

    bool has_configurable() const { return m_bits & Attribute::HasConfigurable; }
    bool has_enumerable() const { return m_bits & Attribute::HasEnumerable; }
    bool has_writable() const { return m_bits & Attribute::HasWritable; }
    bool has_getter() const { return m_bits & Attribute::HasGetter; }
    bool has_setter() const { return m_bits & Attribute::HasSetter; }

    bool is_configurable() const { return m_bits & Attribute::Configurable; }
    bool is_enumerable() const { return m_bits & Attribute::Enumerable; }
    bool is_writable() const { return m_bits & Attribute::Writable; }

    void set_has_configurable() { m_bits |= Attribute::HasConfigurable; }
    void set_has_enumerable() { m_bits |= Attribute::HasEnumerable; }
    void set_has_writable() { m_bits |= Attribute::HasWritable; }
    void set_configurable() { m_bits |= Attribute::Configurable; }
    void set_enumerable() { m_bits |= Attribute::Enumerable; }
    void set_writable() { m_bits |= Attribute::Writable; }
    void set_has_getter() { m_bits |= Attribute::HasGetter; }
    void set_has_setter() { m_bits |= Attribute::HasSetter; }

    bool operator==(const PropertyAttributes& other) const { return m_bits == other.m_bits; }
    bool operator!=(const PropertyAttributes& other) const { return m_bits != other.m_bits; }

    u8 bits() const { return m_bits; }

private:
    u8 m_bits;
};

const PropertyAttributes default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;

}

namespace AK {

template<>
struct Formatter<JS::PropertyAttributes> : Formatter<u8> {
    void format(FormatBuilder& builder, const JS::PropertyAttributes& attributes)
    {
        Formatter<u8>::format(builder, attributes.bits());
    }
};

}