Introduce UniquedStringImpl and SymbolImpl to separate symbolic strings from AtomicSt...
[WebKit-https.git] / Source / JavaScriptCore / builtins / BuiltinNames.h
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef BuiltinNames_h
27 #define BuiltinNames_h
28
29 #include "CommonIdentifiers.h"
30 #include "JSCBuiltins.h"
31
32 namespace JSC {
33     
34 #define INITIALISE_BUILTIN_NAMES(name) , m_##name(Identifier::fromString(vm, #name)), m_##name##PrivateName(Identifier::fromUid(PrivateName()))
35 #define DECLARE_BUILTIN_NAMES(name) const Identifier m_##name; const Identifier m_##name##PrivateName;
36 #define DECLARE_BUILTIN_IDENTIFIER_ACCESSOR(name) \
37     const Identifier& name##PublicName() const { return m_##name; } \
38     const Identifier& name##PrivateName() const { return m_##name##PrivateName; }
39
40 #define INITIALISE_BUILTIN_SYMBOLS(name) INITIALISE_BUILTIN_NAMES(name), m_##name##Symbol(Identifier::fromUid(PrivateName(PrivateName::Description, ASCIILiteral("Symbol." #name))))
41 #define DECLARE_BUILTIN_SYMBOLS(name) DECLARE_BUILTIN_NAMES(name) const Identifier m_##name##Symbol;
42 #define DECLARE_BUILTIN_SYMBOL_ACCESSOR(name) \
43     DECLARE_BUILTIN_IDENTIFIER_ACCESSOR(name) \
44     const Identifier& name##Symbol() const { return m_##name##Symbol; }
45
46 #define INITIALISE_PRIVATE_TO_PUBLIC_ENTRY(name) m_privateToPublicMap.add(m_##name##PrivateName.impl(), &m_##name);
47 #define INITIALISE_PUBLIC_TO_PRIVATE_ENTRY(name) m_publicToPrivateMap.add(m_##name.impl(), &m_##name##PrivateName);
48     
49 class BuiltinNames {
50     WTF_MAKE_NONCOPYABLE(BuiltinNames); WTF_MAKE_FAST_ALLOCATED;
51     
52 public:
53     BuiltinNames(VM* vm, CommonIdentifiers* commonIdentifiers)
54         : m_emptyIdentifier(commonIdentifiers->emptyIdentifier)
55         JSC_FOREACH_BUILTIN_FUNCTION_NAME(INITIALISE_BUILTIN_NAMES)
56         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(INITIALISE_BUILTIN_NAMES)
57         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALISE_BUILTIN_SYMBOLS)
58     {
59         JSC_FOREACH_BUILTIN_FUNCTION_NAME(INITIALISE_PRIVATE_TO_PUBLIC_ENTRY)
60         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(INITIALISE_PRIVATE_TO_PUBLIC_ENTRY)
61         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALISE_PRIVATE_TO_PUBLIC_ENTRY)
62         JSC_FOREACH_BUILTIN_FUNCTION_NAME(INITIALISE_PUBLIC_TO_PRIVATE_ENTRY)
63         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(INITIALISE_PUBLIC_TO_PRIVATE_ENTRY)
64         JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(INITIALISE_PUBLIC_TO_PRIVATE_ENTRY)
65     }
66
67     bool isPrivateName(SymbolImpl& uid) const;
68     bool isPrivateName(UniquedStringImpl& uid) const;
69     bool isPrivateName(const Identifier&) const;
70     const Identifier* getPrivateName(const Identifier&) const;
71     const Identifier& getPublicName(const Identifier&) const;
72     
73     JSC_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
74     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
75     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(DECLARE_BUILTIN_SYMBOL_ACCESSOR)
76
77 private:
78     Identifier m_emptyIdentifier;
79     JSC_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
80     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(DECLARE_BUILTIN_NAMES)
81     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(DECLARE_BUILTIN_SYMBOLS)
82     typedef HashMap<RefPtr<UniquedStringImpl>, const Identifier*, IdentifierRepHash> BuiltinNamesMap;
83     BuiltinNamesMap m_publicToPrivateMap;
84     BuiltinNamesMap m_privateToPublicMap;
85 };
86
87 #undef DECLARE_BUILTIN_NAMES
88 #undef INITIALISE_BUILTIN_NAMES
89 #undef DECLARE_BUILTIN_IDENTIFIER_ACCESSOR
90 #undef DECLARE_BUILTIN_SYMBOLS
91 #undef INITIALISE_BUILTIN_SYMBOLS
92 #undef DECLARE_BUILTIN_SYMBOL_ACCESSOR
93
94 inline bool BuiltinNames::isPrivateName(SymbolImpl& uid) const
95 {
96     return m_privateToPublicMap.contains(&uid);
97 }
98
99 inline bool BuiltinNames::isPrivateName(UniquedStringImpl& uid) const
100 {
101     if (!uid.isSymbol())
102         return false;
103     return m_privateToPublicMap.contains(&uid);
104 }
105
106 inline bool BuiltinNames::isPrivateName(const Identifier& ident) const
107 {
108     if (ident.isNull())
109         return false;
110     return isPrivateName(*ident.impl());
111 }
112
113 inline const Identifier* BuiltinNames::getPrivateName(const Identifier& ident) const
114 {
115     auto iter = m_publicToPrivateMap.find(ident.impl());
116     if (iter != m_publicToPrivateMap.end())
117         return iter->value;
118     return 0;
119 }
120
121 inline const Identifier& BuiltinNames::getPublicName(const Identifier& ident) const
122 {
123     auto iter = m_privateToPublicMap.find(ident.impl());
124     if (iter != m_privateToPublicMap.end())
125         return *iter->value;
126     return m_emptyIdentifier;
127 }
128
129
130 }
131
132 #endif