• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{#--- Begin #}
2  function {{struct.name}}(values) {
3    this.initDefaults_();
4    this.initFields_(values);
5  }
6
7{#--- Enums #}
8{%- from "enum_definition.tmpl" import enum_def %}
9{%  for enum in struct.enums %}
10  {{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}}
11{%-  endfor %}
12
13{#--- Constants #}
14{%  for constant in struct.constants %}
15  {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}};
16{%-  endfor %}
17
18{#--- initDefaults() #}
19  {{struct.name}}.prototype.initDefaults_ = function() {
20{%- for packed_field in struct.packed.packed_fields %}
21    this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
22{%- endfor %}
23  };
24
25{#--- initFields() #}
26  {{struct.name}}.prototype.initFields_ = function(fields) {
27    for(var field in fields) {
28        if (this.hasOwnProperty(field))
29          this[field] = fields[field];
30    }
31  };
32
33{#--- Validation #}
34
35  {{struct.name}}.validate = function(messageValidator, offset) {
36    var err;
37{% macro check_err() -%}
38    if (err !== validator.validationError.NONE)
39        return err;
40{%- endmacro %}
41    err = messageValidator.validateStructHeader(offset, {{struct.name}}.encodedSize, {{struct.packed.packed_fields|length}});
42    {{check_err()}}
43
44{%- for packed_field in struct.packed.packed_fields %}
45{%-   set field_name = packed_field.field.name %}
46{%-   if packed_field.field|is_string_pointer_field %}
47    // validate {{struct.name}}.{{field_name}}
48    err = messageValidator.validateStringPointer({{packed_field|validate_string_params}})
49    {{check_err()}}
50{%-   elif packed_field.field|is_array_pointer_field %}
51    // validate {{struct.name}}.{{field_name}}
52    err = messageValidator.validateArrayPointer({{packed_field|validate_array_params}});
53    {{check_err()}}
54{%-   elif packed_field.field|is_struct_pointer_field %}
55    // validate {{struct.name}}.{{field_name}}
56    err = messageValidator.validateStructPointer({{packed_field|validate_struct_params}});
57    {{check_err()}}
58{%-   elif packed_field.field|is_handle_field %}
59    // validate {{struct.name}}.{{field_name}}
60    err = messageValidator.validateHandle({{packed_field|validate_handle_params}})
61    {{check_err()}}
62{%-   endif %}
63{%- endfor %}
64
65    return validator.validationError.NONE;
66  };
67
68{#--- Encoding and decoding #}
69
70  {{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payload_size}};
71
72  {{struct.name}}.decode = function(decoder) {
73    var packed;
74    var val = new {{struct.name}}();
75    var numberOfBytes = decoder.readUint32();
76    var numberOfFields = decoder.readUint32();
77{%- for byte in struct.bytes %}
78{%-   if byte.packed_fields|length > 1 %}
79    packed = decoder.readUint8();
80{%-     for packed_field in byte.packed_fields %}
81    val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? true : false;
82{%-     endfor %}
83{%-   else %}
84{%-     for packed_field in byte.packed_fields %}
85    val.{{packed_field.field.name}} = decoder.{{packed_field.field.kind|decode_snippet}};
86{%-     endfor %}
87{%-   endif %}
88{%-   if byte.is_padding %}
89    decoder.skip(1);
90{%-   endif %}
91{%- endfor %}
92    return val;
93  };
94
95  {{struct.name}}.encode = function(encoder, val) {
96    var packed;
97    encoder.writeUint32({{struct.name}}.encodedSize);
98    encoder.writeUint32({{struct.packed.packed_fields|length}});
99
100{%- for byte in struct.bytes %}
101{%-   if byte.packed_fields|length > 1 %}
102    packed = 0;
103{%-     for packed_field in byte.packed_fields %}
104    packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}}
105{%-     endfor %}
106    encoder.writeUint8(packed);
107{%-   else %}
108{%-     for packed_field in byte.packed_fields %}
109    encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.name}});
110{%-     endfor %}
111{%-   endif %}
112{%-   if byte.is_padding %}
113    encoder.skip(1);
114{%-   endif %}
115{%- endfor %}
116  };
117