| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Generator(generator.Generator): | 95 class Generator(generator.Generator): |
| 96 def _GetParameters(self): | 96 def _GetParameters(self): |
| 97 return { | 97 return { |
| 98 "namespace": self.module.namespace, | 98 "enums": self.module.enums, |
| 99 "imports": self._GetImports(), | 99 "imports": self.module.imports, |
| 100 "interfaces": self.module.interfaces, |
| 100 "kinds": self.module.kinds, | 101 "kinds": self.module.kinds, |
| 101 "enums": self.module.enums, | |
| 102 "module": self.module, | 102 "module": self.module, |
| 103 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 103 "structs": self.module.structs + self._GetStructsFromMethods(), |
| 104 "unions": self.GetUnions(), | 104 "unions": self.module.unions, |
| 105 "use_new_js_bindings": self.use_new_js_bindings, | 105 "use_new_js_bindings": self.use_new_js_bindings, |
| 106 "interfaces": self.GetInterfaces(), | |
| 107 "imported_interfaces": self._GetImportedInterfaces(), | |
| 108 } | 106 } |
| 109 | 107 |
| 110 @staticmethod | 108 @staticmethod |
| 111 def GetTemplatePrefix(): | 109 def GetTemplatePrefix(): |
| 112 return "js_templates" | 110 return "js_templates" |
| 113 | 111 |
| 114 def GetFilters(self): | 112 def GetFilters(self): |
| 115 js_filters = { | 113 js_filters = { |
| 116 "decode_snippet": self._JavaScriptDecodeSnippet, | 114 "decode_snippet": self._JavaScriptDecodeSnippet, |
| 117 "default_value": self._JavaScriptDefaultValue, | 115 "default_value": self._JavaScriptDefaultValue, |
| 118 "encode_snippet": self._JavaScriptEncodeSnippet, | 116 "encode_snippet": self._JavaScriptEncodeSnippet, |
| 119 "expression_to_text": self._ExpressionToText, | 117 "expression_to_text": self._ExpressionToText, |
| 120 "field_offset": JavaScriptFieldOffset, | 118 "field_offset": JavaScriptFieldOffset, |
| 119 "get_relative_path": GetRelativePath, |
| 121 "has_callbacks": mojom.HasCallbacks, | 120 "has_callbacks": mojom.HasCallbacks, |
| 122 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 121 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
| 123 "is_array_kind": mojom.IsArrayKind, | 122 "is_array_kind": mojom.IsArrayKind, |
| 124 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind, | 123 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind, |
| 125 "is_associated_interface_request_kind": | 124 "is_associated_interface_request_kind": |
| 126 mojom.IsAssociatedInterfaceRequestKind, | 125 mojom.IsAssociatedInterfaceRequestKind, |
| 127 "is_bool_kind": mojom.IsBoolKind, | 126 "is_bool_kind": mojom.IsBoolKind, |
| 128 "is_enum_kind": mojom.IsEnumKind, | 127 "is_enum_kind": mojom.IsEnumKind, |
| 129 "is_any_handle_kind": mojom.IsAnyHandleKind, | 128 "is_any_handle_kind": mojom.IsAnyHandleKind, |
| 130 "is_interface_kind": mojom.IsInterfaceKind, | 129 "is_interface_kind": mojom.IsInterfaceKind, |
| 131 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | 130 "is_interface_request_kind": mojom.IsInterfaceRequestKind, |
| 132 "is_map_kind": mojom.IsMapKind, | 131 "is_map_kind": mojom.IsMapKind, |
| 133 "is_object_kind": mojom.IsObjectKind, | 132 "is_object_kind": mojom.IsObjectKind, |
| 134 "is_string_kind": mojom.IsStringKind, | 133 "is_string_kind": mojom.IsStringKind, |
| 135 "is_struct_kind": mojom.IsStructKind, | 134 "is_struct_kind": mojom.IsStructKind, |
| 136 "is_union_kind": mojom.IsUnionKind, | 135 "is_union_kind": mojom.IsUnionKind, |
| 137 "js_type": self._JavaScriptType, | 136 "js_type": self._JavaScriptType, |
| 138 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, | 137 "method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds, |
| 139 "payload_size": JavaScriptPayloadSize, | 138 "payload_size": JavaScriptPayloadSize, |
| 140 "get_relative_path": GetRelativePath, | 139 "stylize_method": lambda x: generator.ToCamel(x, lower_initial=True), |
| 141 "stylize_method": generator.StudlyCapsToCamel, | 140 "to_camel": generator.ToCamel, |
| 142 "union_decode_snippet": self._JavaScriptUnionDecodeSnippet, | 141 "union_decode_snippet": self._JavaScriptUnionDecodeSnippet, |
| 143 "union_encode_snippet": self._JavaScriptUnionEncodeSnippet, | 142 "union_encode_snippet": self._JavaScriptUnionEncodeSnippet, |
| 144 "validate_array_params": self._JavaScriptValidateArrayParams, | 143 "validate_array_params": self._JavaScriptValidateArrayParams, |
| 145 "validate_enum_params": self._JavaScriptValidateEnumParams, | 144 "validate_enum_params": self._JavaScriptValidateEnumParams, |
| 146 "validate_map_params": self._JavaScriptValidateMapParams, | 145 "validate_map_params": self._JavaScriptValidateMapParams, |
| 147 "validate_nullable_params": self._JavaScriptNullableParam, | 146 "validate_nullable_params": self._JavaScriptNullableParam, |
| 148 "validate_struct_params": self._JavaScriptValidateStructParams, | 147 "validate_struct_params": self._JavaScriptValidateStructParams, |
| 149 "validate_union_params": self._JavaScriptValidateUnionParams, | 148 "validate_union_params": self._JavaScriptValidateUnionParams, |
| 150 } | 149 } |
| 151 return js_filters | 150 return js_filters |
| 152 | 151 |
| 153 @UseJinja("module.amd.tmpl") | 152 @UseJinja("module.amd.tmpl") |
| 154 def _GenerateAMDModule(self): | 153 def _GenerateAMDModule(self): |
| 155 return self._GetParameters() | 154 return self._GetParameters() |
| 156 | 155 |
| 157 def GenerateFiles(self, args): | 156 def GenerateFiles(self, args): |
| 158 if self.variant: | 157 if self.variant: |
| 159 raise Exception("Variants not supported in JavaScript bindings.") | 158 raise Exception("Variants not supported in JavaScript bindings.") |
| 160 | 159 |
| 160 # TODO(yzshen): Remove this method once the old JS bindings go away. |
| 161 self._SetUniqueNameForImports() |
| 162 |
| 161 self.Write(self._GenerateAMDModule(), | 163 self.Write(self._GenerateAMDModule(), |
| 162 self.MatchMojomFilePath("%s.js" % self.module.name)) | 164 self.MatchMojomFilePath("%s.js" % self.module.name)) |
| 163 | 165 |
| 164 def _GetImports(self): | 166 def _SetUniqueNameForImports(self): |
| 165 # TODO(yzshen): Remove this method once the old JS bindings go away. | |
| 166 used_names = set() | 167 used_names = set() |
| 167 for each_import in self.module.imports: | 168 for each_import in self.module.imports: |
| 168 simple_name = each_import.name.split(".")[0] | 169 simple_name = each_import.name.split(".")[0] |
| 169 | 170 |
| 170 # Since each import is assigned a variable in JS, they need to have unique | 171 # Since each import is assigned a variable in JS, they need to have unique |
| 171 # names. | 172 # names. |
| 172 unique_name = simple_name | 173 unique_name = simple_name |
| 173 counter = 0 | 174 counter = 0 |
| 174 while unique_name in used_names: | 175 while unique_name in used_names: |
| 175 counter += 1 | 176 counter += 1 |
| 176 unique_name = simple_name + str(counter) | 177 unique_name = simple_name + str(counter) |
| 177 | 178 |
| 178 used_names.add(unique_name) | 179 used_names.add(unique_name) |
| 179 each_import.unique_name = unique_name + "$" | 180 each_import.unique_name = unique_name + "$" |
| 180 counter += 1 | 181 counter += 1 |
| 181 return self.module.imports | |
| 182 | |
| 183 def _GetImportedInterfaces(self): | |
| 184 interface_to_import = {}; | |
| 185 for each_import in self.module.imports: | |
| 186 for each_interface in each_import.interfaces: | |
| 187 name = each_interface.name | |
| 188 interface_to_import[name] = each_import.unique_name + "." + name | |
| 189 return interface_to_import; | |
| 190 | 182 |
| 191 def _JavaScriptType(self, kind): | 183 def _JavaScriptType(self, kind): |
| 192 name = [] | 184 name = [] |
| 193 if kind.module and kind.module.path != self.module.path: | 185 if kind.module and kind.module.path != self.module.path: |
| 194 name.append(kind.module.unique_name) | 186 name.append(kind.module.unique_name) |
| 195 if kind.parent_kind: | 187 if kind.parent_kind: |
| 196 name.append(kind.parent_kind.name) | 188 name.append(kind.parent_kind.name) |
| 197 name.append(kind.name) | 189 name.append(kind.name) |
| 198 return ".".join(name) | 190 return ".".join(name) |
| 199 | 191 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 token.value == "float.NEGATIVE_INFINITY": | 366 token.value == "float.NEGATIVE_INFINITY": |
| 375 return "-Infinity"; | 367 return "-Infinity"; |
| 376 if token.value == "double.NAN" or token.value == "float.NAN": | 368 if token.value == "double.NAN" or token.value == "float.NAN": |
| 377 return "NaN"; | 369 return "NaN"; |
| 378 | 370 |
| 379 return token | 371 return token |
| 380 | 372 |
| 381 def _ExpressionToText(self, value): | 373 def _ExpressionToText(self, value): |
| 382 return self._TranslateConstants(value) | 374 return self._TranslateConstants(value) |
| 383 | 375 |
| 376 def _GetStructsFromMethods(self): |
| 377 result = [] |
| 378 for interface in self.module.interfaces: |
| 379 for method in interface.methods: |
| 380 result.append(method.param_struct) |
| 381 if method.response_param_struct is not None: |
| 382 result.append(method.response_param_struct) |
| 383 return result |
| OLD | NEW |