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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 mojom.DOUBLE: "codec.Double", | 78 mojom.DOUBLE: "codec.Double", |
79 mojom.STRING: "codec.String", | 79 mojom.STRING: "codec.String", |
80 } | 80 } |
81 | 81 |
82 | 82 |
83 def CodecType(kind): | 83 def CodecType(kind): |
84 if kind in mojom.PRIMITIVES: | 84 if kind in mojom.PRIMITIVES: |
85 return _kind_to_codec_type[kind] | 85 return _kind_to_codec_type[kind] |
86 if isinstance(kind, mojom.Struct): | 86 if isinstance(kind, mojom.Struct): |
87 return "new codec.PointerTo(%s)" % CodecType(kind.name) | 87 return "new codec.PointerTo(%s)" % CodecType(kind.name) |
| 88 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: |
| 89 return "new codec.ArrayOfBoolArrayPointers()" |
88 if isinstance(kind, mojom.Array): | 90 if isinstance(kind, mojom.Array): |
89 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) | 91 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) |
90 if isinstance(kind, mojom.Interface) or \ | 92 if isinstance(kind, mojom.Interface) or \ |
91 isinstance(kind, mojom.InterfaceRequest): | 93 isinstance(kind, mojom.InterfaceRequest): |
92 return CodecType(mojom.MSGPIPE) | 94 return CodecType(mojom.MSGPIPE) |
93 if isinstance(kind, mojom.Enum): | 95 if isinstance(kind, mojom.Enum): |
94 return _kind_to_codec_type[mojom.INT32] | 96 return _kind_to_codec_type[mojom.INT32] |
95 return kind | 97 return kind |
96 | 98 |
97 | 99 |
98 def JavaScriptDecodeSnippet(kind): | 100 def JavaScriptDecodeSnippet(kind): |
99 if kind in mojom.PRIMITIVES: | 101 if kind in mojom.PRIMITIVES: |
100 return "decodeStruct(%s)" % CodecType(kind) | 102 return "decodeStruct(%s)" % CodecType(kind) |
101 if isinstance(kind, mojom.Struct): | 103 if isinstance(kind, mojom.Struct): |
102 return "decodeStructPointer(%s)" % CodecType(kind.name) | 104 return "decodeStructPointer(%s)" % CodecType(kind.name) |
| 105 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: |
| 106 return "decodeBoolArrayPointer()" |
103 if isinstance(kind, mojom.Array): | 107 if isinstance(kind, mojom.Array): |
104 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 108 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
105 if isinstance(kind, mojom.Interface) or \ | 109 if isinstance(kind, mojom.Interface) or \ |
106 isinstance(kind, mojom.InterfaceRequest): | 110 isinstance(kind, mojom.InterfaceRequest): |
107 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 111 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
108 if isinstance(kind, mojom.Enum): | 112 if isinstance(kind, mojom.Enum): |
109 return JavaScriptDecodeSnippet(mojom.INT32) | 113 return JavaScriptDecodeSnippet(mojom.INT32) |
110 | 114 |
111 | 115 |
112 def JavaScriptEncodeSnippet(kind): | 116 def JavaScriptEncodeSnippet(kind): |
113 if kind in mojom.PRIMITIVES: | 117 if kind in mojom.PRIMITIVES: |
114 return "encodeStruct(%s, " % CodecType(kind) | 118 return "encodeStruct(%s, " % CodecType(kind) |
115 if isinstance(kind, mojom.Struct): | 119 if isinstance(kind, mojom.Struct): |
116 return "encodeStructPointer(%s, " % CodecType(kind.name) | 120 return "encodeStructPointer(%s, " % CodecType(kind.name) |
| 121 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: |
| 122 return "encodeBoolArrayPointer("; |
117 if isinstance(kind, mojom.Array): | 123 if isinstance(kind, mojom.Array): |
118 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 124 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
119 if isinstance(kind, mojom.Interface) or \ | 125 if isinstance(kind, mojom.Interface) or \ |
120 isinstance(kind, mojom.InterfaceRequest): | 126 isinstance(kind, mojom.InterfaceRequest): |
121 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 127 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
122 if isinstance(kind, mojom.Enum): | 128 if isinstance(kind, mojom.Enum): |
123 return JavaScriptEncodeSnippet(mojom.INT32) | 129 return JavaScriptEncodeSnippet(mojom.INT32) |
124 | 130 |
125 | 131 |
126 def TranslateConstants(token): | 132 def TranslateConstants(token): |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 183 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
178 | 184 |
179 def GetImports(self): | 185 def GetImports(self): |
180 # Since each import is assigned a variable in JS, they need to have unique | 186 # Since each import is assigned a variable in JS, they need to have unique |
181 # names. | 187 # names. |
182 counter = 1 | 188 counter = 1 |
183 for each in self.module.imports: | 189 for each in self.module.imports: |
184 each["unique_name"] = "import" + str(counter) | 190 each["unique_name"] = "import" + str(counter) |
185 counter += 1 | 191 counter += 1 |
186 return self.module.imports | 192 return self.module.imports |
OLD | NEW |