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