summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h
blob: b3788bd897f60acf51a3557f9ebd1210eccf4957 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
 * 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/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());
    }
};

}