Replace WTF::move with WTFMove
[WebKit-https.git] / Source / JavaScriptCore / inspector / scripts / codegen / generate_objc_backend_dispatcher_implementation.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2014, 2015 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 string
30 import re
31 from string import Template
32
33 from cpp_generator import CppGenerator
34 from generator import Generator
35 from models import PrimitiveType, EnumType, AliasedType, Frameworks
36 from objc_generator import ObjCTypeCategory, ObjCGenerator, join_type_and_name
37 from objc_generator_templates import ObjCGeneratorTemplates as ObjCTemplates
38
39 log = logging.getLogger('global')
40
41
42 class ObjCConfigurationImplementationGenerator(Generator):
43     def __init__(self, model, input_filepath):
44         Generator.__init__(self, model, input_filepath)
45
46     def output_filename(self):
47         return '%sBackendDispatchers.mm' % ObjCGenerator.OBJC_PREFIX
48
49     def domains_to_generate(self):
50         return filter(ObjCGenerator.should_generate_domain_command_handler_filter(self.model()), Generator.domains_to_generate(self))
51
52     def generate_output(self):
53         secondary_headers = [
54             '"%sInternal.h"' % ObjCGenerator.OBJC_PREFIX,
55             '"%sEnumConversionHelpers.h"' % ObjCGenerator.OBJC_PREFIX,
56             '<JavaScriptCore/InspectorValues.h>',
57         ]
58
59         header_args = {
60             'primaryInclude': '"%sBackendDispatchers.h"' % ObjCGenerator.OBJC_PREFIX,
61             'secondaryIncludes': '\n'.join(['#include %s' % header for header in secondary_headers]),
62         }
63
64         domains = self.domains_to_generate()
65         sections = []
66         sections.append(self.generate_license())
67         sections.append(Template(ObjCTemplates.BackendDispatcherImplementationPrelude).substitute(None, **header_args))
68         sections.extend(map(self._generate_handler_implementation_for_domain, domains))
69         sections.append(Template(ObjCTemplates.BackendDispatcherImplementationPostlude).substitute(None, **header_args))
70         return '\n\n'.join(sections)
71
72     def _generate_handler_implementation_for_domain(self, domain):
73         if not domain.commands:
74             return ''
75
76         command_declarations = []
77         for command in domain.commands:
78             command_declarations.append(self._generate_handler_implementation_for_command(domain, command))
79
80         return '\n'.join(command_declarations)
81
82     def _generate_handler_implementation_for_command(self, domain, command):
83         lines = []
84         parameters = ['long requestId']
85         for parameter in command.call_parameters:
86             parameters.append('%s in_%s' % (CppGenerator.cpp_type_for_unchecked_formal_in_parameter(parameter), parameter.parameter_name))
87
88         command_args = {
89             'domainName': domain.domain_name,
90             'commandName': command.command_name,
91             'parameters': ', '.join(parameters),
92             'successCallback': self._generate_success_block_for_command(domain, command),
93             'conversions': self._generate_conversions_for_command(domain, command),
94             'invocation': self._generate_invocation_for_command(domain, command),
95         }
96
97         return self.wrap_with_guard_for_domain(domain, Template(ObjCTemplates.BackendDispatcherHeaderDomainHandlerImplementation).substitute(None, **command_args))
98
99     def _generate_success_block_for_command(self, domain, command):
100         lines = []
101
102         if command.return_parameters:
103             success_block_parameters = []
104             for parameter in command.return_parameters:
105                 objc_type = ObjCGenerator.objc_type_for_param(domain, command.command_name, parameter)
106                 var_name = ObjCGenerator.identifier_to_objc_identifier(parameter.parameter_name)
107                 success_block_parameters.append(join_type_and_name(objc_type, var_name))
108             lines.append('    id successCallback = ^(%s) {' % ', '.join(success_block_parameters))
109         else:
110             lines.append('    id successCallback = ^{')
111
112         if command.return_parameters:
113             lines.append('        Ref<InspectorObject> resultObject = InspectorObject::create();')
114
115             required_pointer_parameters = filter(lambda parameter: not parameter.is_optional and ObjCGenerator.is_type_objc_pointer_type(parameter.type), command.return_parameters)
116             for parameter in required_pointer_parameters:
117                 var_name = ObjCGenerator.identifier_to_objc_identifier(parameter.parameter_name)
118                 lines.append('        THROW_EXCEPTION_FOR_REQUIRED_PARAMETER(%s, @"%s");' % (var_name, var_name))
119                 objc_array_class = ObjCGenerator.objc_class_for_array_type(parameter.type)
120                 if objc_array_class and objc_array_class.startswith(ObjCGenerator.OBJC_PREFIX):
121                     lines.append('        THROW_EXCEPTION_FOR_BAD_TYPE_IN_ARRAY(%s, [%s class]);' % (var_name, objc_array_class))
122
123             optional_pointer_parameters = filter(lambda parameter: parameter.is_optional and ObjCGenerator.is_type_objc_pointer_type(parameter.type), command.return_parameters)
124             for parameter in optional_pointer_parameters:
125                 var_name = ObjCGenerator.identifier_to_objc_identifier(parameter.parameter_name)
126                 lines.append('        THROW_EXCEPTION_FOR_BAD_OPTIONAL_PARAMETER(%s, @"%s");' % (var_name, var_name))
127                 objc_array_class = ObjCGenerator.objc_class_for_array_type(parameter.type)
128                 if objc_array_class and objc_array_class.startswith(ObjCGenerator.OBJC_PREFIX):
129                     lines.append('        THROW_EXCEPTION_FOR_BAD_TYPE_IN_OPTIONAL_ARRAY(%s, [%s class]);' % (var_name, objc_array_class))
130
131             for parameter in command.return_parameters:
132                 keyed_set_method = CppGenerator.cpp_setter_method_for_type(parameter.type)
133                 var_name = ObjCGenerator.identifier_to_objc_identifier(parameter.parameter_name)
134                 var_expression = '*%s' % var_name if parameter.is_optional else var_name
135                 export_expression = ObjCGenerator.objc_protocol_export_expression_for_variable(parameter.type, var_expression)
136                 if not parameter.is_optional:
137                     lines.append('        resultObject->%s(ASCIILiteral("%s"), %s);' % (keyed_set_method, parameter.parameter_name, export_expression))
138                 else:
139                     lines.append('        if (%s)' % var_name)
140                     lines.append('            resultObject->%s(ASCIILiteral("%s"), %s);' % (keyed_set_method, parameter.parameter_name, export_expression))
141             lines.append('        backendDispatcher()->sendResponse(requestId, WTFMove(resultObject));')
142         else:
143             lines.append('        backendDispatcher()->sendResponse(requestId, InspectorObject::create());')
144
145         lines.append('    };')
146         return '\n'.join(lines)
147
148     def _generate_conversions_for_command(self, domain, command):
149         lines = []
150         if command.call_parameters:
151             lines.append('')
152
153         def in_param_expression(param_name, parameter):
154             _type = parameter.type
155             if isinstance(_type, AliasedType):
156                 _type = _type.aliased_type  # Fall through to enum or primitive.
157             if isinstance(_type, EnumType):
158                 _type = _type.primitive_type  # Fall through to primitive.
159             if isinstance(_type, PrimitiveType):
160                 if _type.raw_name() in ['array', 'any', 'object']:
161                     return '&%s' % param_name if not parameter.is_optional else param_name
162                 return '*%s' % param_name if parameter.is_optional else param_name
163             return '&%s' % param_name if not parameter.is_optional else param_name
164
165         for parameter in command.call_parameters:
166             in_param_name = 'in_%s' % parameter.parameter_name
167             objc_in_param_name = 'o_%s' % in_param_name
168             objc_type = ObjCGenerator.objc_type_for_param(domain, command.command_name, parameter, False)
169             param_expression = in_param_expression(in_param_name, parameter)
170             import_expression = ObjCGenerator.objc_protocol_import_expression_for_parameter(param_expression, domain, command.command_name, parameter)
171             if not parameter.is_optional:
172                 lines.append('    %s = %s;' % (join_type_and_name(objc_type, objc_in_param_name), import_expression))
173             else:
174                 lines.append('    %s;' % join_type_and_name(objc_type, objc_in_param_name))
175                 lines.append('    if (%s)' % in_param_name)
176                 lines.append('        %s = %s;' % (objc_in_param_name, import_expression))
177
178         if lines:
179             lines.append('')
180         return '\n'.join(lines)
181
182     def _generate_invocation_for_command(self, domain, command):
183         pairs = []
184         pairs.append('WithErrorCallback:errorCallback')
185         pairs.append('successCallback:successCallback')
186         for parameter in command.call_parameters:
187             in_param_name = 'in_%s' % parameter.parameter_name
188             objc_in_param_name = 'o_%s' % in_param_name
189             if not parameter.is_optional:
190                 pairs.append('%s:%s' % (parameter.parameter_name, objc_in_param_name))
191             else:
192                 optional_expression = '(%s ? &%s : nil)' % (in_param_name, objc_in_param_name)
193                 pairs.append('%s:%s' % (parameter.parameter_name, optional_expression))
194         return '    [m_delegate %s%s];' % (command.command_name, ' '.join(pairs))