OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 3 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
4 # for details. All rights reserved. Use of this source code is governed by a | 4 # for details. All rights reserved. Use of this source code is governed by a |
5 # BSD-style license that can be found in the LICENSE file. | 5 # BSD-style license that can be found in the LICENSE file. |
6 | 6 |
7 """Generates sdk/lib/_blink/dartium/_blink_dartium.dart file.""" | 7 """Generates sdk/lib/_blink/dartium/_blink_dartium.dart file.""" |
8 | 8 |
9 import os | 9 import os |
10 | 10 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 #(argument_count, arguments, interface_name, arguments) | 142 #(argument_count, arguments, interface_name, arguments) |
143 CONSTRUCTOR_ARGS = ' constructorCallback_%s_(%s) => new js.JsObject(Blink_JsNat
ive_DomException.getProperty(js.context, "%s"), [%s]);\n\n' | 143 CONSTRUCTOR_ARGS = ' constructorCallback_%s_(%s) => new js.JsObject(Blink_JsNat
ive_DomException.getProperty(js.context, "%s"), [%s]);\n\n' |
144 | 144 |
145 #(attribute_name, attribute_name) | 145 #(attribute_name, attribute_name) |
146 ATTRIBUTE_GETTER = ' %s_Getter_(mthis) => Blink_JsNative_DomException.getProper
ty(mthis, "%s");\n\n' | 146 ATTRIBUTE_GETTER = ' %s_Getter_(mthis) => Blink_JsNative_DomException.getProper
ty(mthis, "%s");\n\n' |
147 ATTRIBUTE_SETTER = ' %s_Setter_(mthis, __arg_0) => mthis["%s"] = __arg_0;\n\n' | 147 ATTRIBUTE_SETTER = ' %s_Setter_(mthis, __arg_0) => mthis["%s"] = __arg_0;\n\n' |
148 | 148 |
149 #(operation_name, operationName) | 149 #(operation_name, operationName) |
150 OPERATION_0 = ' %s_Callback_0_(mthis) => Blink_JsNative_DomException.callMethod
(mthis, "%s", []);\n\n' | 150 OPERATION_0 = ' %s_Callback_0_(mthis) => Blink_JsNative_DomException.callMethod
(mthis, "%s", []);\n\n' |
151 | 151 |
| 152 # getter, setter, deleter and propertyQuery code |
| 153 OPERATION_1 = ' $%s_Callback_1_(mthis, __arg_0) => Blink_JsNative_DomException.
callMethod(mthis, "%s", [__arg_0]);\n\n' |
| 154 OPERATION_2 = ' $%s_Callback_2_(mthis, __arg_0, __arg_1) => Blink_JsNative_DomE
xception.callMethod(mthis, "%s", [__arg_0, __arg_1]);\n\n' |
| 155 OPERATION_PQ = ' $%s_Callback_1_(mthis, __arg_0) => mthis[__arg_0];\n\n' |
| 156 |
152 #(operation_name, argument_count, arguments, operation_name, arguments) | 157 #(operation_name, argument_count, arguments, operation_name, arguments) |
153 ARGUMENT_NUM = "__arg_%s" | 158 ARGUMENT_NUM = "__arg_%s" |
154 OPERATION_ARGS = ' %s_Callback_%s_(mthis, %s) => Blink_JsNative_DomException.ca
llMethod(mthis, "%s", [%s]);\n\n' | 159 OPERATION_ARGS = ' %s_Callback_%s_(mthis, %s) => Blink_JsNative_DomException.ca
llMethod(mthis, "%s", [%s]);\n\n' |
155 | 160 |
156 CLASS_DEFINITION_END = """} | 161 CLASS_DEFINITION_END = """} |
157 | 162 |
158 """ | 163 """ |
159 | 164 |
160 def ConstantOutputOrder(a, b): | 165 def ConstantOutputOrder(a, b): |
161 """Canonical output ordering for constants.""" | 166 """Canonical output ordering for constants.""" |
162 return cmp(a.id, b.id) | 167 return cmp(a.id, b.id) |
163 | 168 |
164 def generate_parameter_entries(param_infos): | 169 def generate_parameter_entries(param_infos): |
165 optional_default_args = 0; | 170 optional_default_args = 0; |
166 for argument in param_infos: | 171 for argument in param_infos: |
167 if argument.is_optional: | 172 if argument.is_optional: |
168 optional_default_args += 1 | 173 optional_default_args += 1 |
169 | 174 |
170 arg_count = len(param_infos) | 175 arg_count = len(param_infos) |
171 min_arg_count = arg_count - optional_default_args | 176 min_arg_count = arg_count - optional_default_args |
172 lb = min_arg_count - 2 if min_arg_count > 2 else 0 | 177 lb = min_arg_count - 2 if min_arg_count > 2 else 0 |
173 return (lb, arg_count + 1) | 178 return (lb, arg_count + 1) |
174 | 179 |
| 180 constructor_renames = { |
| 181 'RTCPeerConnection': 'webkitRTCPeerConnection', |
| 182 } |
| 183 |
| 184 def rename_constructor(name): |
| 185 return constructor_renames[name] if name in constructor_renames else name |
| 186 |
175 def Generate_Blink(output_dir, database, type_registry): | 187 def Generate_Blink(output_dir, database, type_registry): |
176 blink_filename = os.path.join(output_dir, '_blink_dartium.dart') | 188 blink_filename = os.path.join(output_dir, '_blink_dartium.dart') |
177 blink_file = open(blink_filename, 'w') | 189 blink_file = open(blink_filename, 'w') |
178 | 190 |
179 blink_file.write(HEADER); | 191 blink_file.write(HEADER); |
180 | 192 |
181 interfaces = database.GetInterfaces() | 193 interfaces = database.GetInterfaces() |
182 for interface in interfaces: | 194 for interface in interfaces: |
183 name = interface.id | 195 name = interface.id |
184 resolver_entry = ' if (s == "%s") return Blink%s.instance;\n' % (name, name
) | 196 resolver_entry = ' if (s == "%s") return Blink%s.instance;\n' % (name, name
) |
185 blink_file.write(resolver_entry) | 197 blink_file.write(resolver_entry) |
186 | 198 |
187 blink_file.write(END_RESOLVER); | 199 blink_file.write(END_RESOLVER); |
188 | 200 |
189 for interface in interfaces: | 201 for interface in interfaces: |
190 name = interface.id | 202 name = interface.id |
191 | 203 |
192 if interface.parents and len(interface.parents) > 0 and interface.parents[0]
.id: | 204 if interface.parents and len(interface.parents) > 0 and interface.parents[0]
.id: |
193 extends = interface.parents[0].id | 205 extends = interface.parents[0].id |
194 class_def = CLASS_DEFINITION_EXTENDS % (name, extends, name) | 206 class_def = CLASS_DEFINITION_EXTENDS % (name, extends, name) |
195 else: | 207 else: |
196 class_def = CLASS_DEFINITION % (name, name) | 208 class_def = CLASS_DEFINITION % (name, name) |
197 blink_file.write(class_def); | 209 blink_file.write(class_def); |
198 | 210 |
199 analyzed_constructors = AnalyzeConstructor(interface) | 211 analyzed_constructors = AnalyzeConstructor(interface) |
200 if analyzed_constructors: | 212 if analyzed_constructors: |
201 _Emit_Blink_Constructors(blink_file, analyzed_constructors) | 213 _Emit_Blink_Constructors(blink_file, analyzed_constructors) |
202 elif 'Constructor' in interface.ext_attrs: | 214 elif 'Constructor' in interface.ext_attrs: |
203 # Zero parameter constructor. | 215 # Zero parameter constructor. |
204 blink_file.write(CONSTRUCTOR_0 % name) | 216 blink_file.write(CONSTRUCTOR_0 % rename_constructor(name)) |
205 | 217 |
206 _Process_Attributes(blink_file, interface.attributes) | 218 _Process_Attributes(blink_file, interface.attributes) |
207 _Process_Operations(blink_file, interface, interface.operations) | 219 _Process_Operations(blink_file, interface, interface.operations) |
208 | 220 |
209 secondary_parents = database.TransitiveSecondaryParents(interface, False) | 221 secondary_parents = database.TransitiveSecondaryParents(interface, False) |
210 for secondary in secondary_parents: | 222 for secondary in secondary_parents: |
211 _Process_Attributes(blink_file, secondary.attributes) | 223 _Process_Attributes(blink_file, secondary.attributes) |
212 _Process_Operations(blink_file, secondary, secondary.operations) | 224 _Process_Operations(blink_file, secondary, secondary.operations) |
213 | 225 |
214 blink_file.write(CLASS_DEFINITION_END); | 226 blink_file.write(CLASS_DEFINITION_END); |
215 | 227 |
216 blink_file.write(BLINK_UTILS) | 228 blink_file.write(BLINK_UTILS) |
217 | 229 |
218 blink_file.close() | 230 blink_file.close() |
219 | 231 |
220 def _Emit_Blink_Constructors(blink_file, analyzed_constructors): | 232 def _Emit_Blink_Constructors(blink_file, analyzed_constructors): |
221 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed_construct
ors.param_infos) | 233 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed_construct
ors.param_infos) |
222 name = analyzed_constructors.js_name | 234 name = analyzed_constructors.js_name |
223 if not(name): | 235 if not(name): |
224 name = analyzed_constructors.type_name | 236 name = analyzed_constructors.type_name |
225 | 237 |
226 for callback_index in range(arg_min_count, arg_max_count): | 238 for callback_index in range(arg_min_count, arg_max_count): |
227 if callback_index == 0: | 239 if callback_index == 0: |
228 blink_file.write(CONSTRUCTOR_0 % (name)) | 240 blink_file.write(CONSTRUCTOR_0 % (rename_constructor(name))) |
229 else: | 241 else: |
230 arguments = [] | 242 arguments = [] |
231 for i in range(0, callback_index): | 243 for i in range(0, callback_index): |
232 arguments.append(ARGUMENT_NUM % i) | 244 arguments.append(ARGUMENT_NUM % i) |
233 argument_list = ', '.join(arguments) | 245 argument_list = ', '.join(arguments) |
234 blink_file.write(CONSTRUCTOR_ARGS % (callback_index, argument_list, name,
argument_list)) | 246 blink_file.write(CONSTRUCTOR_ARGS % (callback_index, argument_list, rename
_constructor(name), argument_list)) |
235 | 247 |
236 def _Process_Attributes(blink_file, attributes): | 248 def _Process_Attributes(blink_file, attributes): |
237 # Emit an interface's attributes and operations. | 249 # Emit an interface's attributes and operations. |
238 for attribute in sorted(attributes, ConstantOutputOrder): | 250 for attribute in sorted(attributes, ConstantOutputOrder): |
239 name = attribute.id | 251 name = attribute.id |
240 if attribute.is_read_only: | 252 if attribute.is_read_only: |
241 blink_file.write(ATTRIBUTE_GETTER % (name, name)) | 253 blink_file.write(ATTRIBUTE_GETTER % (name, name)) |
242 else: | 254 else: |
243 blink_file.write(ATTRIBUTE_GETTER % (name, name)) | 255 blink_file.write(ATTRIBUTE_GETTER % (name, name)) |
244 blink_file.write(ATTRIBUTE_SETTER % (name, name)) | 256 blink_file.write(ATTRIBUTE_SETTER % (name, name)) |
(...skipping 12 matching lines...) Expand all Loading... |
257 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) | 269 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) |
258 analyzeOperations = [operation] | 270 analyzeOperations = [operation] |
259 if len(analyzeOperations) > 0: | 271 if len(analyzeOperations) > 0: |
260 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) | 272 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) |
261 | 273 |
262 def _Emit_Blink_Operation(blink_file, interface, analyzeOperations): | 274 def _Emit_Blink_Operation(blink_file, interface, analyzeOperations): |
263 analyzed = AnalyzeOperation(interface, analyzeOperations) | 275 analyzed = AnalyzeOperation(interface, analyzeOperations) |
264 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed.param_inf
os) | 276 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed.param_inf
os) |
265 name = analyzed.js_name | 277 name = analyzed.js_name |
266 | 278 |
| 279 operation = analyzeOperations[0] |
| 280 if (name.startswith('__') and \ |
| 281 ('getter' in operation.specials or \ |
| 282 'setter' in operation.specials or \ |
| 283 'deleter' in operation.specials)): |
| 284 if name == '__propertyQuery__': |
| 285 blink_file.write(OPERATION_PQ % (name)) |
| 286 else: |
| 287 arg_min_count = arg_max_count |
| 288 if arg_max_count == 2: |
| 289 blink_file.write(OPERATION_1 % (name, name)) |
| 290 elif arg_max_count == 3: |
| 291 blink_file.write(OPERATION_2 % (name, name)) |
| 292 else: |
| 293 print "FATAL ERROR: _blink emitter operator %s.%s" % (interface.id, name
) |
| 294 exit |
| 295 |
| 296 return |
| 297 |
267 for callback_index in range(arg_min_count, arg_max_count): | 298 for callback_index in range(arg_min_count, arg_max_count): |
268 if callback_index == 0: | 299 if callback_index == 0: |
269 blink_file.write(OPERATION_0 % (name, name)) | 300 blink_file.write(OPERATION_0 % (name, name)) |
270 else: | 301 else: |
271 arguments = [] | 302 arguments = [] |
272 for i in range(0, callback_index): | 303 for i in range(0, callback_index): |
273 arguments.append(ARGUMENT_NUM % i) | 304 arguments.append(ARGUMENT_NUM % i) |
274 argument_list = ', '.join(arguments) | 305 argument_list = ', '.join(arguments) |
275 blink_file.write(OPERATION_ARGS % (name, callback_index, argument_list, na
me, argument_list)) | 306 blink_file.write(OPERATION_ARGS % (name, callback_index, argument_list, na
me, argument_list)) |
OLD | NEW |