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