[GTK] Make script dialogs modal to the current web view only
[WebKit-https.git] / Source / JavaScriptCore / Scripts / wkbuiltins / builtins_generate_internals_wrapper_implementation.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2016 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
27 import logging
28 from string import Template
29
30 from builtins_generator import BuiltinsGenerator, WK_lcfirst, WK_ucfirst
31 from builtins_templates import BuiltinsGeneratorTemplates as Templates
32
33 log = logging.getLogger('global')
34
35
36 class BuiltinsInternalsWrapperImplementationGenerator(BuiltinsGenerator):
37     def __init__(self, model):
38         BuiltinsGenerator.__init__(self, model)
39         self.internals = filter(lambda object: 'internal' in object.annotations, model.objects)
40
41     def output_filename(self):
42         return "%sJSBuiltinInternals.cpp" % self.model().framework.setting('namespace')
43
44     def generate_output(self):
45         args = {
46             'namespace': self.model().framework.setting('namespace'),
47         }
48
49         sections = []
50         sections.append(self.generate_license())
51         sections.append(Template(Templates.DoNotEditWarning).substitute(args))
52         sections.append(self.generate_primary_header_includes())
53         sections.append(self.generate_secondary_header_includes())
54
55         sections.append(Template(Templates.NamespaceTop).substitute(args))
56         sections.append(self.generate_section_for_object())
57         sections.append(Template(Templates.NamespaceBottom).substitute(args))
58
59         return "\n\n".join(sections)
60
61     def generate_secondary_header_includes(self):
62         header_includes = [
63             (["WebCore"],
64                 ("WebCore", "JSDOMGlobalObject.h"),
65             ),
66             (["WebCore"],
67                 ("WebCore", "WebCoreJSClientData.h"),
68             ),
69             (["WebCore"],
70                 ("JavaScriptCore", "heap/HeapInlines.h"),
71             ),
72             (["WebCore"],
73                 ("JavaScriptCore", "heap/SlotVisitorInlines.h"),
74             ),
75             (["WebCore"],
76                 ("JavaScriptCore", "runtime/JSCJSValueInlines.h"),
77             ),
78             (["WebCore"],
79                 ("JavaScriptCore", "runtime/StructureInlines.h"),
80             ),
81         ]
82         return '\n'.join(self.generate_includes_from_entries(header_includes))
83
84     def generate_section_for_object(self):
85         lines = []
86
87         lines.append(self.generate_constructor())
88         lines.append(self.generate_visit_method())
89         lines.append(self.generate_initialize_method())
90         return '\n'.join(lines)
91
92     def accessor_name(self, object):
93         return WK_lcfirst(object.object_name)
94
95     def member_name(self, object):
96         return "m_" + self.accessor_name(object)
97
98     def member_type(self, object):
99         return WK_ucfirst(object.object_name) + "BuiltinFunctions"
100
101     def generate_constructor(self):
102         guards = set([object.annotations.get('conditional') for object in self.internals if 'conditional' in object.annotations])
103         lines = ["JSBuiltinInternalFunctions::JSBuiltinInternalFunctions(JSC::VM& vm)",
104                  "    : m_vm(vm)"]
105         for object in self.internals:
106             initializer = "    , %s(m_vm)" % self.member_name(object)
107             lines.append(BuiltinsGenerator.wrap_with_guard(object.annotations.get('conditional'), initializer))
108         lines.append("{")
109         lines.append("    UNUSED_PARAM(vm);")
110         lines.append("}\n")
111         return '\n'.join(lines)
112
113     def property_macro(self, object):
114         lines = []
115         lines.append("#define DECLARE_GLOBAL_STATIC(name) \\")
116         lines.append("    JSDOMGlobalObject::GlobalPropertyInfo( \\")
117         lines.append("        clientData.builtinFunctions().%sBuiltins().name##PrivateName(), %s().m_##name##Function.get() , JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)," % (self.accessor_name(object), self.accessor_name(object)))
118         lines.append("    WEBCORE_FOREACH_%s_BUILTIN_FUNCTION_NAME(DECLARE_GLOBAL_STATIC)" % object.object_name.upper())
119         lines.append("#undef DECLARE_GLOBAL_STATIC")
120         return '\n'.join(lines)
121
122     def generate_visit_method(self):
123         lines = ["void JSBuiltinInternalFunctions::visit(JSC::SlotVisitor& visitor)",
124                  "{"]
125         for object in self.internals:
126             visit = "    %s.visit(visitor);" % self.member_name(object)
127             lines.append(BuiltinsGenerator.wrap_with_guard(object.annotations.get('conditional'), visit))
128         lines.append("    UNUSED_PARAM(visitor);")
129         lines.append("}\n")
130         return '\n'.join(lines)
131
132     def _generate_initialize_static_globals(self):
133         lines = ["    JSVMClientData& clientData = *static_cast<JSVMClientData*>(m_vm.clientData);",
134                  "    JSDOMGlobalObject::GlobalPropertyInfo staticGlobals[] = {"]
135         for object in self.internals:
136             lines.append(BuiltinsGenerator.wrap_with_guard(object.annotations.get('conditional'), self.property_macro(object)))
137         lines.append("    };")
138         lines.append("    globalObject.addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));")
139         lines.append("    UNUSED_PARAM(clientData);")
140         return '\n'.join(lines)
141
142     def generate_initialize_method(self):
143         lines = ["void JSBuiltinInternalFunctions::initialize(JSDOMGlobalObject& globalObject)",
144                 "{",
145                 "    UNUSED_PARAM(globalObject);"]
146
147         for object in self.internals:
148             init = "    %s.init(globalObject);" % self.member_name(object)
149             lines.append(BuiltinsGenerator.wrap_with_guard(object.annotations.get('conditional'), init))
150         lines.append("")
151
152         guards = set([object.annotations.get('conditional') for object in self.internals if 'conditional' in object.annotations])
153         lines.append(BuiltinsGenerator.wrap_with_guard(" || ".join(guards), self._generate_initialize_static_globals()))
154
155         lines.append("}")
156         return '\n'.join(lines)