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