{{ self::ts_doc_block(doc, "") }}export function {{ name }}({% for param in params %}{{ param.name }}: {{ param.ts_type }}{% if !loop.last %}, {% endif %}{% endfor %}): {{ return_type_str }} {
{%- if !wrapper_code.is_empty() %}
  {{ wrapper_code }}
{%- endif %}
{%- if cleanup_code.is_empty() %}
{%- if return_route.is_void() %}
  (_exports.{{ ffi_name }} as Function)({{ call_args }});
{%- elif return_route.is_direct() %}
{%- if let Some(callback_return) = return_callback %}
  const result = (_exports.{{ ffi_name }} as Function)({{ call_args }});
{%- if callback_return.nullable %}
  if (result === 0) {
    return null;
  }
{%- endif %}
  return {{ callback_return.wrap_fn }}(result);
{%- else %}
  return (_exports.{{ ffi_name }} as Function)({{ call_args }}){{ return_route.ts_cast() }};
{%- endif %}
{%- elif return_route.is_struct_return_slot() %}
  const __outPtr = _module.exports.boltffi_wasm_alloc({{ return_route.return_slot_size().unwrap() }});
  try {
    (_exports.{{ ffi_name }} as Function)({% if call_args.is_empty() %}__outPtr{% else %}__outPtr, {{ call_args }}{% endif %});
    return {{ return_route.decode_expr() }};
  } finally {
    _module.exports.boltffi_wasm_free(__outPtr, {{ return_route.return_slot_size().unwrap() }});
  }
{%- elif return_route.is_void_slot() %}
  (_exports.{{ ffi_name }} as Function)({{ call_args }});
  return {{ return_route.decode_expr() }};
{%- elif return_route.is_nan_boxed_optional() %}
  const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as number;
  return {{ return_route.decode_expr() }};
{%- elif return_route.is_packed() %}
  const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as bigint;
  const reader = _module.takePackedBuffer(packed);
  return {{ return_route.decode_expr() }};
{%- else %}
  const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as bigint;
  return {{ return_route.decode_expr() }};
{%- endif %}
{%- else %}
  try {
{%- if return_route.is_void() %}
    (_exports.{{ ffi_name }} as Function)({{ call_args }});
{%- elif return_route.is_direct() %}
{%- if let Some(callback_return) = return_callback %}
    const result = (_exports.{{ ffi_name }} as Function)({{ call_args }});
{%- if callback_return.nullable %}
    if (result === 0) {
      return null;
    }
{%- endif %}
    return {{ callback_return.wrap_fn }}(result);
{%- else %}
    return (_exports.{{ ffi_name }} as Function)({{ call_args }}){{ return_route.ts_cast() }};
{%- endif %}
{%- elif return_route.is_struct_return_slot() %}
    const __outPtr = _module.exports.boltffi_wasm_alloc({{ return_route.return_slot_size().unwrap() }});
    try {
      (_exports.{{ ffi_name }} as Function)({% if call_args.is_empty() %}__outPtr{% else %}__outPtr, {{ call_args }}{% endif %});
      return {{ return_route.decode_expr() }};
    } finally {
      _module.exports.boltffi_wasm_free(__outPtr, {{ return_route.return_slot_size().unwrap() }});
    }
{%- elif return_route.is_void_slot() %}
    (_exports.{{ ffi_name }} as Function)({{ call_args }});
    return {{ return_route.decode_expr() }};
{%- elif return_route.is_nan_boxed_optional() %}
    const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as number;
    return {{ return_route.decode_expr() }};
{%- elif return_route.is_packed() %}
    const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as bigint;
    const reader = _module.takePackedBuffer(packed);
    return {{ return_route.decode_expr() }};
{%- else %}
    const packed = (_exports.{{ ffi_name }} as Function)({{ call_args }}) as bigint;
    return {{ return_route.decode_expr() }};
{%- endif %}
  } finally {
    {{ cleanup_code }}
  }
{%- endif %}
}
