A stack overflow in the parsing of a builtin (called by createExecutable) cause a...
[WebKit-https.git] / Source / JavaScriptCore / Scripts / builtins / builtins_generate_combined_header.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2014-2016 Apple Inc. All rights reserved.
4 # Copyright (c) 2014 University of Washington. 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
28 import logging
29 import re
30 import string
31 from string import Template
32
33 from builtins_generator import BuiltinsGenerator
34 from builtins_templates import BuiltinsGeneratorTemplates as Templates
35
36 log = logging.getLogger('global')
37
38
39 class BuiltinsCombinedHeaderGenerator(BuiltinsGenerator):
40     def __init__(self, model):
41         BuiltinsGenerator.__init__(self, model)
42
43     def output_filename(self):
44         return "%sBuiltins.h" % self.model().framework.setting('namespace')
45
46     def generate_output(self):
47         args = {
48             'namespace': self.model().framework.setting('namespace'),
49             'macroPrefix': self.model().framework.setting('macro_prefix'),
50         }
51
52         sections = []
53         sections.append(self.generate_license())
54         sections.append(Template(Templates.DoNotEditWarning).substitute(args))
55         sections.append(Template(Templates.HeaderIncludeGuard).substitute(args))
56         sections.append(self.generate_forward_declarations())
57         sections.append(Template(Templates.NamespaceTop).substitute(args))
58         for object in self.model().objects:
59             sections.append(self.generate_section_for_object(object))
60         sections.append(self.generate_section_for_code_table_macro())
61         sections.append(self.generate_section_for_code_name_macro())
62         sections.append(self.generate_section_for_global_private_code_name_macro())
63         sections.append(Template(Templates.CombinedHeaderStaticMacros).substitute(args))
64         sections.append(Template(Templates.NamespaceBottom).substitute(args))
65
66         return "\n\n".join(sections)
67
68     def generate_forward_declarations(self):
69         return """
70 #include <wtf/Expected.h>
71
72 namespace JSC {
73 class FunctionExecutable;
74 class VM;
75 class ParserError;
76
77 enum class ConstructAbility : unsigned;
78 }
79 """
80
81     def generate_section_for_object(self, object):
82         lines = []
83         lines.append('/* %s */' % object.object_name)
84         lines.extend(self.generate_externs_for_object(object))
85         lines.append("")
86         lines.extend(self.generate_macros_for_object(object))
87         return '\n'.join(lines)
88
89     def generate_externs_for_object(self, object):
90         lines = []
91
92         for function in object.functions:
93             function_args = {
94                 'codeName': BuiltinsGenerator.mangledNameForFunction(function) + 'Code',
95             }
96
97             lines.append("""extern const char* s_%(codeName)s;
98 extern const int s_%(codeName)sLength;
99 extern const JSC::ConstructAbility s_%(codeName)sConstructAbility;""" % function_args)
100
101         return lines
102
103     def generate_macros_for_object(self, object):
104         args = {
105             'macroPrefix': self.model().framework.setting('macro_prefix'),
106             'objectMacro': object.object_name.replace('.', '').upper(),
107         }
108
109         lines = []
110         lines.append("#define %(macroPrefix)s_FOREACH_%(objectMacro)s_BUILTIN_DATA(macro) \\" % args)
111         for function in object.functions:
112             function_args = {
113                 'funcName': function.function_name,
114                 'mangledName': BuiltinsGenerator.mangledNameForFunction(function),
115                 'paramCount': len(function.parameters),
116             }
117
118             lines.append("    macro(%(funcName)s, %(mangledName)s, %(paramCount)d) \\" % function_args)
119         return lines
120
121     def generate_section_for_code_table_macro(self):
122         args = {
123             'macroPrefix': self.model().framework.setting('macro_prefix'),
124         }
125
126         lines = []
127         lines.append("#define %(macroPrefix)s_FOREACH_BUILTIN_CODE(macro) \\" % args)
128         for function in self.model().all_functions():
129             function_args = {
130                 'funcName': function.function_name,
131                 'overriddenName': function.overridden_name,
132                 'codeName': BuiltinsGenerator.mangledNameForFunction(function) + 'Code',
133             }
134
135             lines.append("    macro(%(codeName)s, %(funcName)s, %(overriddenName)s, s_%(codeName)sLength) \\" % function_args)
136         return '\n'.join(lines)
137
138     def generate_section_for_code_name_macro(self):
139         args = {
140             'macroPrefix': self.model().framework.setting('macro_prefix'),
141         }
142
143         internal_function_names = [function.function_name for function in self.model().all_internal_functions()]
144         if len(internal_function_names) != len(set(internal_function_names)):
145             log.error("There are several internal functions with the same name. Private identifiers may clash.")
146
147         lines = []
148         lines.append("#define %(macroPrefix)s_FOREACH_BUILTIN_FUNCTION_NAME(macro) \\" % args)
149         unique_names = list(set([function.function_name for function in self.model().all_functions()]))
150         unique_names.sort()
151         for function_name in unique_names:
152             function_args = {
153                 'funcName': function_name,
154             }
155
156             lines.append("    macro(%(funcName)s) \\" % function_args)
157         return '\n'.join(lines)
158
159     def generate_section_for_global_private_code_name_macro(self):
160         args = {
161             'macroPrefix': self.model().framework.setting('macro_prefix'),
162         }
163
164         lines = []
165         lines.append("#define %(macroPrefix)s_FOREACH_BUILTIN_FUNCTION_PRIVATE_GLOBAL_NAME(macro) \\" % args)
166         functions = filter(lambda function: function.is_global_private, self.model().all_functions())
167         functions.sort(key=lambda x: x.function_name)
168         for function in functions:
169             function_args = {
170                 'funcName': function.function_name,
171                 'codeName': BuiltinsGenerator.mangledNameForFunction(function),
172             }
173
174             lines.append("    macro(%(funcName)s, %(codeName)s) \\" % function_args)
175
176         return '\n'.join(lines)