| 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 # Strings are serialized as variable-length arrays. | 85 # Strings are serialized as variable-length arrays. |
| 86 if (mojom.IsStringKind(kind)): | 86 if (mojom.IsStringKind(kind)): |
| 87 expected_dimension_sizes.append(0) | 87 expected_dimension_sizes.append(0) |
| 88 return expected_dimension_sizes | 88 return expected_dimension_sizes |
| 89 | 89 |
| 90 | 90 |
| 91 def GetRelativePath(module, base_module): | 91 def GetRelativePath(module, base_module): |
| 92 return os.path.relpath(module.path, os.path.dirname(base_module.path)) | 92 return os.path.relpath(module.path, os.path.dirname(base_module.path)) |
| 93 | 93 |
| 94 | 94 |
| 95 class JavaScriptStylizer(generator.Stylizer): | |
| 96 def __init__(self, use_new_js_bindings): | |
| 97 self.use_new_js_bindings = use_new_js_bindings | |
| 98 | |
| 99 def StylizeField(self, mojom_name): | |
| 100 if not self.use_new_js_bindings: | |
| 101 return mojom_name | |
| 102 return generator.ToCamel(mojom_name, lower_initial=True) | |
| 103 | |
| 104 def StylizeParameter(self, mojom_name): | |
| 105 if not self.use_new_js_bindings: | |
| 106 return mojom_name | |
| 107 return generator.ToCamel(mojom_name, lower_initial=True) | |
| 108 | |
| 109 def StylizeMethod(self, mojom_name): | |
| 110 return generator.ToCamel(mojom_name, lower_initial=True) | |
| 111 | |
| 112 def StylizeModule(self, mojom_namespace): | |
| 113 if not self.use_new_js_bindings: | |
| 114 return mojom_namespace | |
| 115 return '.'.join(generator.ToCamel(word, lower_initial=True) | |
| 116 for word in mojom_namespace.split('.')) | |
| 117 | |
| 118 | |
| 119 class Generator(generator.Generator): | 95 class Generator(generator.Generator): |
| 120 def _GetParameters(self): | 96 def _GetParameters(self): |
| 121 return { | 97 return { |
| 122 "enums": self.module.enums, | 98 "enums": self.module.enums, |
| 123 "imports": self.module.imports, | 99 "imports": self.module.imports, |
| 124 "interfaces": self.module.interfaces, | 100 "interfaces": self.module.interfaces, |
| 125 "kinds": self.module.kinds, | 101 "kinds": self.module.kinds, |
| 126 "module": self.module, | 102 "module": self.module, |
| 127 "structs": self.module.structs + self._GetStructsFromMethods(), | 103 "structs": self.module.structs + self._GetStructsFromMethods(), |
| 128 "unions": self.module.unions, | 104 "unions": self.module.unions, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 153 "is_interface_kind": mojom.IsInterfaceKind, | 129 "is_interface_kind": mojom.IsInterfaceKind, |
| 154 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | 130 "is_interface_request_kind": mojom.IsInterfaceRequestKind, |
| 155 "is_map_kind": mojom.IsMapKind, | 131 "is_map_kind": mojom.IsMapKind, |
| 156 "is_object_kind": mojom.IsObjectKind, | 132 "is_object_kind": mojom.IsObjectKind, |
| 157 "is_string_kind": mojom.IsStringKind, | 133 "is_string_kind": mojom.IsStringKind, |
| 158 "is_struct_kind": mojom.IsStructKind, | 134 "is_struct_kind": mojom.IsStructKind, |
| 159 "is_union_kind": mojom.IsUnionKind, | 135 "is_union_kind": mojom.IsUnionKind, |
| 160 "js_type": self._JavaScriptType, | 136 "js_type": self._JavaScriptType, |
| 161 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, | 137 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, |
| 162 "payload_size": JavaScriptPayloadSize, | 138 "payload_size": JavaScriptPayloadSize, |
| 139 "stylize_method": lambda x: generator.ToCamel(x, lower_initial=True), |
| 163 "to_camel": generator.ToCamel, | 140 "to_camel": generator.ToCamel, |
| 164 "union_decode_snippet": self._JavaScriptUnionDecodeSnippet, | 141 "union_decode_snippet": self._JavaScriptUnionDecodeSnippet, |
| 165 "union_encode_snippet": self._JavaScriptUnionEncodeSnippet, | 142 "union_encode_snippet": self._JavaScriptUnionEncodeSnippet, |
| 166 "validate_array_params": self._JavaScriptValidateArrayParams, | 143 "validate_array_params": self._JavaScriptValidateArrayParams, |
| 167 "validate_enum_params": self._JavaScriptValidateEnumParams, | 144 "validate_enum_params": self._JavaScriptValidateEnumParams, |
| 168 "validate_map_params": self._JavaScriptValidateMapParams, | 145 "validate_map_params": self._JavaScriptValidateMapParams, |
| 169 "validate_nullable_params": self._JavaScriptNullableParam, | 146 "validate_nullable_params": self._JavaScriptNullableParam, |
| 170 "validate_struct_params": self._JavaScriptValidateStructParams, | 147 "validate_struct_params": self._JavaScriptValidateStructParams, |
| 171 "validate_union_params": self._JavaScriptValidateUnionParams, | 148 "validate_union_params": self._JavaScriptValidateUnionParams, |
| 172 } | 149 } |
| 173 return js_filters | 150 return js_filters |
| 174 | 151 |
| 175 @UseJinja("module.amd.tmpl") | 152 @UseJinja("module.amd.tmpl") |
| 176 def _GenerateAMDModule(self): | 153 def _GenerateAMDModule(self): |
| 177 return self._GetParameters() | 154 return self._GetParameters() |
| 178 | 155 |
| 179 def GenerateFiles(self, args): | 156 def GenerateFiles(self, args): |
| 180 if self.variant: | 157 if self.variant: |
| 181 raise Exception("Variants not supported in JavaScript bindings.") | 158 raise Exception("Variants not supported in JavaScript bindings.") |
| 182 | 159 |
| 183 self.module.Stylize(JavaScriptStylizer(self.use_new_js_bindings)) | 160 # TODO(yzshen): Add a JavaScriptStylizer. |
| 161 self.module.Stylize(generator.Stylizer()) |
| 184 | 162 |
| 185 # TODO(yzshen): Remove this method once the old JS bindings go away. | 163 # TODO(yzshen): Remove this method once the old JS bindings go away. |
| 186 self._SetUniqueNameForImports() | 164 self._SetUniqueNameForImports() |
| 187 | 165 |
| 188 self.Write(self._GenerateAMDModule(), "%s.js" % self.module.path) | 166 self.Write(self._GenerateAMDModule(), "%s.js" % self.module.path) |
| 189 | 167 |
| 190 def _SetUniqueNameForImports(self): | 168 def _SetUniqueNameForImports(self): |
| 191 used_names = set() | 169 used_names = set() |
| 192 for each_import in self.module.imports: | 170 for each_import in self.module.imports: |
| 193 simple_name = os.path.basename(each_import.path).split(".")[0] | 171 simple_name = os.path.basename(each_import.path).split(".")[0] |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 return self._TranslateConstants(value) | 376 return self._TranslateConstants(value) |
| 399 | 377 |
| 400 def _GetStructsFromMethods(self): | 378 def _GetStructsFromMethods(self): |
| 401 result = [] | 379 result = [] |
| 402 for interface in self.module.interfaces: | 380 for interface in self.module.interfaces: |
| 403 for method in interface.methods: | 381 for method in interface.methods: |
| 404 result.append(method.param_struct) | 382 result.append(method.param_struct) |
| 405 if method.response_param_struct is not None: | 383 if method.response_param_struct is not None: |
| 406 result.append(method.response_param_struct) | 384 result.append(method.response_param_struct) |
| 407 return result | 385 return result |
| OLD | NEW |