79acc14db1483a2ffab218b4f3c32086663c3483
[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(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(StringImpl* uid) const;
68     bool isPrivateName(const Identifier&) const;
69     const Identifier* getPrivateName(const Identifier&) const;
70     const Identifier& getPublicName(const Identifier&) const;
71     
72     JSC_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
73     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
74     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(DECLARE_BUILTIN_SYMBOL_ACCESSOR)
75
76 private:
77     Identifier m_emptyIdentifier;
78     JSC_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
79     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(DECLARE_BUILTIN_NAMES)
80     JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(DECLARE_BUILTIN_SYMBOLS)
81     typedef HashMap<RefPtr<StringImpl>, const Identifier*, IdentifierRepHash> BuiltinNamesMap;
82     BuiltinNamesMap m_publicToPrivateMap;
83     BuiltinNamesMap m_privateToPublicMap;
84 };
85
86 #undef DECLARE_BUILTIN_NAMES
87 #undef INITIALISE_BUILTIN_NAMES
88 #undef DECLARE_BUILTIN_IDENTIFIER_ACCESSOR
89 #undef DECLARE_BUILTIN_SYMBOLS
90 #undef INITIALISE_BUILTIN_SYMBOLS
91 #undef DECLARE_BUILTIN_SYMBOL_ACCESSOR
92
93 inline bool BuiltinNames::isPrivateName(StringImpl* uid) const
94 {
95     if (!uid->isSymbol())
96         return false;
97     return m_privateToPublicMap.contains(uid);
98 }
99
100 inline bool BuiltinNames::isPrivateName(const Identifier& ident) const
101 {
102     return isPrivateName(ident.impl());
103 }
104
105 inline const Identifier* BuiltinNames::getPrivateName(const Identifier& ident) const
106 {
107     auto iter = m_publicToPrivateMap.find(ident.impl());
108     if (iter != m_publicToPrivateMap.end())
109         return iter->value;
110     return 0;
111 }
112
113 inline const Identifier& BuiltinNames::getPublicName(const Identifier& ident) const
114 {
115     auto iter = m_privateToPublicMap.find(ident.impl());
116     if (iter != m_privateToPublicMap.end())
117         return *iter->value;
118     return m_emptyIdentifier;
119 }
120
121
122 }
123
124 #endif