Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: mojo/public/bindings/generators/mojom_js_generator.py

Issue 164873002: Fix bug with using enums as default values in mojom. We were previously (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 from generate import mojom 7 from generate import mojom
8 from generate import mojom_pack 8 from generate import mojom_pack
9 from generate import mojom_generator 9 from generate import mojom_generator
10 10
11 from generate.template_expander import UseJinja 11 from generate.template_expander import UseJinja
12 12
13
14 _kind_to_javascript_default_value = { 13 _kind_to_javascript_default_value = {
15 mojom.BOOL: "false", 14 mojom.BOOL: "false",
16 mojom.INT8: "0", 15 mojom.INT8: "0",
17 mojom.UINT8: "0", 16 mojom.UINT8: "0",
18 mojom.INT16: "0", 17 mojom.INT16: "0",
19 mojom.UINT16: "0", 18 mojom.UINT16: "0",
20 mojom.INT32: "0", 19 mojom.INT32: "0",
21 mojom.UINT32: "0", 20 mojom.UINT32: "0",
22 mojom.FLOAT: "0", 21 mojom.FLOAT: "0",
23 mojom.HANDLE: "core.kInvalidHandle", 22 mojom.HANDLE: "core.kInvalidHandle",
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 if kind in mojom.PRIMITIVES: 140 if kind in mojom.PRIMITIVES:
142 return _kind_to_javascript_encode_snippet[kind] 141 return _kind_to_javascript_encode_snippet[kind]
143 if isinstance(kind, mojom.Struct): 142 if isinstance(kind, mojom.Struct):
144 return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); 143 return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name);
145 if isinstance(kind, mojom.Array): 144 if isinstance(kind, mojom.Array):
146 return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); 145 return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind);
147 if isinstance(kind, mojom.Interface): 146 if isinstance(kind, mojom.Interface):
148 return JavaScriptEncodeSnippet(mojom.MSGPIPE) 147 return JavaScriptEncodeSnippet(mojom.MSGPIPE)
149 148
150 149
151 def SubstituteNamespace(value, imports): 150 def GetConstants(module):
152 for import_item in imports: 151 """Returns a generator that enumerates all constants that can be referenced
153 value = value.replace(import_item["namespace"] + ".", 152 from this module."""
154 import_item["unique_name"] + ".") 153 class Constant:
154 pass
155
156 for enum in module.enums:
157 for field in enum.fields:
158 constant = Constant()
159 constant.namespace = module.namespace
160 constant.is_current_namespace = True
161 constant.import_item = None
162 constant.name = (enum.name, field.name)
163 yield constant
164
165 for each in module.imports:
166 for enum in each["module"].enums:
167 for field in enum.fields:
168 constant = Constant()
169 constant.namespace = each["namespace"]
170 constant.is_current_namespace = constant.namespace == module.namespace
171 constant.import_item = each
172 constant.name = (enum.name, field.name)
173 yield constant
174
175
176 def TranslateConstants(value, module):
177 # We're assuming we're dealing with an identifier, but that may not be
178 # the case. If we're not, we just won't find any matches.
179 if value.find(".") != -1:
180 namespace, identifier = value.split(".")
181 else:
182 namespace, identifier = "", value
183
184 for constant in GetConstants(module):
185 if namespace == constant.namespace or (
186 namespace == "" and constant.is_current_namespace):
187 if constant.name[1] == identifier:
188 if constant.import_item:
189 return "%s.%s.%s" % (constant.import_item["unique_name"],
190 constant.name[0], constant.name[1])
191 else:
192 return "%s.%s" % (constant.name[0], constant.name[1])
155 return value 193 return value
156 194
157 195
196 def ExpressionToText(value, module):
197 if value[0] != "EXPRESSION":
198 raise Exception("Expected EXPRESSION, got" + value)
199 return "".join(mojom_generator.ExpressionMapper(value,
200 lambda token: TranslateConstants(token, module)))
201
202
158 def JavascriptType(kind): 203 def JavascriptType(kind):
159 if kind.imported_from: 204 if kind.imported_from:
160 return kind.imported_from["unique_name"] + "." + kind.name 205 return kind.imported_from["unique_name"] + "." + kind.name
161 return kind.name 206 return kind.name
162 207
163 208
164 class Generator(mojom_generator.Generator): 209 class Generator(mojom_generator.Generator):
165 210
166 js_filters = { 211 js_filters = {
167 "camel_to_underscores": mojom_generator.CamelToUnderscores, 212 "camel_to_underscores": mojom_generator.CamelToUnderscores,
168 "default_value": JavaScriptDefaultValue, 213 "default_value": JavaScriptDefaultValue,
169 "payload_size": JavaScriptPayloadSize, 214 "payload_size": JavaScriptPayloadSize,
170 "decode_snippet": JavaScriptDecodeSnippet, 215 "decode_snippet": JavaScriptDecodeSnippet,
171 "encode_snippet": JavaScriptEncodeSnippet, 216 "encode_snippet": JavaScriptEncodeSnippet,
217 "expression_to_text": ExpressionToText,
172 "is_object_kind": mojom_generator.IsObjectKind, 218 "is_object_kind": mojom_generator.IsObjectKind,
173 "is_string_kind": mojom_generator.IsStringKind, 219 "is_string_kind": mojom_generator.IsStringKind,
174 "is_array_kind": lambda kind: isinstance(kind, mojom.Array), 220 "is_array_kind": lambda kind: isinstance(kind, mojom.Array),
175 "js_type": JavascriptType, 221 "js_type": JavascriptType,
176 "stylize_method": mojom_generator.StudlyCapsToCamel, 222 "stylize_method": mojom_generator.StudlyCapsToCamel,
177 "substitute_namespace": SubstituteNamespace,
178 "verify_token_type": mojom_generator.VerifyTokenType, 223 "verify_token_type": mojom_generator.VerifyTokenType,
179 } 224 }
180 225
181 @UseJinja("js_templates/module.js.tmpl", filters=js_filters) 226 @UseJinja("js_templates/module.js.tmpl", filters=js_filters)
182 def GenerateJsModule(self): 227 def GenerateJsModule(self):
183 return { 228 return {
184 "imports": self.GetImports(), 229 "imports": self.GetImports(),
185 "kinds": self.module.kinds, 230 "kinds": self.module.kinds,
186 "enums": self.module.enums, 231 "enums": self.module.enums,
232 "module": self.module,
187 "structs": self.GetStructs() + self.GetStructsFromMethods(), 233 "structs": self.GetStructs() + self.GetStructsFromMethods(),
188 "interfaces": self.module.interfaces, 234 "interfaces": self.module.interfaces,
189 } 235 }
190 236
191 def GenerateFiles(self): 237 def GenerateFiles(self):
192 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) 238 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name)
193 239
194 def GetImports(self): 240 def GetImports(self):
195 # Since each import is assigned a variable in JS, they need to have unique 241 # Since each import is assigned a variable in JS, they need to have unique
196 # names. 242 # names.
197 counter = 1 243 counter = 1
198 for each in self.module.imports: 244 for each in self.module.imports:
199 each["unique_name"] = "import" + str(counter) 245 each["unique_name"] = "import" + str(counter)
200 counter += 1 246 counter += 1
201 return self.module.imports 247 return self.module.imports
OLDNEW
« no previous file with comments | « mojo/public/bindings/generators/mojom_cpp_generator.py ('k') | mojo/public/bindings/mojom_bindings_generator.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698