{%- 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_pending_receiver_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_pending_remote_kind -%}
{{build_call(obj, operation, 'Interface', name, '"' ~ kind.kind.module.namespace ~ '.' ~ kind.kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
{%- elif kind|is_pending_associated_receiver_kind -%}
{{build_call(obj, operation, 'AssociatedInterfaceRequest', name, '"' ~ kind.kind.module.namespace ~ '.' ~ kind.kind.name ~ '"', kind.is_nullable|to_js_boolean)}}
{%- elif kind|is_pending_associated_remote_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', name, kind.module.namespace ~ '.' ~ kind.name, kind.is_nullable|to_js_boolean)}}
{%- elif kind|is_union_kind -%}
{{build_call(obj, operation, 'Union', name, 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 -%}