OLD | NEW |
1 {%- macro union_def(union) %} | 1 {%- macro union_def(union) %} |
2 function {{union.name}}(value) { | 2 function {{union.stylized_name}}(value) { |
3 this.initDefault_(); | 3 this.initDefault_(); |
4 this.initValue_(value); | 4 this.initValue_(value); |
5 } | 5 } |
6 | 6 |
7 {{tags(union)}} | 7 {{tags(union)}} |
8 | 8 |
9 {{union.name}}.prototype.initDefault_ = function() { | 9 {{union.stylized_name}}.prototype.initDefault_ = function() { |
10 this.$data = null; | 10 this.$data = null; |
11 this.$tag = undefined; | 11 this.$tag = undefined; |
12 } | 12 } |
13 | 13 |
14 {{union.name}}.prototype.initValue_ = function(value) { | 14 {{union.stylized_name}}.prototype.initValue_ = function(value) { |
15 if (value == undefined) { | 15 if (value == undefined) { |
16 return; | 16 return; |
17 } | 17 } |
18 | 18 |
19 var keys = Object.keys(value); | 19 var keys = Object.keys(value); |
20 if (keys.length == 0) { | 20 if (keys.length == 0) { |
21 return; | 21 return; |
22 } | 22 } |
23 | 23 |
24 if (keys.length > 1) { | 24 if (keys.length > 1) { |
25 throw new TypeError("You may set only one member on a union."); | 25 throw new TypeError("You may set only one member on a union."); |
26 } | 26 } |
27 | 27 |
28 var fields = [ | 28 var fields = [ |
29 {%- for field in union.fields %} | 29 {%- for field in union.fields %} |
30 "{{field.name}}", | 30 "{{field.stylized_name}}", |
31 {%- endfor %} | 31 {%- endfor %} |
32 ]; | 32 ]; |
33 | 33 |
34 if (fields.indexOf(keys[0]) < 0) { | 34 if (fields.indexOf(keys[0]) < 0) { |
35 throw new ReferenceError(keys[0] + " is not a {{union.name}} member."); | 35 throw new ReferenceError( |
| 36 keys[0] + " is not a {{union.stylized_name}} member."); |
36 | 37 |
37 } | 38 } |
38 | 39 |
39 this[keys[0]] = value[keys[0]]; | 40 this[keys[0]] = value[keys[0]]; |
40 } | 41 } |
41 | 42 |
42 {%- for field in union.fields %} | 43 {%- for field in union.fields %} |
43 Object.defineProperty({{union.name}}.prototype, "{{field.name}}", { | 44 Object.defineProperty({{union.stylized_name}}.prototype, |
| 45 "{{field.stylized_name}}", { |
44 get: function() { | 46 get: function() { |
45 if (this.$tag != {{union.name}}.Tags.{{field.name}}) { | 47 if (this.$tag != {{union.stylized_name}}.Tags.{{field.stylized_name}}) { |
46 throw new ReferenceError( | 48 throw new ReferenceError( |
47 "{{union.name}}.{{field.name}} is not currently set."); | 49 "{{union.stylized_name}}.{{field.stylized_name}} is not currently set.
"); |
48 } | 50 } |
49 return this.$data; | 51 return this.$data; |
50 }, | 52 }, |
51 | 53 |
52 set: function(value) { | 54 set: function(value) { |
53 this.$tag = {{union.name}}.Tags.{{field.name}}; | 55 this.$tag = {{union.stylized_name}}.Tags.{{field.stylized_name}}; |
54 this.$data = value; | 56 this.$data = value; |
55 } | 57 } |
56 }); | 58 }); |
57 {%- endfor %} | 59 {%- endfor %} |
58 | 60 |
59 {{encode(union)|indent(2)}} | 61 {{encode(union)|indent(2)}} |
60 | 62 |
61 {{decode(union)|indent(2)}} | 63 {{decode(union)|indent(2)}} |
62 | 64 |
63 {{validate(union)|indent(2)}} | 65 {{validate(union)|indent(2)}} |
64 | 66 |
65 {{union.name}}.encodedSize = 16; | 67 {{union.stylized_name}}.encodedSize = 16; |
66 {%- endmacro %} | 68 {%- endmacro %} |
67 | 69 |
68 {%- macro tags(union) %} | 70 {%- macro tags(union) %} |
69 {{union.name}}.Tags = { | 71 {{union.stylized_name}}.Tags = { |
70 {%- for field in union.fields %} | 72 {%- for field in union.fields %} |
71 {{field.name}}: {{field.ordinal}}, | 73 {{field.stylized_name}}: {{field.ordinal}}, |
72 {%- endfor %} | 74 {%- endfor %} |
73 }; | 75 }; |
74 {%- endmacro %} | 76 {%- endmacro %} |
75 | 77 |
76 {%- macro encode(union) %} | 78 {%- macro encode(union) %} |
77 {{union.name}}.encode = function(encoder, val) { | 79 {{union.stylized_name}}.encode = function(encoder, val) { |
78 if (val == null) { | 80 if (val == null) { |
79 encoder.writeUint64(0); | 81 encoder.writeUint64(0); |
80 encoder.writeUint64(0); | 82 encoder.writeUint64(0); |
81 return; | 83 return; |
82 } | 84 } |
83 if (val.$tag == undefined) { | 85 if (val.$tag == undefined) { |
84 throw new TypeError("Cannot encode unions with an unknown member set."); | 86 throw new TypeError("Cannot encode unions with an unknown member set."); |
85 } | 87 } |
86 | 88 |
87 encoder.writeUint32(16); | 89 encoder.writeUint32(16); |
88 encoder.writeUint32(val.$tag); | 90 encoder.writeUint32(val.$tag); |
89 switch (val.$tag) { | 91 switch (val.$tag) { |
90 {%- for field in union.fields %} | 92 {%- for field in union.fields %} |
91 case {{union.name}}.Tags.{{field.name}}: | 93 case {{union.stylized_name}}.Tags.{{field.stylized_name}}: |
92 {%- if field|is_bool_field %} | 94 {%- if field|is_bool_field %} |
93 encoder.writeUint8(val.{{field.name}} ? 1 : 0); | 95 encoder.writeUint8(val.{{field.stylized_name}} ? 1 : 0); |
94 {%- else %} | 96 {%- else %} |
95 encoder.{{field.kind|union_encode_snippet}}val.{{field.name}}); | 97 encoder.{{field.kind|union_encode_snippet}}val.{{field.stylized_name}}); |
96 {%- endif %} | 98 {%- endif %} |
97 break; | 99 break; |
98 {%- endfor %} | 100 {%- endfor %} |
99 } | 101 } |
100 encoder.align(); | 102 encoder.align(); |
101 }; | 103 }; |
102 {%- endmacro %} | 104 {%- endmacro %} |
103 | 105 |
104 {%- macro decode(union) %} | 106 {%- macro decode(union) %} |
105 {{union.name}}.decode = function(decoder) { | 107 {{union.stylized_name}}.decode = function(decoder) { |
106 var size = decoder.readUint32(); | 108 var size = decoder.readUint32(); |
107 if (size == 0) { | 109 if (size == 0) { |
108 decoder.readUint32(); | 110 decoder.readUint32(); |
109 decoder.readUint64(); | 111 decoder.readUint64(); |
110 return null; | 112 return null; |
111 } | 113 } |
112 | 114 |
113 var result = new {{union.name}}(); | 115 var result = new {{union.stylized_name}}(); |
114 var tag = decoder.readUint32(); | 116 var tag = decoder.readUint32(); |
115 switch (tag) { | 117 switch (tag) { |
116 {%- for field in union.fields %} | 118 {%- for field in union.fields %} |
117 case {{union.name}}.Tags.{{field.name}}: | 119 case {{union.stylized_name}}.Tags.{{field.stylized_name}}: |
118 {%- if field|is_bool_field %} | 120 {%- if field|is_bool_field %} |
119 result.{{field.name}} = decoder.readUint8() ? true : false; | 121 result.{{field.stylized_name}} = decoder.readUint8() ? true : false; |
120 {%- else %} | 122 {%- else %} |
121 result.{{field.name}} = decoder.{{field.kind|union_decode_snippet}}; | 123 result.{{field.stylized_name}} = |
| 124 decoder.{{field.kind|union_decode_snippet}}; |
122 {%- endif %} | 125 {%- endif %} |
123 break; | 126 break; |
124 {%- endfor %} | 127 {%- endfor %} |
125 } | 128 } |
126 decoder.align(); | 129 decoder.align(); |
127 | 130 |
128 return result; | 131 return result; |
129 }; | 132 }; |
130 {%- endmacro %} | 133 {%- endmacro %} |
131 | 134 |
132 {%- from "validation_macros.tmpl" import validate_union_field %} | 135 {%- from "validation_macros.tmpl" import validate_union_field %} |
133 {%- macro validate(union) %} | 136 {%- macro validate(union) %} |
134 {{union.name}}.validate = function(messageValidator, offset) { | 137 {{union.stylized_name}}.validate = function(messageValidator, offset) { |
135 var size = messageValidator.decodeUnionSize(offset); | 138 var size = messageValidator.decodeUnionSize(offset); |
136 if (size != 16) { | 139 if (size != 16) { |
137 return validator.validationError.INVALID_UNION_SIZE; | 140 return validator.validationError.INVALID_UNION_SIZE; |
138 } | 141 } |
139 | 142 |
140 var tag = messageValidator.decodeUnionTag(offset); | 143 var tag = messageValidator.decodeUnionTag(offset); |
141 var data_offset = offset + 8; | 144 var data_offset = offset + 8; |
142 var err; | 145 var err; |
143 switch (tag) { | 146 switch (tag) { |
144 {%- for field in union.fields %} | 147 {%- for field in union.fields %} |
145 {%- set name = union.name ~ '.' ~ field.name %} | 148 {%- set name = union.stylized_name ~ '.' ~ field.stylized_name %} |
146 case {{union.name}}.Tags.{{field.name}}: | 149 case {{union.stylized_name}}.Tags.{{field.stylized_name}}: |
147 {{validate_union_field(field, "data_offset", name)}} | 150 {{validate_union_field(field, "data_offset", name)}} |
148 break; | 151 break; |
149 {%- endfor %} | 152 {%- endfor %} |
150 } | 153 } |
151 | 154 |
152 return validator.validationError.NONE; | 155 return validator.validationError.NONE; |
153 }; | 156 }; |
154 {%- endmacro %} | 157 {%- endmacro %} |
OLD | NEW |