| 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 18 matching lines...) Expand all Loading... |
| 29 mojom.NULLABLE_MSGPIPE: "null", | 29 mojom.NULLABLE_MSGPIPE: "null", |
| 30 mojom.NULLABLE_SHAREDBUFFER: "null", | 30 mojom.NULLABLE_SHAREDBUFFER: "null", |
| 31 mojom.INT64: "0", | 31 mojom.INT64: "0", |
| 32 mojom.UINT64: "0", | 32 mojom.UINT64: "0", |
| 33 mojom.DOUBLE: "0", | 33 mojom.DOUBLE: "0", |
| 34 mojom.STRING: '""', | 34 mojom.STRING: '""', |
| 35 mojom.NULLABLE_STRING: '""' | 35 mojom.NULLABLE_STRING: '""' |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 def JavaScriptType(kind): |
| 40 if kind.imported_from: |
| 41 return kind.imported_from["unique_name"] + "." + kind.name |
| 42 return kind.name |
| 43 |
| 44 |
| 39 def JavaScriptDefaultValue(field): | 45 def JavaScriptDefaultValue(field): |
| 40 if field.default: | 46 if field.default: |
| 41 if mojom.IsStructKind(field.kind): | 47 if mojom.IsStructKind(field.kind): |
| 42 assert field.default == "default" | 48 assert field.default == "default" |
| 43 return "new %s()" % JavascriptType(field.kind) | 49 return "new %s()" % JavaScriptType(field.kind) |
| 44 return ExpressionToText(field.default) | 50 return ExpressionToText(field.default) |
| 45 if field.kind in mojom.PRIMITIVES: | 51 if field.kind in mojom.PRIMITIVES: |
| 46 return _kind_to_javascript_default_value[field.kind] | 52 return _kind_to_javascript_default_value[field.kind] |
| 47 if mojom.IsStructKind(field.kind): | 53 if mojom.IsStructKind(field.kind): |
| 48 return "null" | 54 return "null" |
| 49 if mojom.IsAnyArrayKind(field.kind): | 55 if mojom.IsAnyArrayKind(field.kind): |
| 50 return "null" | 56 return "null" |
| 51 if mojom.IsInterfaceKind(field.kind) or \ | 57 if mojom.IsInterfaceKind(field.kind) or \ |
| 52 mojom.IsInterfaceRequestKind(field.kind): | 58 mojom.IsInterfaceRequestKind(field.kind): |
| 53 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 59 return _kind_to_javascript_default_value[mojom.MSGPIPE] |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 mojom.DOUBLE: "codec.Double", | 95 mojom.DOUBLE: "codec.Double", |
| 90 mojom.STRING: "codec.String", | 96 mojom.STRING: "codec.String", |
| 91 mojom.NULLABLE_STRING: "codec.String", | 97 mojom.NULLABLE_STRING: "codec.String", |
| 92 } | 98 } |
| 93 | 99 |
| 94 | 100 |
| 95 def CodecType(kind): | 101 def CodecType(kind): |
| 96 if kind in mojom.PRIMITIVES: | 102 if kind in mojom.PRIMITIVES: |
| 97 return _kind_to_codec_type[kind] | 103 return _kind_to_codec_type[kind] |
| 98 if mojom.IsStructKind(kind): | 104 if mojom.IsStructKind(kind): |
| 99 return "new codec.PointerTo(%s)" % CodecType(kind.name) | 105 return "new codec.PointerTo(%s)" % JavaScriptType(kind) |
| 100 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 106 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 101 return "new codec.ArrayOf(codec.PackedBool)" | 107 return "new codec.ArrayOf(codec.PackedBool)" |
| 102 if mojom.IsAnyArrayKind(kind): | 108 if mojom.IsAnyArrayKind(kind): |
| 103 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) | 109 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) |
| 104 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 110 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 105 return CodecType(mojom.MSGPIPE) | 111 return CodecType(mojom.MSGPIPE) |
| 106 if mojom.IsEnumKind(kind): | 112 if mojom.IsEnumKind(kind): |
| 107 return _kind_to_codec_type[mojom.INT32] | 113 return _kind_to_codec_type[mojom.INT32] |
| 108 return kind | 114 return kind |
| 109 | 115 |
| 110 | 116 |
| 111 def JavaScriptDecodeSnippet(kind): | 117 def JavaScriptDecodeSnippet(kind): |
| 112 if kind in mojom.PRIMITIVES: | 118 if kind in mojom.PRIMITIVES: |
| 113 return "decodeStruct(%s)" % CodecType(kind) | 119 return "decodeStruct(%s)" % CodecType(kind) |
| 114 if mojom.IsStructKind(kind): | 120 if mojom.IsStructKind(kind): |
| 115 return "decodeStructPointer(%s)" % CodecType(kind.name) | 121 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 116 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 122 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 117 return "decodeArrayPointer(codec.PackedBool)" | 123 return "decodeArrayPointer(codec.PackedBool)" |
| 118 if mojom.IsAnyArrayKind(kind): | 124 if mojom.IsAnyArrayKind(kind): |
| 119 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 125 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
| 120 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 126 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 121 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 127 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
| 122 if mojom.IsEnumKind(kind): | 128 if mojom.IsEnumKind(kind): |
| 123 return JavaScriptDecodeSnippet(mojom.INT32) | 129 return JavaScriptDecodeSnippet(mojom.INT32) |
| 124 | 130 |
| 125 | 131 |
| 126 def JavaScriptEncodeSnippet(kind): | 132 def JavaScriptEncodeSnippet(kind): |
| 127 if kind in mojom.PRIMITIVES: | 133 if kind in mojom.PRIMITIVES: |
| 128 return "encodeStruct(%s, " % CodecType(kind) | 134 return "encodeStruct(%s, " % CodecType(kind) |
| 129 if mojom.IsStructKind(kind): | 135 if mojom.IsStructKind(kind): |
| 130 return "encodeStructPointer(%s, " % CodecType(kind.name) | 136 return "encodeStructPointer(%s, " % JavaScriptType(kind) |
| 131 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 137 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 132 return "encodeArrayPointer(codec.PackedBool, "; | 138 return "encodeArrayPointer(codec.PackedBool, "; |
| 133 if mojom.IsAnyArrayKind(kind): | 139 if mojom.IsAnyArrayKind(kind): |
| 134 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 140 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
| 135 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 141 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 136 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 142 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
| 137 if mojom.IsEnumKind(kind): | 143 if mojom.IsEnumKind(kind): |
| 138 return JavaScriptEncodeSnippet(mojom.INT32) | 144 return JavaScriptEncodeSnippet(mojom.INT32) |
| 139 | 145 |
| 140 | 146 |
| 141 def JavaScriptFieldOffset(packed_field): | 147 def JavaScriptFieldOffset(packed_field): |
| 142 return "offset + codec.kStructHeaderSize + %s" % packed_field.offset | 148 return "offset + codec.kStructHeaderSize + %s" % packed_field.offset |
| 143 | 149 |
| 144 def JavaScriptValidateArrayParams(pf): | 150 def JavaScriptValidateArrayParams(pf): |
| 145 elementKind = pf.field.kind.kind | 151 elementKind = pf.field.kind.kind |
| 146 elementSize = pack.PackedField.GetSizeForKind(elementKind) | 152 elementSize = pack.PackedField.GetSizeForKind(elementKind) |
| 147 elementCount = generator.ExpectedArraySize(pf.field.kind) | 153 elementCount = generator.ExpectedArraySize(pf.field.kind) |
| 148 elementType = "codec.PackedBool" if mojom.IsBoolKind(elementKind) \ | 154 elementType = "codec.PackedBool" if mojom.IsBoolKind(elementKind) \ |
| 149 else CodecType(elementKind) | 155 else CodecType(elementKind) |
| 150 return "%s, %s, %s, %s" % \ | 156 return "%s, %s, %s, %s" % \ |
| 151 (JavaScriptFieldOffset(pf), elementSize, elementCount, elementType) | 157 (JavaScriptFieldOffset(pf), elementSize, elementCount, elementType) |
| 152 | 158 |
| 153 def JavaScriptValidateStructParams(pf): | 159 def JavaScriptValidateStructParams(pf): |
| 154 return "%s, %s" % (JavaScriptFieldOffset(pf), pf.field.kind.name) | 160 structType = JavaScriptType(pf.field.kind) |
| 161 return "%s, %s" % (JavaScriptFieldOffset(pf), structType) |
| 155 | 162 |
| 156 | 163 |
| 157 def TranslateConstants(token): | 164 def TranslateConstants(token): |
| 158 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 165 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
| 159 # Both variable and enum constants are constructed like: | 166 # Both variable and enum constants are constructed like: |
| 160 # NamespaceUid.Struct[.Enum].CONSTANT_NAME | 167 # NamespaceUid.Struct[.Enum].CONSTANT_NAME |
| 161 name = [] | 168 name = [] |
| 162 if token.imported_from: | 169 if token.imported_from: |
| 163 name.append(token.imported_from["unique_name"]) | 170 name.append(token.imported_from["unique_name"]) |
| 164 if token.parent_kind: | 171 if token.parent_kind: |
| 165 name.append(token.parent_kind.name) | 172 name.append(token.parent_kind.name) |
| 166 if isinstance(token, mojom.EnumValue): | 173 if isinstance(token, mojom.EnumValue): |
| 167 name.append(token.enum_name) | 174 name.append(token.enum_name) |
| 168 name.append(token.name) | 175 name.append(token.name) |
| 169 return ".".join(name) | 176 return ".".join(name) |
| 170 return token | 177 return token |
| 171 | 178 |
| 172 | 179 |
| 173 def ExpressionToText(value): | 180 def ExpressionToText(value): |
| 174 return TranslateConstants(value) | 181 return TranslateConstants(value) |
| 175 | 182 |
| 176 | 183 |
| 177 def JavascriptType(kind): | |
| 178 if kind.imported_from: | |
| 179 return kind.imported_from["unique_name"] + "." + kind.name | |
| 180 return kind.name | |
| 181 | |
| 182 def IsArrayPointerField(field): | 184 def IsArrayPointerField(field): |
| 183 return mojom.IsAnyArrayKind(field.kind) | 185 return mojom.IsAnyArrayKind(field.kind) |
| 184 | 186 |
| 185 def IsStringPointerField(field): | 187 def IsStringPointerField(field): |
| 186 return mojom.IsStringKind(field.kind) | 188 return mojom.IsStringKind(field.kind) |
| 187 | 189 |
| 188 def IsStructPointerField(field): | 190 def IsStructPointerField(field): |
| 189 return mojom.IsStructKind(field.kind) | 191 return mojom.IsStructKind(field.kind) |
| 190 | 192 |
| 191 def IsHandleField(field): | 193 def IsHandleField(field): |
| 192 return mojom.IsAnyHandleKind(field.kind) | 194 return mojom.IsAnyHandleKind(field.kind) |
| 193 | 195 |
| 194 | 196 |
| 195 class Generator(generator.Generator): | 197 class Generator(generator.Generator): |
| 196 | 198 |
| 197 js_filters = { | 199 js_filters = { |
| 198 "default_value": JavaScriptDefaultValue, | 200 "default_value": JavaScriptDefaultValue, |
| 199 "payload_size": JavaScriptPayloadSize, | 201 "payload_size": JavaScriptPayloadSize, |
| 200 "decode_snippet": JavaScriptDecodeSnippet, | 202 "decode_snippet": JavaScriptDecodeSnippet, |
| 201 "encode_snippet": JavaScriptEncodeSnippet, | 203 "encode_snippet": JavaScriptEncodeSnippet, |
| 202 "expression_to_text": ExpressionToText, | 204 "expression_to_text": ExpressionToText, |
| 203 "field_offset": JavaScriptFieldOffset, | 205 "field_offset": JavaScriptFieldOffset, |
| 204 "has_callbacks": mojom.HasCallbacks, | 206 "has_callbacks": mojom.HasCallbacks, |
| 205 "is_array_pointer_field": IsArrayPointerField, | 207 "is_array_pointer_field": IsArrayPointerField, |
| 206 "is_struct_pointer_field": IsStructPointerField, | 208 "is_struct_pointer_field": IsStructPointerField, |
| 207 "is_string_pointer_field": IsStringPointerField, | 209 "is_string_pointer_field": IsStringPointerField, |
| 208 "is_handle_field": IsHandleField, | 210 "is_handle_field": IsHandleField, |
| 209 "js_type": JavascriptType, | 211 "js_type": JavaScriptType, |
| 210 "stylize_method": generator.StudlyCapsToCamel, | 212 "stylize_method": generator.StudlyCapsToCamel, |
| 211 "validate_array_params": JavaScriptValidateArrayParams, | 213 "validate_array_params": JavaScriptValidateArrayParams, |
| 212 "validate_struct_params": JavaScriptValidateStructParams, | 214 "validate_struct_params": JavaScriptValidateStructParams, |
| 213 } | 215 } |
| 214 | 216 |
| 215 @UseJinja("js_templates/module.js.tmpl", filters=js_filters) | 217 @UseJinja("js_templates/module.js.tmpl", filters=js_filters) |
| 216 def GenerateJsModule(self): | 218 def GenerateJsModule(self): |
| 217 return { | 219 return { |
| 218 "namespace": self.module.namespace, | 220 "namespace": self.module.namespace, |
| 219 "imports": self.GetImports(), | 221 "imports": self.GetImports(), |
| 220 "kinds": self.module.kinds, | 222 "kinds": self.module.kinds, |
| 221 "enums": self.module.enums, | 223 "enums": self.module.enums, |
| 222 "module": self.module, | 224 "module": self.module, |
| 223 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 225 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
| 224 "interfaces": self.module.interfaces, | 226 "interfaces": self.module.interfaces, |
| 225 } | 227 } |
| 226 | 228 |
| 227 def GenerateFiles(self, args): | 229 def GenerateFiles(self, args): |
| 228 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 230 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
| 229 | 231 |
| 230 def GetImports(self): | 232 def GetImports(self): |
| 231 # Since each import is assigned a variable in JS, they need to have unique | 233 # Since each import is assigned a variable in JS, they need to have unique |
| 232 # names. | 234 # names. |
| 233 counter = 1 | 235 counter = 1 |
| 234 for each in self.module.imports: | 236 for each in self.module.imports: |
| 235 each["unique_name"] = "import" + str(counter) | 237 each["unique_name"] = "import" + str(counter) |
| 236 counter += 1 | 238 counter += 1 |
| 237 return self.module.imports | 239 return self.module.imports |
| OLD | NEW |