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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
diff --git a/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
index df3ee17e4049a351c3bc56d3df361a58629aa820..d77b28b80eaf9bc739eff75e89cde43aa6e471d0 100644
--- a/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
@@ -1,26 +1,35 @@
{#--- Begin #}
- function {{struct.name}}() {
+ function {{struct.name}}(values) {
this.initDefaults_();
+ this.initFields_(values);
}
{#--- Enums #}
{%- from "enum_definition.tmpl" import enum_def %}
{% for enum in struct.enums %}
{{enum_def("%s.%s"|format(struct.name, enum.name), enum, module)}}
-{% endfor %}
+{%- endfor %}
{#--- Constants #}
{% for constant in struct.constants %}
{{struct.name}}.{{constant.name}} = {{constant.value|expression_to_text}};
-{% endfor %}
+{%- endfor %}
-{#--- Set up defaults #}
+{#--- initDefaults() #}
{{struct.name}}.prototype.initDefaults_ = function() {
{%- for packed_field in struct.packed.packed_fields %}
this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
{%- endfor %}
};
+{#--- initFields() #}
+ {{struct.name}}.prototype.initFields_ = function(fields) {
+ for(var field in fields) {
+ if (this.hasOwnProperty(field))
+ this[field] = fields[field];
+ }
+ };
+
{#--- Validation #}
{{struct.name}}.validate = function(messageValidator, offset) {
« 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