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