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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
101 return "new codec.ArrayOfBoolArrayPointers()" | 101 return "new codec.ArrayOf(new codec.ArrayOf(codec.PackedBool))" |
102 if mojom.IsArrayKind(kind): | 102 if mojom.IsArrayKind(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.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
117 return "decodeBoolArrayPointer()" | 117 return "decodeArrayPointer(new codec.ArrayOf(codec.PackedBool))" |
118 if mojom.IsArrayKind(kind): | 118 if mojom.IsArrayKind(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.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
132 return "encodeBoolArrayPointer("; | 132 return "encodeArrayPointer(new codec.ArrayOf(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 TranslateConstants(token): | 141 def TranslateConstants(token): |
142 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 142 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 192 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
193 | 193 |
194 def GetImports(self): | 194 def GetImports(self): |
195 # Since each import is assigned a variable in JS, they need to have unique | 195 # Since each import is assigned a variable in JS, they need to have unique |
196 # names. | 196 # names. |
197 counter = 1 | 197 counter = 1 |
198 for each in self.module.imports: | 198 for each in self.module.imports: |
199 each["unique_name"] = "import" + str(counter) | 199 each["unique_name"] = "import" + str(counter) |
200 counter += 1 | 200 counter += 1 |
201 return self.module.imports | 201 return self.module.imports |
OLD | NEW |