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

Unified Diff: mojo/public/tools/bindings/generators/mojom_js_generator.py

Issue 468713002: JavaScript bindings for Mojo message validation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Binding generator simplification for Arrays 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_js_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_js_generator.py b/mojo/public/tools/bindings/generators/mojom_js_generator.py
index 5b83974d97943ec15a117e877cd978bef32a1c73..51b1012a53b703c6fe46eda843f54f601cc9b622 100644
--- a/mojo/public/tools/bindings/generators/mojom_js_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_js_generator.py
@@ -97,9 +97,9 @@ def CodecType(kind):
return _kind_to_codec_type[kind]
if mojom.IsStructKind(kind):
return "new codec.PointerTo(%s)" % CodecType(kind.name)
- if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
- return "new codec.ArrayOf(new codec.ArrayOf(codec.PackedBool))"
- if mojom.IsArrayKind(kind):
+ if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind):
+ return "new codec.ArrayOf(codec.PackedBool)"
+ if mojom.IsAnyArrayKind(kind):
return "new codec.ArrayOf(%s)" % CodecType(kind.kind)
if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
return CodecType(mojom.MSGPIPE)
@@ -113,9 +113,9 @@ def JavaScriptDecodeSnippet(kind):
return "decodeStruct(%s)" % CodecType(kind)
if mojom.IsStructKind(kind):
return "decodeStructPointer(%s)" % CodecType(kind.name)
- if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
- return "decodeArrayPointer(new codec.ArrayOf(codec.PackedBool))"
- if mojom.IsArrayKind(kind):
+ if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind):
+ return "decodeArrayPointer(codec.PackedBool)"
+ if mojom.IsAnyArrayKind(kind):
return "decodeArrayPointer(%s)" % CodecType(kind.kind)
if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
return JavaScriptDecodeSnippet(mojom.MSGPIPE)
@@ -128,8 +128,8 @@ def JavaScriptEncodeSnippet(kind):
return "encodeStruct(%s, " % CodecType(kind)
if mojom.IsStructKind(kind):
return "encodeStructPointer(%s, " % CodecType(kind.name)
- if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
- return "encodeArrayPointer(new codec.ArrayOf(codec.PackedBool), ";
+ if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind):
+ return "encodeArrayPointer(codec.PackedBool, ";
if mojom.IsAnyArrayKind(kind):
return "encodeArrayPointer(%s, " % CodecType(kind.kind)
if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
@@ -138,6 +138,22 @@ def JavaScriptEncodeSnippet(kind):
return JavaScriptEncodeSnippet(mojom.INT32)
+def JavaScriptFieldOffset(packed_field):
+ return "offset + codec.kStructHeaderSize + %s" % packed_field.offset
+
+def JavaScriptValidateArrayParams(pf):
+ elementKind = pf.field.kind.kind
+ elementSize = pack.PackedField.GetSizeForKind(elementKind)
+ elementCount = generator.ExpectedArraySize(pf.field.kind)
+ elementType = "codec.PackedBool" if mojom.IsBoolKind(elementKind) \
+ else CodecType(elementKind)
+ return "%s, %s, %s, %s" % \
+ (JavaScriptFieldOffset(pf), elementSize, elementCount, elementType)
+
+def JavaScriptValidateStructParams(pf):
+ return "%s, %s" % (JavaScriptFieldOffset(pf), pf.field.kind.name)
+
+
def TranslateConstants(token):
if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
# Both variable and enum constants are constructed like:
@@ -164,6 +180,25 @@ def JavascriptType(kind):
return kind.name
+def HasCallbacks(interface):
yzshen1 2014/08/14 18:02:22 Shall we move this method into generator.py so tha
hansmuller 2014/08/14 23:51:46 Yes. I moved it into module.py, since the Interfac
+ for method in interface.methods:
+ if method.response_parameters != None:
+ return True
+ return False
+
+def IsArrayPointerField(field):
yzshen1 2014/08/14 18:02:22 [optional] If you want, you could consider directl
hansmuller 2014/08/14 23:51:46 That's a reasonable suggestion however if you don'
yzshen1 2014/08/15 16:26:36 Acknowledged.
+ return mojom.IsAnyArrayKind(field.kind)
+
+def IsStringPointerField(field):
+ return mojom.IsStringKind(field.kind)
+
+def IsStructPointerField(field):
+ return mojom.IsStructKind(field.kind)
+
+def IsHandleField(field):
+ return mojom.IsAnyHandleKind(field.kind)
+
+
class Generator(generator.Generator):
js_filters = {
@@ -172,8 +207,16 @@ class Generator(generator.Generator):
"decode_snippet": JavaScriptDecodeSnippet,
"encode_snippet": JavaScriptEncodeSnippet,
"expression_to_text": ExpressionToText,
+ "field_offset": JavaScriptFieldOffset,
+ "has_callbacks": HasCallbacks,
+ "is_array_pointer_field": IsArrayPointerField,
+ "is_struct_pointer_field": IsStructPointerField,
+ "is_string_pointer_field": IsStringPointerField,
+ "is_handle_field": IsHandleField,
"js_type": JavascriptType,
"stylize_method": generator.StudlyCapsToCamel,
+ "validate_array_params": JavaScriptValidateArrayParams,
+ "validate_struct_params": JavaScriptValidateStructParams,
}
@UseJinja("js_templates/module.js.tmpl", filters=js_filters)

Powered by Google App Engine
This is Rietveld 408576698