Mojo JavaScript bindings: follow JavaScript naming rules.
This patch makes sure the following names follows the JavaScript naming rules in the use_new_js_bindings mode:
- method names;
- parameter names;
- struct/union field names;
- namespaces.
BUG=718614
Review-Url: https://codereview.chromium.org/2889723002
Cr-Commit-Position: refs/heads/master@{#472260}
CrOS-Libchrome-Original-Commit: 40b5e68dcd6a93d50931898b2d180195966adac3
diff --git a/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl
index afee2ba..5f50257 100644
--- a/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl
@@ -1,5 +1,7 @@
{%- for method in interface.methods %}
- var k{{interface.name}}_{{method.name}}_Name = {{method.ordinal}};
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
+ var k{{interface_method_id}}_Name = {{method.ordinal}};
{%- endfor %}
function {{interface.name}}Ptr(handleOrPtrInfo) {
@@ -22,17 +24,19 @@
}
{%- for method in interface.methods %}
- {{interface.name}}Ptr.prototype.{{method.name|stylize_method}} = function() {
- return {{interface.name}}Proxy.prototype.{{method.name|stylize_method}}
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
+ {{interface.name}}Ptr.prototype.{{method.name}} = function() {
+ return {{interface.name}}Proxy.prototype.{{method.name}}
.apply(this.ptr.getProxy(), arguments);
};
- {{interface.name}}Proxy.prototype.{{method.name|stylize_method}} = function(
+ {{interface.name}}Proxy.prototype.{{method.name}} = function(
{%- for parameter in method.parameters -%}
{{parameter.name}}{% if not loop.last %}, {% endif %}
{%- endfor -%}
) {
- var params = new {{interface.name}}_{{method.name}}_Params();
+ var params = new {{interface_method_id}}_Params();
{%- for parameter in method.parameters %}
params.{{parameter.name}} = {{parameter.name}};
{%- endfor %}
@@ -40,14 +44,14 @@
{%- if method.response_parameters == None %}
{%- if method|method_passes_associated_kinds and not use_new_js_bindings %}
var builder = new codec.MessageV2Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_Params.encodedSize));
- builder.setPayload({{interface.name}}_{{method.name}}_Params, params);
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_Params.encodedSize));
+ builder.setPayload({{interface_method_id}}_Params, params);
{%- else %}
var builder = new codec.MessageV0Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_Params.encodedSize));
- builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params);
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_Params.encodedSize));
+ builder.encodeStruct({{interface_method_id}}_Params, params);
{%- endif %}
var message = builder.finish();
this.receiver_.accept(message);
@@ -55,22 +59,22 @@
return new Promise(function(resolve, reject) {
{%- if method|method_passes_associated_kinds and not use_new_js_bindings %}
var builder = new codec.MessageV2Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_Params.encodedSize),
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_Params.encodedSize),
codec.kMessageExpectsResponse, 0);
- builder.setPayload({{interface.name}}_{{method.name}}_Params, params);
+ builder.setPayload({{interface_method_id}}_Params, params);
{%- else %}
var builder = new codec.MessageV1Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_Params.encodedSize),
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_Params.encodedSize),
codec.kMessageExpectsResponse, 0);
- builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params);
+ builder.encodeStruct({{interface_method_id}}_Params, params);
{%- endif %}
var message = builder.finish();
this.receiver_.acceptAndExpectResponse(message).then(function(message) {
var reader = new codec.MessageReader(message);
var responseParams =
- reader.decodeStruct({{interface.name}}_{{method.name}}_ResponseParams);
+ reader.decodeStruct({{interface_method_id}}_ResponseParams);
resolve(responseParams);
}).catch(function(result) {
reject(Error("Connection error: " + result));
@@ -85,9 +89,8 @@
}
{%- for method in interface.methods %}
-{%- set js_method_name = method.name|stylize_method %}
- {{interface.name}}Stub.prototype.{{js_method_name}} = function({{method.parameters|map(attribute='name')|join(', ')}}) {
- return this.delegate_ && this.delegate_.{{js_method_name}} && this.delegate_.{{js_method_name}}({{method.parameters|map(attribute='name')|join(', ')}});
+ {{interface.name}}Stub.prototype.{{method.name}} = function({{method.parameters|map(attribute='name')|join(', ')}}) {
+ return this.delegate_ && this.delegate_.{{method.name}} && this.delegate_.{{method.name}}({{method.parameters|map(attribute='name')|join(', ')}});
}
{%- endfor %}
@@ -95,10 +98,12 @@
var reader = new codec.MessageReader(message);
switch (reader.messageName) {
{%- for method in interface.methods %}
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
{%- if method.response_parameters == None %}
- case k{{interface.name}}_{{method.name}}_Name:
- var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params);
- this.{{method.name|stylize_method}}(
+ case k{{interface_method_id}}_Name:
+ var params = reader.decodeStruct({{interface_method_id}}_Params);
+ this.{{method.name}}(
{%- for parameter in method.parameters -%}
params.{{parameter.name}}{% if not loop.last %}, {% endif %}
{%- endfor %});
@@ -115,32 +120,34 @@
var reader = new codec.MessageReader(message);
switch (reader.messageName) {
{%- for method in interface.methods %}
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
{%- if method.response_parameters != None %}
- case k{{interface.name}}_{{method.name}}_Name:
- var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params);
- this.{{method.name|stylize_method}}(
+ case k{{interface_method_id}}_Name:
+ var params = reader.decodeStruct({{interface_method_id}}_Params);
+ this.{{method.name}}(
{%- for parameter in method.parameters -%}
params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{%- endfor %}).then(function(response) {
var responseParams =
- new {{interface.name}}_{{method.name}}_ResponseParams();
+ new {{interface_method_id}}_ResponseParams();
{%- for parameter in method.response_parameters %}
responseParams.{{parameter.name}} = response.{{parameter.name}};
{%- endfor %}
{%- if method|method_passes_associated_kinds and not use_new_js_bindings %}
var builder = new codec.MessageV2Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_ResponseParams
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_ResponseParams
.encodedSize),
codec.kMessageIsResponse, reader.requestID);
- builder.setPayload({{interface.name}}_{{method.name}}_ResponseParams,
+ builder.setPayload({{interface_method_id}}_ResponseParams,
responseParams);
{%- else %}
var builder = new codec.MessageV1Builder(
- k{{interface.name}}_{{method.name}}_Name,
- codec.align({{interface.name}}_{{method.name}}_ResponseParams.encodedSize),
+ k{{interface_method_id}}_Name,
+ codec.align({{interface_method_id}}_ResponseParams.encodedSize),
codec.kMessageIsResponse, reader.requestID);
- builder.encodeStruct({{interface.name}}_{{method.name}}_ResponseParams,
+ builder.encodeStruct({{interface_method_id}}_ResponseParams,
responseParams);
{%- endif %}
var message = builder.finish();
@@ -164,13 +171,15 @@
var paramsClass = null;
switch (message.getName()) {
{%- for method in interface.methods %}
- case k{{interface.name}}_{{method.name}}_Name:
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
+ case k{{interface_method_id}}_Name:
{%- if method.response_parameters == None %}
if (!message.expectsResponse() && !message.isResponse())
- paramsClass = {{interface.name}}_{{method.name}}_Params;
+ paramsClass = {{interface_method_id}}_Params;
{%- else %}
if (message.expectsResponse())
- paramsClass = {{interface.name}}_{{method.name}}_Params;
+ paramsClass = {{interface_method_id}}_Params;
{%- endif %}
break;
{%- endfor %}
@@ -189,10 +198,12 @@
var paramsClass = null;
switch (message.getName()) {
{%- for method in interface.methods %}
+{%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
{%- if method.response_parameters != None %}
- case k{{interface.name}}_{{method.name}}_Name:
+ case k{{interface_method_id}}_Name:
if (message.isResponse())
- paramsClass = {{interface.name}}_{{method.name}}_ResponseParams;
+ paramsClass = {{interface_method_id}}_ResponseParams;
break;
{%- endif %}
{%- endfor %}
@@ -204,7 +215,7 @@
}
var {{interface.name}} = {
- name: '{{module.namespace|replace(".","::")}}::{{interface.name}}',
+ name: '{{module.mojom_namespace|replace(".","::")}}::{{interface.mojom_name}}',
kVersion: {{interface.version}},
ptrClass: {{interface.name}}Ptr,
proxyClass: {{interface.name}}Proxy,
diff --git a/mojo/public/tools/bindings/generators/mojom_js_generator.py b/mojo/public/tools/bindings/generators/mojom_js_generator.py
index 13a9c39..a6e3134 100644
--- a/mojo/public/tools/bindings/generators/mojom_js_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_js_generator.py
@@ -92,6 +92,30 @@
return os.path.relpath(module.path, os.path.dirname(base_module.path))
+class JavaScriptStylizer(generator.Stylizer):
+ def __init__(self, use_new_js_bindings):
+ self.use_new_js_bindings = use_new_js_bindings
+
+ def StylizeField(self, mojom_name):
+ if not self.use_new_js_bindings:
+ return mojom_name
+ return generator.ToCamel(mojom_name, lower_initial=True)
+
+ def StylizeParameter(self, mojom_name):
+ if not self.use_new_js_bindings:
+ return mojom_name
+ return generator.ToCamel(mojom_name, lower_initial=True)
+
+ def StylizeMethod(self, mojom_name):
+ return generator.ToCamel(mojom_name, lower_initial=True)
+
+ def StylizeModule(self, mojom_namespace):
+ if not self.use_new_js_bindings:
+ return mojom_namespace
+ return '.'.join(generator.ToCamel(word, lower_initial=True)
+ for word in mojom_namespace.split('.'))
+
+
class Generator(generator.Generator):
def _GetParameters(self):
return {
@@ -136,7 +160,6 @@
"js_type": self._JavaScriptType,
"method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds,
"payload_size": JavaScriptPayloadSize,
- "stylize_method": lambda x: generator.ToCamel(x, lower_initial=True),
"to_camel": generator.ToCamel,
"union_decode_snippet": self._JavaScriptUnionDecodeSnippet,
"union_encode_snippet": self._JavaScriptUnionEncodeSnippet,
@@ -157,8 +180,7 @@
if self.variant:
raise Exception("Variants not supported in JavaScript bindings.")
- # TODO(yzshen): Add a JavaScriptStylizer.
- self.module.Stylize(generator.Stylizer())
+ self.module.Stylize(JavaScriptStylizer(self.use_new_js_bindings))
# TODO(yzshen): Remove this method once the old JS bindings go away.
self._SetUniqueNameForImports()