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

Unified Diff: mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl

Issue 2855263002: Mojo bindings: support generating identifers using different style rules for different target langu…
Patch Set: . Created 3 years, 8 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
Index: mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
diff --git a/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
index 4823febeca9f20b1ffcfe685962e12ab8f28a570..c717d244ede5a5835b5163abeb0c9be5186aa96b 100644
--- a/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
@@ -1,17 +1,17 @@
{%- macro union_def(union) %}
-function {{union.name}}(value) {
+function {{union.stylized_name}}(value) {
this.initDefault_();
this.initValue_(value);
}
{{tags(union)}}
-{{union.name}}.prototype.initDefault_ = function() {
+{{union.stylized_name}}.prototype.initDefault_ = function() {
this.$data = null;
this.$tag = undefined;
}
-{{union.name}}.prototype.initValue_ = function(value) {
+{{union.stylized_name}}.prototype.initValue_ = function(value) {
if (value == undefined) {
return;
}
@@ -27,12 +27,13 @@ function {{union.name}}(value) {
var fields = [
{%- for field in union.fields %}
- "{{field.name}}",
+ "{{field.stylized_name}}",
{%- endfor %}
];
if (fields.indexOf(keys[0]) < 0) {
- throw new ReferenceError(keys[0] + " is not a {{union.name}} member.");
+ throw new ReferenceError(
+ keys[0] + " is not a {{union.stylized_name}} member.");
}
@@ -40,17 +41,18 @@ function {{union.name}}(value) {
}
{%- for field in union.fields %}
-Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
+Object.defineProperty({{union.stylized_name}}.prototype,
+ "{{field.stylized_name}}", {
get: function() {
- if (this.$tag != {{union.name}}.Tags.{{field.name}}) {
+ if (this.$tag != {{union.stylized_name}}.Tags.{{field.stylized_name}}) {
throw new ReferenceError(
- "{{union.name}}.{{field.name}} is not currently set.");
+ "{{union.stylized_name}}.{{field.stylized_name}} is not currently set.");
}
return this.$data;
},
set: function(value) {
- this.$tag = {{union.name}}.Tags.{{field.name}};
+ this.$tag = {{union.stylized_name}}.Tags.{{field.stylized_name}};
this.$data = value;
}
});
@@ -62,19 +64,19 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
{{validate(union)|indent(2)}}
-{{union.name}}.encodedSize = 16;
+{{union.stylized_name}}.encodedSize = 16;
{%- endmacro %}
{%- macro tags(union) %}
-{{union.name}}.Tags = {
+{{union.stylized_name}}.Tags = {
{%- for field in union.fields %}
- {{field.name}}: {{field.ordinal}},
+ {{field.stylized_name}}: {{field.ordinal}},
{%- endfor %}
};
{%- endmacro %}
{%- macro encode(union) %}
-{{union.name}}.encode = function(encoder, val) {
+{{union.stylized_name}}.encode = function(encoder, val) {
if (val == null) {
encoder.writeUint64(0);
encoder.writeUint64(0);
@@ -88,11 +90,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
encoder.writeUint32(val.$tag);
switch (val.$tag) {
{%- for field in union.fields %}
- case {{union.name}}.Tags.{{field.name}}:
+ case {{union.stylized_name}}.Tags.{{field.stylized_name}}:
{%- if field|is_bool_field %}
- encoder.writeUint8(val.{{field.name}} ? 1 : 0);
+ encoder.writeUint8(val.{{field.stylized_name}} ? 1 : 0);
{%- else %}
- encoder.{{field.kind|union_encode_snippet}}val.{{field.name}});
+ encoder.{{field.kind|union_encode_snippet}}val.{{field.stylized_name}});
{%- endif %}
break;
{%- endfor %}
@@ -102,7 +104,7 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
{%- endmacro %}
{%- macro decode(union) %}
-{{union.name}}.decode = function(decoder) {
+{{union.stylized_name}}.decode = function(decoder) {
var size = decoder.readUint32();
if (size == 0) {
decoder.readUint32();
@@ -110,15 +112,16 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
return null;
}
- var result = new {{union.name}}();
+ var result = new {{union.stylized_name}}();
var tag = decoder.readUint32();
switch (tag) {
{%- for field in union.fields %}
- case {{union.name}}.Tags.{{field.name}}:
+ case {{union.stylized_name}}.Tags.{{field.stylized_name}}:
{%- if field|is_bool_field %}
- result.{{field.name}} = decoder.readUint8() ? true : false;
+ result.{{field.stylized_name}} = decoder.readUint8() ? true : false;
{%- else %}
- result.{{field.name}} = decoder.{{field.kind|union_decode_snippet}};
+ result.{{field.stylized_name}} =
+ decoder.{{field.kind|union_decode_snippet}};
{%- endif %}
break;
{%- endfor %}
@@ -131,7 +134,7 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
{%- from "validation_macros.tmpl" import validate_union_field %}
{%- macro validate(union) %}
-{{union.name}}.validate = function(messageValidator, offset) {
+{{union.stylized_name}}.validate = function(messageValidator, offset) {
var size = messageValidator.decodeUnionSize(offset);
if (size != 16) {
return validator.validationError.INVALID_UNION_SIZE;
@@ -142,8 +145,8 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
var err;
switch (tag) {
{%- for field in union.fields %}
-{%- set name = union.name ~ '.' ~ field.name %}
- case {{union.name}}.Tags.{{field.name}}:
+{%- set name = union.stylized_name ~ '.' ~ field.stylized_name %}
+ case {{union.stylized_name}}.Tags.{{field.stylized_name}}:
{{validate_union_field(field, "data_offset", name)}}
break;
{%- endfor %}

Powered by Google App Engine
This is Rietveld 408576698