| 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 from mojom.generate.template_expander import UseJinja | 10 from mojom.generate.template_expander import UseJinja |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 def CodecType(kind): | 103 def CodecType(kind): |
| 104 if kind in mojom.PRIMITIVES: | 104 if kind in mojom.PRIMITIVES: |
| 105 return _kind_to_codec_type[kind] | 105 return _kind_to_codec_type[kind] |
| 106 if mojom.IsStructKind(kind): | 106 if mojom.IsStructKind(kind): |
| 107 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ | 107 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ |
| 108 else "PointerTo" | 108 else "PointerTo" |
| 109 return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) | 109 return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) |
| 110 if mojom.IsArrayKind(kind): | 110 if mojom.IsArrayKind(kind): |
| 111 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" | 111 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" |
| 112 array_length = "" if kind.length is None else ", %d" % kind.length | 112 array_length = "" if kind.length is None else ", %d" % kind.length |
| 113 element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \ | 113 element_type = ElementCodecType(kind.kind) |
| 114 else CodecType(kind.kind) | |
| 115 return "new codec.%s(%s%s)" % (array_type, element_type, array_length) | 114 return "new codec.%s(%s%s)" % (array_type, element_type, array_length) |
| 116 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 115 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 117 return CodecType(mojom.MSGPIPE) | 116 return CodecType(mojom.MSGPIPE) |
| 118 if mojom.IsEnumKind(kind): | 117 if mojom.IsEnumKind(kind): |
| 119 return _kind_to_codec_type[mojom.INT32] | 118 return _kind_to_codec_type[mojom.INT32] |
| 119 if mojom.IsMapKind(kind): |
| 120 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" |
| 121 key_type = ElementCodecType(kind.key_kind) |
| 122 value_type = ElementCodecType(kind.value_kind) |
| 123 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) |
| 120 return kind | 124 return kind |
| 121 | 125 |
| 122 def MapCodecType(kind): | 126 def ElementCodecType(kind): |
| 123 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) | 127 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
| 124 | 128 |
| 125 def JavaScriptDecodeSnippet(kind): | 129 def JavaScriptDecodeSnippet(kind): |
| 126 if kind in mojom.PRIMITIVES: | 130 if kind in mojom.PRIMITIVES: |
| 127 return "decodeStruct(%s)" % CodecType(kind) | 131 return "decodeStruct(%s)" % CodecType(kind) |
| 128 if mojom.IsStructKind(kind): | 132 if mojom.IsStructKind(kind): |
| 129 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 133 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 130 if mojom.IsMapKind(kind): | 134 if mojom.IsMapKind(kind): |
| 131 return "decodeMapPointer(%s, %s)" % \ | 135 return "decodeMapPointer(%s, %s)" % \ |
| 132 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 136 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
| 133 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 137 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 134 return "decodeArrayPointer(codec.PackedBool)" | 138 return "decodeArrayPointer(codec.PackedBool)" |
| 135 if mojom.IsArrayKind(kind): | 139 if mojom.IsArrayKind(kind): |
| 136 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 140 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
| 137 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 141 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 138 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 142 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
| 139 if mojom.IsEnumKind(kind): | 143 if mojom.IsEnumKind(kind): |
| 140 return JavaScriptDecodeSnippet(mojom.INT32) | 144 return JavaScriptDecodeSnippet(mojom.INT32) |
| 141 | 145 |
| 142 | 146 |
| 143 def JavaScriptEncodeSnippet(kind): | 147 def JavaScriptEncodeSnippet(kind): |
| 144 if kind in mojom.PRIMITIVES: | 148 if kind in mojom.PRIMITIVES: |
| 145 return "encodeStruct(%s, " % CodecType(kind) | 149 return "encodeStruct(%s, " % CodecType(kind) |
| 146 if mojom.IsStructKind(kind): | 150 if mojom.IsStructKind(kind): |
| 147 return "encodeStructPointer(%s, " % JavaScriptType(kind) | 151 return "encodeStructPointer(%s, " % JavaScriptType(kind) |
| 148 if mojom.IsMapKind(kind): | 152 if mojom.IsMapKind(kind): |
| 149 return "encodeMapPointer(%s, %s, " % \ | 153 return "encodeMapPointer(%s, %s, " % \ |
| 150 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 154 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
| 151 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 155 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 152 return "encodeArrayPointer(codec.PackedBool, "; | 156 return "encodeArrayPointer(codec.PackedBool, "; |
| 153 if mojom.IsArrayKind(kind): | 157 if mojom.IsArrayKind(kind): |
| 154 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 158 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
| 155 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 159 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 156 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 160 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
| 157 if mojom.IsEnumKind(kind): | 161 if mojom.IsEnumKind(kind): |
| 158 return JavaScriptEncodeSnippet(mojom.INT32) | 162 return JavaScriptEncodeSnippet(mojom.INT32) |
| 159 | 163 |
| 160 | 164 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 177 return expected_dimension_sizes | 181 return expected_dimension_sizes |
| 178 | 182 |
| 179 | 183 |
| 180 def JavaScriptValidateArrayParams(packed_field): | 184 def JavaScriptValidateArrayParams(packed_field): |
| 181 nullable = JavaScriptNullableParam(packed_field) | 185 nullable = JavaScriptNullableParam(packed_field) |
| 182 field_offset = JavaScriptFieldOffset(packed_field) | 186 field_offset = JavaScriptFieldOffset(packed_field) |
| 183 element_kind = packed_field.field.kind.kind | 187 element_kind = packed_field.field.kind.kind |
| 184 element_size = pack.PackedField.GetSizeForKind(element_kind) | 188 element_size = pack.PackedField.GetSizeForKind(element_kind) |
| 185 expected_dimension_sizes = GetArrayExpectedDimensionSizes( | 189 expected_dimension_sizes = GetArrayExpectedDimensionSizes( |
| 186 packed_field.field.kind) | 190 packed_field.field.kind) |
| 187 element_type = "codec.PackedBool" if mojom.IsBoolKind(element_kind) \ | 191 element_type = ElementCodecType(element_kind) |
| 188 else CodecType(element_kind) | |
| 189 return "%s, %s, %s, %s, %s, 0" % \ | 192 return "%s, %s, %s, %s, %s, 0" % \ |
| 190 (field_offset, element_size, element_type, nullable, | 193 (field_offset, element_size, element_type, nullable, |
| 191 expected_dimension_sizes) | 194 expected_dimension_sizes) |
| 192 | 195 |
| 193 | 196 |
| 194 def JavaScriptValidateStructParams(packed_field): | 197 def JavaScriptValidateStructParams(packed_field): |
| 195 nullable = JavaScriptNullableParam(packed_field) | 198 nullable = JavaScriptNullableParam(packed_field) |
| 196 field_offset = JavaScriptFieldOffset(packed_field) | 199 field_offset = JavaScriptFieldOffset(packed_field) |
| 197 struct_type = JavaScriptType(packed_field.field.kind) | 200 struct_type = JavaScriptType(packed_field.field.kind) |
| 198 return "%s, %s, %s" % (field_offset, struct_type, nullable) | 201 return "%s, %s, %s" % (field_offset, struct_type, nullable) |
| 199 | 202 |
| 200 | 203 |
| 201 def JavaScriptValidateMapParams(packed_field): | 204 def JavaScriptValidateMapParams(packed_field): |
| 202 nullable = JavaScriptNullableParam(packed_field) | 205 nullable = JavaScriptNullableParam(packed_field) |
| 203 field_offset = JavaScriptFieldOffset(packed_field) | 206 field_offset = JavaScriptFieldOffset(packed_field) |
| 204 keys_type = MapCodecType(packed_field.field.kind.key_kind) | 207 keys_type = ElementCodecType(packed_field.field.kind.key_kind) |
| 205 values_kind = packed_field.field.kind.value_kind; | 208 values_kind = packed_field.field.kind.value_kind; |
| 206 values_type = MapCodecType(values_kind) | 209 values_type = ElementCodecType(values_kind) |
| 207 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" | 210 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" |
| 208 return "%s, %s, %s, %s, %s" % \ | 211 return "%s, %s, %s, %s, %s" % \ |
| 209 (field_offset, nullable, keys_type, values_type, values_nullable) | 212 (field_offset, nullable, keys_type, values_type, values_nullable) |
| 210 | 213 |
| 211 | 214 |
| 212 def JavaScriptValidateStringParams(packed_field): | 215 def JavaScriptValidateStringParams(packed_field): |
| 213 nullable = JavaScriptNullableParam(packed_field) | 216 nullable = JavaScriptNullableParam(packed_field) |
| 214 return "%s, %s" % (JavaScriptFieldOffset(packed_field), nullable) | 217 return "%s, %s" % (JavaScriptFieldOffset(packed_field), nullable) |
| 215 | 218 |
| 216 | 219 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 return self.module.imports | 345 return self.module.imports |
| 343 | 346 |
| 344 def GetImportedInterfaces(self): | 347 def GetImportedInterfaces(self): |
| 345 interface_to_import = {}; | 348 interface_to_import = {}; |
| 346 for each_import in self.module.imports: | 349 for each_import in self.module.imports: |
| 347 for each_interface in each_import["module"].interfaces: | 350 for each_interface in each_import["module"].interfaces: |
| 348 name = each_interface.name | 351 name = each_interface.name |
| 349 interface_to_import[name] = each_import["unique_name"] + "." + name | 352 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 350 return interface_to_import; | 353 return interface_to_import; |
| 351 | 354 |
| OLD | NEW |