5c340b2d91afe374ef134bd5b0ad8bac25b2e3aa
[WebKit-https.git] / Source / JavaScriptCore / Scripts / builtins / builtins_templates.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2014, 2015 Apple Inc. All rights reserved.
4 # Copyright (C) 2015 Canon Inc. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 # THE POSSIBILITY OF SUCH DAMAGE.
26
27 # Builtins generator templates, which can be filled with string.Template.
28
29
30 class BuiltinsGeneratorTemplates:
31
32     LicenseText = (
33     """Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions
35 are met:
36 1. Redistributions of source code must retain the above copyright
37    notice, this list of conditions and the following disclaimer.
38 2. Redistributions in binary form must reproduce the above copyright
39    notice, this list of conditions and the following disclaimer in the
40    documentation and/or other materials provided with the distribution.
41
42 THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
43 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
44 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
46 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
52 THE POSSIBILITY OF SUCH DAMAGE.
53 """)
54
55     DoNotEditWarning = (
56     """// DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for
57 // builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py""")
58
59     HeaderIncludeGuardTop = (
60     """#ifndef ${headerGuard}
61 #define ${headerGuard}""")
62
63     HeaderIncludeGuardBottom = (
64     """#endif // ${headerGuard}
65 """)
66
67     NamespaceTop = (
68     """namespace ${namespace} {""")
69
70     NamespaceBottom = (
71     """} // namespace ${namespace}""")
72
73     CombinedHeaderStaticMacros = (
74     """#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
75     JSC::FunctionExecutable* codeName##Generator(JSC::VM&);
76
77 ${macroPrefix}_FOREACH_BUILTIN_CODE(DECLARE_BUILTIN_GENERATOR)
78 #undef DECLARE_BUILTIN_GENERATOR
79
80 #define ${macroPrefix}_BUILTIN_EXISTS(object, func) defined ${macroPrefix}_BUILTIN_ ## object ## _ ## func""")
81
82     SeparateHeaderStaticMacros = (
83     """#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
84     JSC::FunctionExecutable* codeName##Generator(JSC::VM&);
85
86 ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(DECLARE_BUILTIN_GENERATOR)
87 #undef DECLARE_BUILTIN_GENERATOR
88
89 #define ${macroPrefix}_BUILTIN_${objectMacro}_EXISTS(object, func) defined ${macroPrefix}_BUILTIN_ ## object ## _ ## func""")
90
91     CombinedJSCImplementationStaticMacros = (
92     """
93 #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
94 JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \\
95 {\\
96     return vm.builtinExecutables()->codeName##Executable()->link(vm, vm.builtinExecutables()->codeName##Source()); \
97 }
98 ${macroPrefix}_FOREACH_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
99 #undef DEFINE_BUILTIN_GENERATOR
100 """)
101
102     SeparateJSCImplementationStaticMacros = (
103     """
104 #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
105 JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \\
106 {\\
107     return vm.builtinExecutables()->codeName##Executable()->link(vm, vm.builtinExecutables()->codeName##Source()); \
108 }
109 ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
110 #undef DEFINE_BUILTIN_GENERATOR
111 """)
112
113     CombinedWebCoreImplementationStaticMacros = (
114         """
115 #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
116 JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \\
117 {\\
118     JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \\
119     return clientData->builtinFunctions().${objectNameLC}Builtins().codeName##Executable()->link(vm, clientData->builtinFunctions().${objectNameLC}Builtins().codeName##Source()); \\
120 }
121 ${macroPrefix}_FOREACH_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
122 #undef DEFINE_BUILTIN_GENERATOR
123 """)
124
125     SeparateWebCoreImplementationStaticMacros = (
126         """
127 #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, argumentCount) \\
128 JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \\
129 {\\
130     JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \\
131     return clientData->builtinFunctions().${objectNameLC}Builtins().codeName##Executable()->link(vm, clientData->builtinFunctions().${objectNameLC}Builtins().codeName##Source()); \\
132 }
133 ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
134 #undef DEFINE_BUILTIN_GENERATOR
135 """)
136
137     SeparateHeaderWrapperBoilerplate = (
138     """class ${objectName}BuiltinsWrapper : private JSC::WeakHandleOwner {
139 public:
140     explicit ${objectName}BuiltinsWrapper(JSC::VM* vm)
141         : m_vm(*vm)
142         ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(INITIALIZE_BUILTIN_NAMES)
143 #define INITIALIZE_BUILTIN_SOURCE_MEMBERS(name, functionName, length) , m_##name##Source(JSC::makeSource(StringImpl::createFromLiteral(s_##name, length)))
144         ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(INITIALIZE_BUILTIN_SOURCE_MEMBERS)
145 #undef INITIALIZE_BUILTIN_SOURCE_MEMBERS
146     {
147     }
148
149 #define EXPOSE_BUILTIN_EXECUTABLES(name, functionName, length) \\
150     JSC::UnlinkedFunctionExecutable* name##Executable(); \\
151     const JSC::SourceCode& name##Source() const { return m_##name##Source; }
152     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(EXPOSE_BUILTIN_EXECUTABLES)
153 #undef EXPOSE_BUILTIN_EXECUTABLES
154
155     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
156
157     void exportNames();
158
159 private:
160     JSC::VM& m_vm;
161
162     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
163
164 #define DECLARE_BUILTIN_SOURCE_MEMBERS(name, functionName, length) \\
165     JSC::SourceCode m_##name##Source;\\
166     JSC::Weak<JSC::UnlinkedFunctionExecutable> m_##name##Executable;
167     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(DECLARE_BUILTIN_SOURCE_MEMBERS)
168 #undef DECLARE_BUILTIN_SOURCE_MEMBERS
169
170 };
171
172 #define DEFINE_BUILTIN_EXECUTABLES(name, functionName, length) \\
173 inline JSC::UnlinkedFunctionExecutable* ${objectName}BuiltinsWrapper::name##Executable() \\
174 {\\
175     if (!m_##name##Executable)\\
176         m_##name##Executable = JSC::Weak<JSC::UnlinkedFunctionExecutable>(JSC::createBuiltinExecutable(m_vm, m_##name##Source, functionName##PublicName(), s_##name##ConstructAbility), this, &m_##name##Executable);\\
177     return m_##name##Executable.get();\\
178 }
179 ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(DEFINE_BUILTIN_EXECUTABLES)
180 #undef DEFINE_BUILTIN_EXECUTABLES
181
182 inline void ${objectName}BuiltinsWrapper::exportNames()
183 {
184 #define EXPORT_FUNCTION_NAME(name) m_vm.propertyNames->appendExternalName(name##PublicName(), name##PrivateName());
185     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(EXPORT_FUNCTION_NAME)
186 #undef EXPORT_FUNCTION_NAME
187 }
188
189 class ${objectName}BuiltinFunctions {
190 public:
191     explicit ${objectName}BuiltinFunctions(JSC::VM& vm) : m_vm(vm) { }
192
193     void init(JSC::JSGlobalObject&);
194     void visit(JSC::SlotVisitor&);
195
196 public:
197     JSC::VM& m_vm;
198
199 #define DECLARE_BUILTIN_SOURCE_MEMBERS(functionName) \\
200     JSC::WriteBarrier<JSC::JSFunction> m_##functionName##Function;
201     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_SOURCE_MEMBERS)
202 #undef DECLARE_BUILTIN_SOURCE_MEMBERS
203 };
204
205 inline void ${objectName}BuiltinFunctions::init(JSC::JSGlobalObject& globalObject)
206 {
207 #define EXPORT_FUNCTION(codeName, functionName, length)\\
208     m_##functionName##Function.set(m_vm, &globalObject, JSC::JSFunction::createBuiltinFunction(m_vm, codeName##Generator(m_vm), &globalObject));
209     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_CODE(EXPORT_FUNCTION)
210 #undef EXPORT_FUNCTION
211 }
212
213 inline void ${objectName}BuiltinFunctions::visit(JSC::SlotVisitor& visitor)
214 {
215 #define VISIT_FUNCTION(name) visitor.append(&m_##name##Function);
216     ${macroPrefix}_FOREACH_${objectMacro}_BUILTIN_FUNCTION_NAME(VISIT_FUNCTION)
217 #undef VISIT_FUNCTION
218 }
219 """)