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 23 matching lines...) Expand all Loading... |
34 mojom.STRING: '""', | 34 mojom.STRING: '""', |
35 mojom.NULLABLE_STRING: '""' | 35 mojom.NULLABLE_STRING: '""' |
36 } | 36 } |
37 | 37 |
38 | 38 |
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, kind=field.kind) |
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): |
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): |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 131 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
132 return "encodeArrayPointer(new codec.ArrayOf(codec.PackedBool), "; | 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, kind=None): |
142 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 142 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
143 # Both variable and enum constants are constructed like: | 143 # Both variable and enum constants are constructed like: |
144 # NamespaceUid.Struct[.Enum].CONSTANT_NAME | 144 # NamespaceUid.Struct[.Enum].CONSTANT_NAME |
145 name = [] | 145 name = [] |
146 if token.imported_from: | 146 if token.imported_from: |
147 name.append(token.imported_from["unique_name"]) | 147 name.append(token.imported_from["unique_name"]) |
148 if token.parent_kind: | 148 if token.parent_kind: |
149 name.append(token.parent_kind.name) | 149 name.append(token.parent_kind.name) |
150 if isinstance(token, mojom.EnumValue): | 150 if isinstance(token, mojom.EnumValue): |
151 name.append(token.enum_name) | 151 name.append(token.enum_name) |
152 name.append(token.name) | 152 name.append(token.name) |
153 return ".".join(name) | 153 return ".".join(name) |
| 154 |
| 155 if kind == mojom.DOUBLE or kind == mojom.FLOAT: |
| 156 if token == "Inf" or token == "+Inf": |
| 157 return "Infinity" |
| 158 if token == "-Inf": |
| 159 return "-Infinity" |
| 160 if token == "NaN": |
| 161 return "NaN" |
| 162 |
154 return token | 163 return token |
155 | 164 |
156 | 165 |
157 def ExpressionToText(value): | 166 def ExpressionToText(value, kind=None): |
158 return TranslateConstants(value) | 167 return TranslateConstants(value, kind) |
159 | 168 |
160 | 169 |
161 def JavascriptType(kind): | 170 def JavascriptType(kind): |
162 if kind.imported_from: | 171 if kind.imported_from: |
163 return kind.imported_from["unique_name"] + "." + kind.name | 172 return kind.imported_from["unique_name"] + "." + kind.name |
164 return kind.name | 173 return kind.name |
165 | 174 |
166 | 175 |
167 class Generator(generator.Generator): | 176 class Generator(generator.Generator): |
168 | 177 |
(...skipping 23 matching lines...) Expand all Loading... |
192 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 201 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
193 | 202 |
194 def GetImports(self): | 203 def GetImports(self): |
195 # Since each import is assigned a variable in JS, they need to have unique | 204 # Since each import is assigned a variable in JS, they need to have unique |
196 # names. | 205 # names. |
197 counter = 1 | 206 counter = 1 |
198 for each in self.module.imports: | 207 for each in self.module.imports: |
199 each["unique_name"] = "import" + str(counter) | 208 each["unique_name"] = "import" + str(counter) |
200 counter += 1 | 209 counter += 1 |
201 return self.module.imports | 210 return self.module.imports |
OLD | NEW |