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

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

Issue 459873003: Mojom generator: move Is.*Kind() functions into module.py and use them from all mojom_.*_generator.… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix java compilation Created 6 years, 4 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 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
11 11
12 _kind_to_javascript_default_value = { 12 _kind_to_javascript_default_value = {
13 mojom.BOOL: "false", 13 mojom.BOOL: "false",
14 mojom.INT8: "0", 14 mojom.INT8: "0",
15 mojom.UINT8: "0", 15 mojom.UINT8: "0",
16 mojom.INT16: "0", 16 mojom.INT16: "0",
17 mojom.UINT16: "0", 17 mojom.UINT16: "0",
18 mojom.INT32: "0", 18 mojom.INT32: "0",
19 mojom.UINT32: "0", 19 mojom.UINT32: "0",
20 mojom.FLOAT: "0", 20 mojom.FLOAT: "0",
21 mojom.HANDLE: "null", 21 mojom.HANDLE: "null",
22 mojom.DCPIPE: "null", 22 mojom.DCPIPE: "null",
23 mojom.DPPIPE: "null", 23 mojom.DPPIPE: "null",
24 mojom.MSGPIPE: "null", 24 mojom.MSGPIPE: "null",
25 mojom.SHAREDBUFFER: "null", 25 mojom.SHAREDBUFFER: "null",
26 mojom.INT64: "0", 26 mojom.NULLABLE_HANDLE: "null",
27 mojom.UINT64: "0", 27 mojom.NULLABLE_DCPIPE: "null",
28 mojom.DOUBLE: "0", 28 mojom.NULLABLE_DPPIPE: "null",
29 mojom.STRING: '""', 29 mojom.NULLABLE_MSGPIPE: "null",
30 mojom.NULLABLE_SHAREDBUFFER: "null",
31 mojom.INT64: "0",
32 mojom.UINT64: "0",
33 mojom.DOUBLE: "0",
34 mojom.STRING: '""',
35 mojom.NULLABLE_STRING: '""'
30 } 36 }
31 37
32 38
33 def JavaScriptDefaultValue(field): 39 def JavaScriptDefaultValue(field):
34 if field.default: 40 if field.default:
35 if isinstance(field.kind, mojom.Struct): 41 if mojom.IsStructKind(field.kind):
36 assert field.default == "default" 42 assert field.default == "default"
37 return "new %s()" % JavascriptType(field.kind) 43 return "new %s()" % JavascriptType(field.kind)
38 return ExpressionToText(field.default) 44 return ExpressionToText(field.default)
39 if field.kind in mojom.PRIMITIVES: 45 if field.kind in mojom.PRIMITIVES:
40 return _kind_to_javascript_default_value[field.kind] 46 return _kind_to_javascript_default_value[field.kind]
41 if isinstance(field.kind, mojom.Struct): 47 if mojom.IsStructKind(field.kind):
42 return "null" 48 return "null"
43 if isinstance(field.kind, mojom.Array): 49 if mojom.IsArrayKind(field.kind):
44 return "null" 50 return "null"
45 if isinstance(field.kind, mojom.Interface) or \ 51 if mojom.IsInterfaceKind(field.kind) or \
46 isinstance(field.kind, mojom.InterfaceRequest): 52 mojom.IsInterfaceRequestKind(field.kind):
47 return _kind_to_javascript_default_value[mojom.MSGPIPE] 53 return _kind_to_javascript_default_value[mojom.MSGPIPE]
48 if isinstance(field.kind, mojom.Enum): 54 if mojom.IsEnumKind(field.kind):
49 return "0" 55 return "0"
50 56
51 57
52 def JavaScriptPayloadSize(packed): 58 def JavaScriptPayloadSize(packed):
53 packed_fields = packed.packed_fields 59 packed_fields = packed.packed_fields
54 if not packed_fields: 60 if not packed_fields:
55 return 0 61 return 0
56 last_field = packed_fields[-1] 62 last_field = packed_fields[-1]
57 offset = last_field.offset + last_field.size 63 offset = last_field.offset + last_field.size
58 pad = pack.GetPad(offset, 8) 64 pad = pack.GetPad(offset, 8)
59 return offset + pad 65 return offset + pad
60 66
61 67
62 _kind_to_codec_type = { 68 _kind_to_codec_type = {
63 mojom.BOOL: "codec.Uint8", 69 mojom.BOOL: "codec.Uint8",
64 mojom.INT8: "codec.Int8", 70 mojom.INT8: "codec.Int8",
65 mojom.UINT8: "codec.Uint8", 71 mojom.UINT8: "codec.Uint8",
66 mojom.INT16: "codec.Int16", 72 mojom.INT16: "codec.Int16",
67 mojom.UINT16: "codec.Uint16", 73 mojom.UINT16: "codec.Uint16",
68 mojom.INT32: "codec.Int32", 74 mojom.INT32: "codec.Int32",
69 mojom.UINT32: "codec.Uint32", 75 mojom.UINT32: "codec.Uint32",
70 mojom.FLOAT: "codec.Float", 76 mojom.FLOAT: "codec.Float",
71 mojom.HANDLE: "codec.Handle", 77 mojom.HANDLE: "codec.Handle",
72 mojom.DCPIPE: "codec.Handle", 78 mojom.DCPIPE: "codec.Handle",
73 mojom.DPPIPE: "codec.Handle", 79 mojom.DPPIPE: "codec.Handle",
74 mojom.MSGPIPE: "codec.Handle", 80 mojom.MSGPIPE: "codec.Handle",
75 mojom.SHAREDBUFFER: "codec.Handle", 81 mojom.SHAREDBUFFER: "codec.Handle",
76 mojom.INT64: "codec.Int64", 82 mojom.NULLABLE_HANDLE: "codec.Handle",
77 mojom.UINT64: "codec.Uint64", 83 mojom.NULLABLE_DCPIPE: "codec.Handle",
78 mojom.DOUBLE: "codec.Double", 84 mojom.NULLABLE_DPPIPE: "codec.Handle",
79 mojom.STRING: "codec.String", 85 mojom.NULLABLE_MSGPIPE: "codec.Handle",
86 mojom.NULLABLE_SHAREDBUFFER: "codec.Handle",
87 mojom.INT64: "codec.Int64",
88 mojom.UINT64: "codec.Uint64",
89 mojom.DOUBLE: "codec.Double",
90 mojom.STRING: "codec.String",
91 mojom.NULLABLE_STRING: "codec.String",
80 } 92 }
81 93
82 94
83 def CodecType(kind): 95 def CodecType(kind):
84 if kind in mojom.PRIMITIVES: 96 if kind in mojom.PRIMITIVES:
85 return _kind_to_codec_type[kind] 97 return _kind_to_codec_type[kind]
86 if isinstance(kind, mojom.Struct): 98 if mojom.IsStructKind(kind):
87 return "new codec.PointerTo(%s)" % CodecType(kind.name) 99 return "new codec.PointerTo(%s)" % CodecType(kind.name)
88 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: 100 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
89 return "new codec.ArrayOfBoolArrayPointers()" 101 return "new codec.ArrayOfBoolArrayPointers()"
90 if isinstance(kind, mojom.Array): 102 if mojom.IsArrayKind(kind):
91 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) 103 return "new codec.ArrayOf(%s)" % CodecType(kind.kind)
92 if isinstance(kind, mojom.Interface) or \ 104 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
93 isinstance(kind, mojom.InterfaceRequest):
94 return CodecType(mojom.MSGPIPE) 105 return CodecType(mojom.MSGPIPE)
95 if isinstance(kind, mojom.Enum): 106 if mojom.IsEnumKind(kind):
96 return _kind_to_codec_type[mojom.INT32] 107 return _kind_to_codec_type[mojom.INT32]
97 return kind 108 return kind
98 109
99 110
100 def JavaScriptDecodeSnippet(kind): 111 def JavaScriptDecodeSnippet(kind):
101 if kind in mojom.PRIMITIVES: 112 if kind in mojom.PRIMITIVES:
102 return "decodeStruct(%s)" % CodecType(kind) 113 return "decodeStruct(%s)" % CodecType(kind)
103 if isinstance(kind, mojom.Struct): 114 if mojom.IsStructKind(kind):
104 return "decodeStructPointer(%s)" % CodecType(kind.name) 115 return "decodeStructPointer(%s)" % CodecType(kind.name)
105 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: 116 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
106 return "decodeBoolArrayPointer()" 117 return "decodeBoolArrayPointer()"
107 if isinstance(kind, mojom.Array): 118 if mojom.IsArrayKind(kind):
108 return "decodeArrayPointer(%s)" % CodecType(kind.kind) 119 return "decodeArrayPointer(%s)" % CodecType(kind.kind)
109 if isinstance(kind, mojom.Interface) or \ 120 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
110 isinstance(kind, mojom.InterfaceRequest):
111 return JavaScriptDecodeSnippet(mojom.MSGPIPE) 121 return JavaScriptDecodeSnippet(mojom.MSGPIPE)
112 if isinstance(kind, mojom.Enum): 122 if mojom.IsEnumKind(kind):
113 return JavaScriptDecodeSnippet(mojom.INT32) 123 return JavaScriptDecodeSnippet(mojom.INT32)
114 124
115 125
116 def JavaScriptEncodeSnippet(kind): 126 def JavaScriptEncodeSnippet(kind):
117 if kind in mojom.PRIMITIVES: 127 if kind in mojom.PRIMITIVES:
118 return "encodeStruct(%s, " % CodecType(kind) 128 return "encodeStruct(%s, " % CodecType(kind)
119 if isinstance(kind, mojom.Struct): 129 if mojom.IsStructKind(kind):
120 return "encodeStructPointer(%s, " % CodecType(kind.name) 130 return "encodeStructPointer(%s, " % CodecType(kind.name)
121 if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL: 131 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
122 return "encodeBoolArrayPointer("; 132 return "encodeBoolArrayPointer(";
123 if isinstance(kind, (mojom.Array, mojom.FixedArray)): 133 if mojom.IsAnyArrayKind(kind):
124 return "encodeArrayPointer(%s, " % CodecType(kind.kind) 134 return "encodeArrayPointer(%s, " % CodecType(kind.kind)
125 if isinstance(kind, mojom.Interface) or \ 135 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
126 isinstance(kind, mojom.InterfaceRequest):
127 return JavaScriptEncodeSnippet(mojom.MSGPIPE) 136 return JavaScriptEncodeSnippet(mojom.MSGPIPE)
128 if isinstance(kind, mojom.Enum): 137 if mojom.IsEnumKind(kind):
129 return JavaScriptEncodeSnippet(mojom.INT32) 138 return JavaScriptEncodeSnippet(mojom.INT32)
130 139
131 140
132 def TranslateConstants(token): 141 def TranslateConstants(token):
133 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): 142 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
134 # Both variable and enum constants are constructed like: 143 # Both variable and enum constants are constructed like:
135 # NamespaceUid.Struct[.Enum].CONSTANT_NAME 144 # NamespaceUid.Struct[.Enum].CONSTANT_NAME
136 name = [] 145 name = []
137 if token.imported_from: 146 if token.imported_from:
138 name.append(token.imported_from["unique_name"]) 147 name.append(token.imported_from["unique_name"])
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) 192 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name)
184 193
185 def GetImports(self): 194 def GetImports(self):
186 # 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
187 # names. 196 # names.
188 counter = 1 197 counter = 1
189 for each in self.module.imports: 198 for each in self.module.imports:
190 each["unique_name"] = "import" + str(counter) 199 each["unique_name"] = "import" + str(counter)
191 counter += 1 200 counter += 1
192 return self.module.imports 201 return self.module.imports
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698