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