| 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 |
| 11 import urllib |
| 11 from mojom.generate.template_expander import UseJinja | 12 from mojom.generate.template_expander import UseJinja |
| 12 | 13 |
| 13 _kind_to_javascript_default_value = { | 14 _kind_to_javascript_default_value = { |
| 14 mojom.BOOL: "false", | 15 mojom.BOOL: "false", |
| 15 mojom.INT8: "0", | 16 mojom.INT8: "0", |
| 16 mojom.UINT8: "0", | 17 mojom.UINT8: "0", |
| 17 mojom.INT16: "0", | 18 mojom.INT16: "0", |
| 18 mojom.UINT16: "0", | 19 mojom.UINT16: "0", |
| 19 mojom.INT32: "0", | 20 mojom.INT32: "0", |
| 20 mojom.UINT32: "0", | 21 mojom.UINT32: "0", |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 expected_dimension_sizes = [] | 82 expected_dimension_sizes = [] |
| 82 while mojom.IsArrayKind(kind): | 83 while mojom.IsArrayKind(kind): |
| 83 expected_dimension_sizes.append(generator.ExpectedArraySize(kind) or 0) | 84 expected_dimension_sizes.append(generator.ExpectedArraySize(kind) or 0) |
| 84 kind = kind.kind | 85 kind = kind.kind |
| 85 # Strings are serialized as variable-length arrays. | 86 # Strings are serialized as variable-length arrays. |
| 86 if (mojom.IsStringKind(kind)): | 87 if (mojom.IsStringKind(kind)): |
| 87 expected_dimension_sizes.append(0) | 88 expected_dimension_sizes.append(0) |
| 88 return expected_dimension_sizes | 89 return expected_dimension_sizes |
| 89 | 90 |
| 90 | 91 |
| 91 def GetRelativePath(module, base_module): | 92 def GetRelativeUrl(module, base_module): |
| 92 return os.path.relpath(module.path, os.path.dirname(base_module.path)) | 93 return urllib.pathname2url( |
| 94 os.path.relpath(module.path, os.path.dirname(base_module.path))) |
| 93 | 95 |
| 94 | 96 |
| 95 class JavaScriptStylizer(generator.Stylizer): | 97 class JavaScriptStylizer(generator.Stylizer): |
| 96 def __init__(self, use_new_js_bindings): | 98 def __init__(self, use_new_js_bindings): |
| 97 self.use_new_js_bindings = use_new_js_bindings | 99 self.use_new_js_bindings = use_new_js_bindings |
| 98 | 100 |
| 99 def StylizeField(self, mojom_name): | 101 def StylizeField(self, mojom_name): |
| 100 if not self.use_new_js_bindings: | 102 if not self.use_new_js_bindings: |
| 101 return mojom_name | 103 return mojom_name |
| 102 return generator.ToCamel(mojom_name, lower_initial=True) | 104 return generator.ToCamel(mojom_name, lower_initial=True) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 133 def GetTemplatePrefix(): | 135 def GetTemplatePrefix(): |
| 134 return "js_templates" | 136 return "js_templates" |
| 135 | 137 |
| 136 def GetFilters(self): | 138 def GetFilters(self): |
| 137 js_filters = { | 139 js_filters = { |
| 138 "decode_snippet": self._JavaScriptDecodeSnippet, | 140 "decode_snippet": self._JavaScriptDecodeSnippet, |
| 139 "default_value": self._JavaScriptDefaultValue, | 141 "default_value": self._JavaScriptDefaultValue, |
| 140 "encode_snippet": self._JavaScriptEncodeSnippet, | 142 "encode_snippet": self._JavaScriptEncodeSnippet, |
| 141 "expression_to_text": self._ExpressionToText, | 143 "expression_to_text": self._ExpressionToText, |
| 142 "field_offset": JavaScriptFieldOffset, | 144 "field_offset": JavaScriptFieldOffset, |
| 143 "get_relative_path": GetRelativePath, | 145 "get_relative_url": GetRelativeUrl, |
| 144 "has_callbacks": mojom.HasCallbacks, | 146 "has_callbacks": mojom.HasCallbacks, |
| 145 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 147 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
| 146 "is_array_kind": mojom.IsArrayKind, | 148 "is_array_kind": mojom.IsArrayKind, |
| 147 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind, | 149 "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind, |
| 148 "is_associated_interface_request_kind": | 150 "is_associated_interface_request_kind": |
| 149 mojom.IsAssociatedInterfaceRequestKind, | 151 mojom.IsAssociatedInterfaceRequestKind, |
| 150 "is_bool_kind": mojom.IsBoolKind, | 152 "is_bool_kind": mojom.IsBoolKind, |
| 151 "is_enum_kind": mojom.IsEnumKind, | 153 "is_enum_kind": mojom.IsEnumKind, |
| 152 "is_any_handle_kind": mojom.IsAnyHandleKind, | 154 "is_any_handle_kind": mojom.IsAnyHandleKind, |
| 153 "is_interface_kind": mojom.IsInterfaceKind, | 155 "is_interface_kind": mojom.IsInterfaceKind, |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 return self._TranslateConstants(value) | 400 return self._TranslateConstants(value) |
| 399 | 401 |
| 400 def _GetStructsFromMethods(self): | 402 def _GetStructsFromMethods(self): |
| 401 result = [] | 403 result = [] |
| 402 for interface in self.module.interfaces: | 404 for interface in self.module.interfaces: |
| 403 for method in interface.methods: | 405 for method in interface.methods: |
| 404 result.append(method.param_struct) | 406 result.append(method.param_struct) |
| 405 if method.response_param_struct is not None: | 407 if method.response_param_struct is not None: |
| 406 result.append(method.response_param_struct) | 408 result.append(method.response_param_struct) |
| 407 return result | 409 return result |
| OLD | NEW |