    fn {{ method_name }}({{ signature }}) -> {{ return_type }} {
        // self.env is the napi_env that owned the persistent reference; it is stable for
        // the lifetime of the Node process. Env::from_raw is a safe constructor that
        // gives us a live Env handle for the current scope.
        let __env = napi::Env::from_raw(self.env);
        let obj_ref = match self.obj_ref.as_ref() {
            Some(r) => r,
            None => return {{ default_result_expr }},
        };
        let obj = match obj_ref.get_value(&__env) {
            Ok(o) => o,
            Err(_) => return {{ default_result_expr }},
        };

        // Try to get the JS function from the object
        let func: napi::bindgen_prelude::Function<{{ args_tuple_ty }}, napi::bindgen_prelude::Unknown> = match obj.get_named_property("{{ js_method_name }}") {
            Ok(f) => f,
            Err(_) => return {{ default_result_expr }},
        };

{% if empty_args %}
        let result = func.call(());
{% else %}
{% for arg_expr in arg_exprs %}
        let arg_{{ loop.index0 }}: napi::bindgen_prelude::Unknown = {{ arg_expr }};
{% endfor %}
        let result = func.call(napi::bindgen_prelude::FnArgs::from({{ tuple_args }}));
{% endif %}

        match result {
            Err(_) => {{ default_result_expr }},
            Ok(val) => {
                if val.get_type().ok() == Some(napi::bindgen_prelude::ValueType::Object) {
                    if let Ok(obj) = val.coerce_to_object() {
{%- for variant in payload_result_variants %}
                        let prop_name = match "{{ variant.name }}" {
                            "Custom" => "custom",
                            "Error" => "error",
                            _ => "{{ variant.wire_name }}",
                        };
                        if let Ok(cv) = obj.get_named_property::<napi::bindgen_prelude::Unknown>(prop_name) {
                            if !matches!(cv.get_type().unwrap_or(napi::bindgen_prelude::ValueType::Undefined), napi::bindgen_prelude::ValueType::Undefined | napi::bindgen_prelude::ValueType::Null) {
                                if let Ok(s) = cv.coerce_to_string().and_then(|s| s.into_utf8()).and_then(|s| s.into_owned()) {
                                    return {{ return_type }}::{{ variant.name }}(s);
                                }
                            }
                        }
{%- endfor %}
                    }
                }
                if let Ok(s) = val.coerce_to_string().and_then(|s| s.into_utf8()).and_then(|s| s.into_owned()) {
                    match s.as_str() {
{%- for variant in unit_result_variants %}
                        "{{ variant.wire_name }}" => {{ return_type }}::{{ variant.name }},
{%- endfor %}
                        _ => {{ unknown_string_result_expr }},
                    }
                } else {
                    {{ default_result_expr }}
                }
            }
        }
    }
