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