summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PropertyName.h
blob: 45bae0f3474cff2c67f16ff6cda6524a5d30836e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/FlyString.h>
#include <LibJS/Runtime/StringOrSymbol.h>

namespace JS {

class PropertyName {
public:
    enum class Type {
        Invalid,
        Number,
        String,
        Symbol,
    };

    static PropertyName from_value(GlobalObject& global_object, Value value)
    {
        if (value.is_empty())
            return {};
        if (value.is_symbol())
            return &value.as_symbol();
        if (value.is_integer() && value.as_i32() >= 0)
            return value.as_i32();
        auto string = value.to_string(global_object);
        if (string.is_null())
            return {};
        return string;
    }

    PropertyName() { }

    PropertyName(i32 index)
        : m_type(Type::Number)
        , m_number(index)
    {
        VERIFY(index >= 0);
    }

    PropertyName(const char* chars)
        : m_type(Type::String)
        , m_string(FlyString(chars))
    {
    }

    PropertyName(const String& string)
        : m_type(Type::String)
        , m_string(FlyString(string))
    {
        VERIFY(!string.is_null());
    }

    PropertyName(const FlyString& string)
        : m_type(Type::String)
        , m_string(string)
    {
        VERIFY(!string.is_null());
    }

    PropertyName(Symbol* symbol)
        : m_type(Type::Symbol)
        , m_symbol(symbol)
    {
        VERIFY(symbol);
    }

    PropertyName(const StringOrSymbol& string_or_symbol)
    {
        if (string_or_symbol.is_string()) {
            m_string = string_or_symbol.as_string();
            m_type = Type::String;
        } else if (string_or_symbol.is_symbol()) {
            m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
            m_type = Type::Symbol;
        }
    }

    bool is_valid() const { return m_type != Type::Invalid; }
    bool is_number() const { return m_type == Type::Number; }
    bool is_string() const { return m_type == Type::String; }
    bool is_symbol() const { return m_type == Type::Symbol; }

    u32 as_number() const
    {
        VERIFY(is_number());
        return m_number;
    }

    const FlyString& as_string() const
    {
        VERIFY(is_string());
        return m_string;
    }

    const Symbol* as_symbol() const
    {
        VERIFY(is_symbol());
        return m_symbol;
    }

    String to_string() const
    {
        VERIFY(is_valid());
        VERIFY(!is_symbol());
        if (is_string())
            return as_string();
        return String::number(as_number());
    }

    StringOrSymbol to_string_or_symbol() const
    {
        VERIFY(is_valid());
        VERIFY(!is_number());
        if (is_string())
            return StringOrSymbol(as_string());
        return StringOrSymbol(as_symbol());
    }

    Value to_value(VM& vm) const
    {
        if (is_string())
            return js_string(vm, m_string);
        if (is_number())
            return Value(m_number);
        if (is_symbol())
            return m_symbol;
        return js_undefined();
    }

private:
    Type m_type { Type::Invalid };
    FlyString m_string;
    Symbol* m_symbol { nullptr };
    u32 m_number { 0 };
};

}