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

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

Issue 595063002: Use a JS dictionary to initialize a the fields in a Mojo struct (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed console module test dependency Created 6 years, 3 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
« no previous file with comments | « mojo/public/js/bindings/tests/run_js_tests.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 {#--- Begin #} 1 {#--- Begin #}
2 function {{struct.name}}() { 2 function {{struct.name}}(values) {
3 this.initDefaults_(); 3 this.initDefaults_();
4 this.initFields_(values);
4 } 5 }
5 6
6 {#--- Enums #} 7 {#--- Enums #}
7 {%- from "enum_definition.tmpl" import enum_def %} 8 {%- from "enum_definition.tmpl" import enum_def %}
8 {% for enum in struct.enums %} 9 {% for enum in struct.enums %}
9 {{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}} 10 {{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}}
10 {% endfor %} 11 {%- endfor %}
11 12
12 {#--- Constants #} 13 {#--- Constants #}
13 {% for constant in struct.constants %} 14 {% for constant in struct.constants %}
14 {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}}; 15 {{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}};
15 {% endfor %} 16 {%- endfor %}
16 17
17 {#--- Set up defaults #} 18 {#--- initDefaults() #}
18 {{struct.name}}.prototype.initDefaults_ = function() { 19 {{struct.name}}.prototype.initDefaults_ = function() {
19 {%- for packed_field in struct.packed.packed_fields %} 20 {%- for packed_field in struct.packed.packed_fields %}
20 this.{{packed_field.field.name}} = {{packed_field.field|default_value}}; 21 this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
21 {%- endfor %} 22 {%- endfor %}
22 }; 23 };
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
24 {#--- Validation #} 33 {#--- Validation #}
25 34
26 {{struct.name}}.validate = function(messageValidator, offset) { 35 {{struct.name}}.validate = function(messageValidator, offset) {
27 var err; 36 var err;
28 {% macro check_err() -%} 37 {% macro check_err() -%}
29 if (err !== validator.validationError.NONE) 38 if (err !== validator.validationError.NONE)
30 return err; 39 return err;
31 {%- endmacro %} 40 {%- endmacro %}
32 err = messageValidator.validateStructHeader(offset, {{struct.name}}.encodedS ize, {{struct.packed.packed_fields|length}}); 41 err = messageValidator.validateStructHeader(offset, {{struct.name}}.encodedS ize, {{struct.packed.packed_fields|length}});
33 {{check_err()}} 42 {{check_err()}}
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 {%- else %} 107 {%- else %}
99 {%- for packed_field in byte.packed_fields %} 108 {%- for packed_field in byte.packed_fields %}
100 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n ame}}); 109 encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.n ame}});
101 {%- endfor %} 110 {%- endfor %}
102 {%- endif %} 111 {%- endif %}
103 {%- if byte.is_padding %} 112 {%- if byte.is_padding %}
104 encoder.skip(1); 113 encoder.skip(1);
105 {%- endif %} 114 {%- endif %}
106 {%- endfor %} 115 {%- endfor %}
107 }; 116 };
OLDNEW
« no previous file with comments | « mojo/public/js/bindings/tests/run_js_tests.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698