OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates JavaScript source files from a mojom.Module.""" | 5 """Generates JavaScript source files from a mojom.Module.""" |
6 | 6 |
7 import mojom.generate.generator as generator | 7 import mojom.generate.generator as generator |
8 import mojom.generate.module as mojom | 8 import mojom.generate.module as mojom |
9 import mojom.generate.pack as pack | 9 import mojom.generate.pack as pack |
10 import os | 10 import os |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 return mojom.IsAnyHandleOrInterfaceKind(field.kind) | 335 return mojom.IsAnyHandleOrInterfaceKind(field.kind) |
336 | 336 |
337 def IsEnumField(field): | 337 def IsEnumField(field): |
338 return mojom.IsEnumKind(field.kind) | 338 return mojom.IsEnumKind(field.kind) |
339 | 339 |
340 def GetRelativePath(module, base_module): | 340 def GetRelativePath(module, base_module): |
341 return os.path.relpath(module.path, os.path.dirname(base_module.path)) | 341 return os.path.relpath(module.path, os.path.dirname(base_module.path)) |
342 | 342 |
343 | 343 |
344 class Generator(generator.Generator): | 344 class Generator(generator.Generator): |
345 | 345 def _GetParameters(self): |
346 js_filters = { | |
347 "decode_snippet": JavaScriptDecodeSnippet, | |
348 "default_value": JavaScriptDefaultValue, | |
349 "encode_snippet": JavaScriptEncodeSnippet, | |
350 "expression_to_text": ExpressionToText, | |
351 "field_offset": JavaScriptFieldOffset, | |
352 "has_callbacks": mojom.HasCallbacks, | |
353 "is_any_handle_or_interface_field": IsAnyHandleOrInterfaceField, | |
354 "is_array_pointer_field": IsArrayPointerField, | |
355 "is_associated_interface_field": IsAssociatedInterfaceField, | |
356 "is_associated_interface_request_field": IsAssociatedInterfaceRequestField, | |
357 "is_bool_field": IsBoolField, | |
358 "is_enum_field": IsEnumField, | |
359 "is_handle_field": IsHandleField, | |
360 "is_interface_field": IsInterfaceField, | |
361 "is_interface_request_field": IsInterfaceRequestField, | |
362 "is_map_pointer_field": IsMapPointerField, | |
363 "is_object_field": IsObjectField, | |
364 "is_string_pointer_field": IsStringPointerField, | |
365 "is_struct_pointer_field": IsStructPointerField, | |
366 "is_union_field": IsUnionField, | |
367 "js_type": JavaScriptType, | |
368 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, | |
369 "payload_size": JavaScriptPayloadSize, | |
370 "get_relative_path": GetRelativePath, | |
371 "stylize_method": generator.StudlyCapsToCamel, | |
372 "union_decode_snippet": JavaScriptUnionDecodeSnippet, | |
373 "union_encode_snippet": JavaScriptUnionEncodeSnippet, | |
374 "validate_array_params": JavaScriptValidateArrayParams, | |
375 "validate_enum_params": JavaScriptValidateEnumParams, | |
376 "validate_map_params": JavaScriptValidateMapParams, | |
377 "validate_nullable_params": JavaScriptNullableParam, | |
378 "validate_struct_params": JavaScriptValidateStructParams, | |
379 "validate_union_params": JavaScriptValidateUnionParams, | |
380 } | |
381 | |
382 def GetParameters(self): | |
383 return { | 346 return { |
384 "namespace": self.module.namespace, | 347 "namespace": self.module.namespace, |
385 "imports": self.GetImports(), | 348 "imports": self._GetImports(), |
386 "kinds": self.module.kinds, | 349 "kinds": self.module.kinds, |
387 "enums": self.module.enums, | 350 "enums": self.module.enums, |
388 "module": self.module, | 351 "module": self.module, |
389 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 352 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
390 "unions": self.GetUnions(), | 353 "unions": self.GetUnions(), |
391 "use_new_js_bindings": self.use_new_js_bindings, | 354 "use_new_js_bindings": self.use_new_js_bindings, |
392 "interfaces": self.GetInterfaces(), | 355 "interfaces": self.GetInterfaces(), |
393 "imported_interfaces": self.GetImportedInterfaces(), | 356 "imported_interfaces": self._GetImportedInterfaces(), |
394 } | 357 } |
395 | 358 |
396 @staticmethod | 359 @staticmethod |
397 def GetTemplatePrefix(): | 360 def GetTemplatePrefix(): |
398 return "js_templates" | 361 return "js_templates" |
399 | 362 |
400 @classmethod | 363 def GetFilters(self): |
401 def GetFilters(cls): | 364 js_filters = { |
402 return cls.js_filters | 365 "decode_snippet": JavaScriptDecodeSnippet, |
| 366 "default_value": JavaScriptDefaultValue, |
| 367 "encode_snippet": JavaScriptEncodeSnippet, |
| 368 "expression_to_text": ExpressionToText, |
| 369 "field_offset": JavaScriptFieldOffset, |
| 370 "has_callbacks": mojom.HasCallbacks, |
| 371 "is_any_handle_or_interface_field": IsAnyHandleOrInterfaceField, |
| 372 "is_array_pointer_field": IsArrayPointerField, |
| 373 "is_associated_interface_field": IsAssociatedInterfaceField, |
| 374 "is_associated_interface_request_field": |
| 375 IsAssociatedInterfaceRequestField, |
| 376 "is_bool_field": IsBoolField, |
| 377 "is_enum_field": IsEnumField, |
| 378 "is_handle_field": IsHandleField, |
| 379 "is_interface_field": IsInterfaceField, |
| 380 "is_interface_request_field": IsInterfaceRequestField, |
| 381 "is_map_pointer_field": IsMapPointerField, |
| 382 "is_object_field": IsObjectField, |
| 383 "is_string_pointer_field": IsStringPointerField, |
| 384 "is_struct_pointer_field": IsStructPointerField, |
| 385 "is_union_field": IsUnionField, |
| 386 "js_type": JavaScriptType, |
| 387 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, |
| 388 "payload_size": JavaScriptPayloadSize, |
| 389 "get_relative_path": GetRelativePath, |
| 390 "stylize_method": generator.StudlyCapsToCamel, |
| 391 "union_decode_snippet": JavaScriptUnionDecodeSnippet, |
| 392 "union_encode_snippet": JavaScriptUnionEncodeSnippet, |
| 393 "validate_array_params": JavaScriptValidateArrayParams, |
| 394 "validate_enum_params": JavaScriptValidateEnumParams, |
| 395 "validate_map_params": JavaScriptValidateMapParams, |
| 396 "validate_nullable_params": JavaScriptNullableParam, |
| 397 "validate_struct_params": JavaScriptValidateStructParams, |
| 398 "validate_union_params": JavaScriptValidateUnionParams, |
| 399 } |
| 400 return js_filters |
403 | 401 |
404 @UseJinja("module.amd.tmpl") | 402 @UseJinja("module.amd.tmpl") |
405 def GenerateAMDModule(self): | 403 def _GenerateAMDModule(self): |
406 return self.GetParameters() | 404 return self._GetParameters() |
407 | 405 |
408 def GenerateFiles(self, args): | 406 def GenerateFiles(self, args): |
409 if self.variant: | 407 if self.variant: |
410 raise Exception("Variants not supported in JavaScript bindings.") | 408 raise Exception("Variants not supported in JavaScript bindings.") |
411 | 409 |
412 self.Write(self.GenerateAMDModule(), | 410 self.Write(self._GenerateAMDModule(), |
413 self.MatchMojomFilePath("%s.js" % self.module.name)) | 411 self.MatchMojomFilePath("%s.js" % self.module.name)) |
414 | 412 |
415 def GetImports(self): | 413 def _GetImports(self): |
416 used_names = set() | 414 used_names = set() |
417 for each_import in self.module.imports: | 415 for each_import in self.module.imports: |
418 simple_name = each_import["module_name"].split(".")[0] | 416 simple_name = each_import["module_name"].split(".")[0] |
419 | 417 |
420 # Since each import is assigned a variable in JS, they need to have unique | 418 # Since each import is assigned a variable in JS, they need to have unique |
421 # names. | 419 # names. |
422 unique_name = simple_name | 420 unique_name = simple_name |
423 counter = 0 | 421 counter = 0 |
424 while unique_name in used_names: | 422 while unique_name in used_names: |
425 counter += 1 | 423 counter += 1 |
426 unique_name = simple_name + str(counter) | 424 unique_name = simple_name + str(counter) |
427 | 425 |
428 used_names.add(unique_name) | 426 used_names.add(unique_name) |
429 each_import["unique_name"] = unique_name + "$" | 427 each_import["unique_name"] = unique_name + "$" |
430 counter += 1 | 428 counter += 1 |
431 return self.module.imports | 429 return self.module.imports |
432 | 430 |
433 def GetImportedInterfaces(self): | 431 def _GetImportedInterfaces(self): |
434 interface_to_import = {}; | 432 interface_to_import = {}; |
435 for each_import in self.module.imports: | 433 for each_import in self.module.imports: |
436 for each_interface in each_import["module"].interfaces: | 434 for each_interface in each_import["module"].interfaces: |
437 name = each_interface.name | 435 name = each_interface.name |
438 interface_to_import[name] = each_import["unique_name"] + "." + name | 436 interface_to_import[name] = each_import["unique_name"] + "." + name |
439 return interface_to_import; | 437 return interface_to_import; |
440 | 438 |
OLD | NEW |