{{ self::kdoc_block(doc, "") }}{%- if throws %}
@Throws({{ err_type }}::class)
{%- endif %}
fun {{ method_name }}({% for param in signature_params %}{{ param.name }}: {{ param.kotlin_type }}{% if !loop.last %}, {% endif %}{% endfor %}){% if let Some(ret) = return_type %}: {{ ret }}{% endif %} {
{%- if wire_writers.is_empty() %}
{%- if return_is_unit %}
Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %})
{%- elif return_is_direct %}
return Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %}){{ return_cast }}
{%- else %}
val buf = Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %})
?: throw FfiException(-1, "Null buffer returned")
{%- if is_blittable_return %}
return useWireBytes(buf) { buffer ->
{{ decode_expr }}
}
{%- else %}
val reader = WireReader(buf)
return {{ decode_expr }}
{%- endif %}
{%- endif %}
{%- else %}
{%- for binding in wire_writers %}
val {{ binding.binding_name }} = WireWriterPool.acquire({{ binding.size_expr }})
{%- endfor %}
try {
{%- for binding in wire_writers %}
run {
val wire = {{ binding.binding_name }}.writer
{{ binding.encode_expr }}
}
{%- endfor %}
{%- if return_is_unit %}
Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %})
{%- elif return_is_direct %}
return Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %}){{ return_cast }}
{%- else %}
val buf = Native.{{ ffi_name }}({% if include_handle %}handle{% if native_args.len() > 0 %}, {% endif %}{% endif %}{% for arg in native_args %}{{ arg }}{% if !loop.last %}, {% endif %}{% endfor %})
?: throw FfiException(-1, "Null buffer returned")
{%- if is_blittable_return %}
return useWireBytes(buf) { buffer ->
{{ decode_expr }}
}
{%- else %}
val reader = WireReader(buf)
return {{ decode_expr }}
{%- endif %}
{%- endif %}
} finally {
{%- for binding_name in wire_writer_closes %}
{{ binding_name }}.close()
{%- endfor %}
}
{%- endif %}
}