summaryrefslogtreecommitdiff
path: root/mojo/public/tools/bindings/generators/js_templates
diff options
context:
space:
mode:
Diffstat (limited to 'mojo/public/tools/bindings/generators/js_templates')
-rw-r--r--mojo/public/tools/bindings/generators/js_templates/fuzzing.tmpl125
-rw-r--r--mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl12
-rw-r--r--mojo/public/tools/bindings/generators/js_templates/module_definition.tmpl2
-rw-r--r--mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl53
-rw-r--r--mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl67
5 files changed, 257 insertions, 2 deletions
diff --git a/mojo/public/tools/bindings/generators/js_templates/fuzzing.tmpl b/mojo/public/tools/bindings/generators/js_templates/fuzzing.tmpl
new file mode 100644
index 0000000000..fb535fe03a
--- /dev/null
+++ b/mojo/public/tools/bindings/generators/js_templates/fuzzing.tmpl
@@ -0,0 +1,125 @@
+{%- macro get_handle_deps(kind, name) -%}
+{%- if kind|is_struct_kind or kind|is_union_kind -%}
+{{name}}.getHandleDeps()
+{%- elif kind|is_array_kind -%}
+[].concat.apply([], {{name}}.map(function(val) {
+ if (val) {
+ return {{get_handle_deps(kind.kind, 'val')|indent(4)}};
+ }
+ return [];
+}))
+{%- elif kind|is_map_kind -%}
+[].concat.apply([], Array.from({{name}}.values()).map(function(val) {
+ if (val) {
+ return {{get_handle_deps(kind.value_kind, 'val')|indent(4)}};
+ }
+ return [];
+}))
+{%- elif kind|is_any_handle_or_interface_kind -%}
+ ["{{kind|fuzz_handle_name}}"]
+{%- else -%}
+ []
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro set_handles(kind, name) -%}
+{%- if kind|is_struct_kind or kind|is_union_kind -%}
+idx = {{name}}.setHandlesInternal_(handles, idx)
+{%- elif kind|is_array_kind -%}
+{{name}}.forEach(function(val) {
+ {{set_handles(kind.kind, 'val')|indent(2)}};
+})
+{%- elif kind|is_map_kind -%}
+{{name}}.forEach(function(val) {
+ {{set_handles(kind.value_kind, 'val')|indent(2)}};
+})
+{%- elif kind|is_any_handle_or_interface_kind -%}
+{{name}} = handles[idx++];
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro build_call(obj, operation, type, name) -%}
+{%- if name -%}
+{{obj}}.{{operation}}{{type}}({{((name,) + varargs)|join(', ')}})
+{%- else -%}
+{{obj}}.{{operation}}{{type}}({{varargs|join(', ')}})
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate_enum(obj, operation, kind, name) -%}
+{%- if kind.max_value is not none -%}
+{{build_call(obj, operation, 'Enum', name, '0', kind.max_value)}}
+{%- else -%}
+{{build_call(obj, operation, 'Enum', name)}}
+{%- endif %}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate_array(obj, operation, kind, name) -%}
+{%- if operation == 'mutate' -%}
+{{obj}}.{{operation}}Array({{name}}, function(val) {
+ return {{generate_or_mutate(obj, operation, kind.kind, 'val')|indent(2)}};
+})
+{%- else -%}
+{{obj}}.{{operation}}Array(function() {
+ return {{generate_or_mutate(obj, operation, kind.kind)|indent(2)}};
+})
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate_map(obj, operation, kind, name) -%}
+{%- if operation == 'mutate' -%}
+{{obj}}.{{operation}}Map({{name}},
+ function(val) {
+ return {{generate_or_mutate(obj, operation, kind.key_kind, 'val')|indent(4)}};
+ },
+ function(val) {
+ return {{generate_or_mutate(obj, operation, kind.value_kind, 'val')|indent(4)}};
+ })
+{%- else -%}
+{{obj}}.{{operation}}Map(
+ function() {
+ return {{generate_or_mutate(obj, operation, kind.key_kind)|indent(4)}};
+ },
+ function() {
+ return {{generate_or_mutate(obj, operation, kind.value_kind)|indent(4)}};
+ })
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate_primitive(obj, operation, kind, name) -%}
+{%- if kind|is_reference_kind -%}
+{{build_call(obj, operation, kind|primitive_to_fuzz_type, name, kind.is_nullable|to_js_boolean)}}
+{%- else -%}
+{{build_call(obj, operation, kind|primitive_to_fuzz_type, name)}}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate_interface(obj, operation, kind, name) -%}
+{%- if kind|is_interface_request_kind -%}
+{{build_call(obj, operation, 'InterfaceRequest', name, '"' ~ kind.kind.module.namespace ~ '.' ~ kind.kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
+{%- elif kind|is_interface_kind -%}
+{{build_call(obj, operation, 'Interface', name, '"' ~ kind.module.namespace ~ '.' ~ kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
+{%- elif kind|is_associated_interface_request_kind -%}
+{{build_call(obj, operation, 'AssociatedInterfaceRequest', name, '"' ~ kind.kind.module.namespace ~ '.' ~ kind.kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
+{%- elif kind|is_associated_interface_kind -%}
+{{build_call(obj, operation, 'AssociatedInterface', name, '"' ~ kind.kind.module.namespace ~ '.' ~ kind.kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro generate_or_mutate(obj, operation, kind, name='') -%}
+{%- if kind|is_primitive_kind -%}
+{{generate_or_mutate_primitive(obj, operation, kind, name)}}
+{%- elif kind|is_any_interface_kind -%}
+{{generate_or_mutate_interface(obj, operation, kind, name)}}
+{%- elif kind|is_enum_kind -%}
+{{generate_or_mutate_enum(obj, operation, kind, name)}}
+{%- elif kind|is_struct_kind -%}
+{{build_call(obj, operation, 'Struct', kind.module.namespace ~ '.' ~ kind.name, kind.is_nullable|to_js_boolean)}}
+{%- elif kind|is_union_kind -%}
+{{build_call(obj, operation, 'Union', kind.module.namespace ~ '.' ~ kind.name, kind.is_nullable|to_js_boolean)}}
+{%- elif kind|is_array_kind -%}
+{{generate_or_mutate_array(obj, operation, kind, name)}}
+{%- elif kind|is_map_kind -%}
+{{generate_or_mutate_map(obj, operation, kind, name)}}
+{%- endif -%}
+{%- endmacro -%}
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 988ca0af4d..356fc5bb12 100644
--- a/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl
@@ -226,6 +226,18 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{%- else %}
validateResponse: null,
{%- endif %}
+{%- if generate_fuzzing %}
+ mojomId: '{{module.path}}',
+ fuzzMethods: {
+ {%- for method in interface.methods %}
+ {%- set interface_method_id =
+ interface.mojom_name ~ "_" ~ method.mojom_name %}
+ {{ method.name }}: {
+ params: {{interface_method_id}}_Params,
+ },
+ {%- endfor %}
+ },
+{%- endif %}
};
{#--- Interface Constants #}
{%- for constant in interface.constants %}
diff --git a/mojo/public/tools/bindings/generators/js_templates/module_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/module_definition.tmpl
index 21165ddcad..a6736b5658 100644
--- a/mojo/public/tools/bindings/generators/js_templates/module_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/module_definition.tmpl
@@ -17,7 +17,7 @@
{#--- Union definitions #}
{%- from "union_definition.tmpl" import union_def %}
{%- for union in unions %}
-{{union_def(union)|indent(2)}}
+{{union_def(union, generate_fuzzing)|indent(2)}}
{%- endfor %}
{#--- Interface definitions #}
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 7bffe60885..176f1b8909 100644
--- a/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/struct_definition.tmpl
@@ -30,6 +30,59 @@
}
};
+{#--- Fuzzing #}
+{%- if generate_fuzzing %}
+ {{struct.name}}.generate = function(generator_) {
+ var generated = new {{struct.name}};
+{%- for field in struct.fields %}
+{%- if not field.kind|is_any_handle_or_interface_kind %}
+{%- from "fuzzing.tmpl" import generate_or_mutate %}
+ generated.{{field.name}} = {{generate_or_mutate('generator_', 'generate', field.kind)|indent(4)}};
+{%- endif %}
+{%- endfor %}
+ return generated;
+ };
+
+ {{struct.name}}.prototype.mutate = function(mutator_) {
+{%- for field in struct.fields %}
+{%- if not field.kind|is_any_handle_or_interface_kind %}
+ if (mutator_.chooseMutateField()) {
+{%- from "fuzzing.tmpl" import generate_or_mutate %}
+ this.{{field.name}} = {{generate_or_mutate('mutator_', 'mutate', field.kind, 'this.' ~ field.name)|indent(6)}};
+ }
+{%- endif %}
+{%- endfor %}
+ return this;
+ };
+
+ {{struct.name}}.prototype.getHandleDeps = function() {
+ var handles = [];
+{%- for field in struct.fields %}
+{%- if field.kind|contains_handles_or_interfaces %}
+ if (this.{{field.name}} !== null) {
+{%- from "fuzzing.tmpl" import get_handle_deps %}
+ Array.prototype.push.apply(handles, {{get_handle_deps(field.kind, 'this.' ~ field.name)|indent(6)}});
+ }
+{%- endif %}
+{%- endfor %}
+ return handles;
+ };
+
+ {{struct.name}}.prototype.setHandles = function() {
+ this.setHandlesInternal_(arguments, 0);
+ };
+
+ {{struct.name}}.prototype.setHandlesInternal_ = function(handles, idx) {
+{%- for field in struct.fields %}
+{%- if field.kind|contains_handles_or_interfaces %}
+{%- from "fuzzing.tmpl" import set_handles %}
+ {{set_handles(field.kind, 'this.' ~ field.name)|indent(4)}};
+{%- endif %}
+{%- endfor %}
+ return idx;
+ };
+{%- endif %}
+
{#--- Validation #}
{{struct.name}}.validate = function(messageValidator, offset) {
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 dde69aeffd..d00e641db6 100644
--- a/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/union_definition.tmpl
@@ -1,4 +1,4 @@
-{%- macro union_def(union) %}
+{%- macro union_def(union, generate_fuzzing=false) %}
function {{union.name}}(value) {
this.initDefault_();
this.initValue_(value);
@@ -39,6 +39,71 @@ function {{union.name}}(value) {
this[keys[0]] = value[keys[0]];
}
+{% if generate_fuzzing %}
+{{union.name}}.generate = function(generator_) {
+ var generated = new {{union.name}};
+ var generators = [
+{%- for field in union.fields %}
+ {
+ field: "{{field.name}}",
+
+{%- if not field.kind|is_any_handle_or_interface_kind %}
+{%- from "fuzzing.tmpl" import generate_or_mutate %}
+ generator: function() { return {{generate_or_mutate('generator_', 'generate', field.kind)|indent(6)}}; },
+{%- endif %}
+ },
+{%- endfor %}
+ ];
+
+ var result = generator_.generateUnionField(generators);
+ generated[result.field] = result.value;
+ return generated;
+}
+
+{{union.name}}.prototype.mutate = function(mutator_) {
+ var mutators = [
+{%- for field in union.fields %}
+ {
+ field: "{{field.name}}",
+
+{%- if not field.kind|is_any_handle_or_interface_kind %}
+{%- from "fuzzing.tmpl" import generate_or_mutate %}
+ mutator: function() { return {{generate_or_mutate('mutator_', 'mutate', field.kind, 'this.' ~ field.name)|indent(6)}}; },
+{%- endif %}
+ },
+{%- endfor %}
+ ];
+
+ var result = mutator_.mutateUnionField(this, mutators);
+ generated[result.field] = result.value;
+ return this;
+}
+
+{{union.name}}.prototype.getHandleDeps = function() {
+{%- for field in union.fields %}
+{%- if field.kind|contains_handles_or_interfaces %}
+ if (this.$tag == {{union.name}}.Tags.{{field.name}}) {
+{%- from "fuzzing.tmpl" import get_handle_deps %}
+ return {{get_handle_deps(field.kind, 'this.' ~ field.name)}};
+ }
+{%- endif %}
+{%- endfor %}
+ return [];
+}
+
+{{union.name}}.prototype.setHandles = function() {
+{%- for field in union.fields %}
+{%- if field.kind|contains_handles_or_interfaces %}
+ if (this.$tag == {{union.name}}.Tags.{{field.name}}) {
+{%- from "fuzzing.tmpl" import set_handles %}
+ return {{set_handles(field.kind, 'this.' ~ field.name)}};
+ }
+{%- endif %}
+{%- endfor %}
+ return [];
+}
+{%- endif %}
+
{%- for field in union.fields %}
Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
get: function() {