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

Side by Side Diff: mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl

Issue 468713002: JavaScript bindings for Mojo message validation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Skip unexpected null tests" 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 {#--- Begin #} 1 {#--- Begin #}
2 function {{struct.name}}() { 2 function {{struct.name}}() {
3 this.initDefaults_(); 3 this.initDefaults_();
4 } 4 }
5 5
6 {#--- Enums #} 6 {#--- Enums #}
7 {%- from "enum_definition.tmpl" import enum_def %} 7 {%- from "enum_definition.tmpl" import enum_def %}
8 {% for enum in struct.enums %} 8 {% for enum in struct.enums %}
9 {{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}} 9 {{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}}
10 {% endfor %} 10 {% endfor %}
11 11
12 {#--- Constants #} 12 {#--- Constants #}
13 {% for constant in struct.constants %} 13 {% for constant in struct.constants %}
14 {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}}; 14 {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}};
15 {% endfor %} 15 {% endfor %}
16 16
17 {#--- Set up defaults #} 17 {#--- Set up defaults #}
18 {{struct.name}}.prototype.initDefaults_ = function() { 18 {{struct.name}}.prototype.initDefaults_ = function() {
19 {%- for packed_field in struct.packed.packed_fields %} 19 {%- for packed_field in struct.packed.packed_fields %}
20 this.{{packed_field.field.name}} = {{packed_field.field|default_value}}; 20 this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
21 {%- endfor %} 21 {%- endfor %}
22 }; 22 };
23 23
24 {#--- Validation #}
25
26 {{struct.name}}.validate = function(messageValidator, offset) {
27 var err;
28
29 err = messageValidator.validateStructHeader(offset, {{struct.name}}.encodedS ize, {{struct.packed.packed_fields|length}});
30 if (err !== validator.validationError.NONE)
31 return err;
32 {%- for packed_field in struct.packed.packed_fields %}
33 {%- set field_name = packed_field.field.name %}
34 {%- if packed_field.field|is_string_pointer_field %}
35 // validate {{struct.name}}.{{field_name}}
36 err = messageValidator.validateStringPointer({{packed_field|field_offset}})
37 {%- elif packed_field.field|is_array_pointer_field %}
38 // validate {{struct.name}}.{{field_name}}
39 err = messageValidator.validateArrayPointer({{packed_field|validate_array_pa rams}});
40 {%- elif packed_field.field|is_struct_pointer_field %}
41 // validate {{struct.name}}.{{field_name}}
42 err = messageValidator.validateStructPointer({{packed_field|validate_struct_ params}});
43 {%- elif packed_field.field|is_handle_field %}
44 // validate {{struct.name}}.{{field_name}}
45 err = messageValidator.validateHandle({{packed_field|field_offset}})
46 {%- endif %}
47 if (err !== validator.validationError.NONE)
48 return err;
49 {%- endfor %}
50
51 return validator.validationError.NONE;
52 };
53
24 {#--- Encoding and decoding #} 54 {#--- Encoding and decoding #}
25 55
26 {{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payloa d_size}}; 56 {{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payloa d_size}};
27 57
28 {{struct.name}}.decode = function(decoder) { 58 {{struct.name}}.decode = function(decoder) {
29 var packed; 59 var packed;
30 var val = new {{struct.name}}(); 60 var val = new {{struct.name}}();
31 var numberOfBytes = decoder.readUint32(); 61 var numberOfBytes = decoder.readUint32();
32 var numberOfFields = decoder.readUint32(); 62 var numberOfFields = decoder.readUint32();
33 {%- for byte in struct.bytes %} 63 {%- for byte in struct.bytes %}
(...skipping 29 matching lines...) Expand all
63 {%- else %} 93 {%- else %}
64 {%- for packed_field in byte.packed_fields %} 94 {%- for packed_field in byte.packed_fields %}
65 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n ame}}); 95 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n ame}});
66 {%- endfor %} 96 {%- endfor %}
67 {%- endif %} 97 {%- endif %}
68 {%- if byte.is_padding %} 98 {%- if byte.is_padding %}
69 encoder.skip(1); 99 encoder.skip(1);
70 {%- endif %} 100 {%- endif %}
71 {%- endfor %} 101 {%- endfor %}
72 }; 102 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698