Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple #[godot_api] impl blocks #927

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

0x53A
Copy link
Contributor

@0x53A 0x53A commented Oct 23, 2024

fixes #925

  • it works
  • docs
  • itests
  • manual test
    • linux
    • macos
    • windows
    • webassembly
use godot::prelude::*;

use godot::classes::Node;
use godot::classes::INode;

#[derive(GodotClass)]
#[class(base=Node, init)]
pub struct TestNode {
    base: Base<Node>
}

#[godot_api]
impl TestNode {
    #[func]
    fn foo() {
        godot_print!("[TestNode] called 'foo'");
    }
}



#[godot_api(secondary)]
impl TestNode {
    #[func]
    fn bar() {
        godot_print!("[TestNode] called 'bar'");
    }
}

It errors with acceptable error messages and spans when unsupported keys are added to the attribute:

#[godot_api(anything_except_secondary)]
impl TestNode {
}



#[godot_api(anything)]
impl INode for TestNode {
}

image

@0x53A 0x53A marked this pull request as draft October 23, 2024 01:11
@0x53A
Copy link
Contributor Author

0x53A commented Oct 23, 2024

The idea is that the "primary" impl block defines a static Vec<fn()> (two, actually, one for methods, one for constants) and does the class registration, which executes all therein stored callbacks.

The "secondary" impl blocks then just push into these Vecs.

Here is the generated code, for completeness sake:

use godot::prelude::*;

use godot::classes::Node;
use godot::classes::INode;

#[derive(GodotClass)]
#[class(base=Node, init)]
pub struct TestNode {
    base: Base<Node>
}

// Recursive expansion of godot_api macro
// =======================================

impl TestNode {
    fn foo() {
        godot_print!("[TestNode] called 'foo'");
    }
}
#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static __registration_methods_TestNode: std::sync::Mutex<Vec<fn()>> =
    std::sync::Mutex::new(Vec::new());

#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static __registration_constants_TestNode: std::sync::Mutex<Vec<fn()>> =
    std::sync::Mutex::new(Vec::new());

impl ::godot::obj::cap::ImplementsGodotApi for TestNode {
    fn __register_methods() {
        let guard = __registration_methods_TestNode.lock().unwrap();
        for f in guard.iter() {
            f();
        }
    }
    fn __register_constants() {
        let guard = __registration_constants_TestNode.lock().unwrap();
        for f in guard.iter() {
            f();
        }
    }
}

const _: () = {
    #[allow(non_upper_case_globals)]
    #[used]
    #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
    #[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "android", link_section = ".init_array")]
    #[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
    #[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "linux", link_section = ".init_array")]
    #[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
    static __init: extern "C" fn() = {
        #[cfg_attr(target_os = "android", link_section = ".text.startup")]
        #[cfg_attr(target_os = "linux", link_section = ".text.startup")]
        extern "C" fn __inner_init() {
            {
                __registration_methods_TestNode.lock().unwrap().push(|| {
                    {
                        use ::godot::builtin::{StringName, Variant};
                        use ::godot::obj::GodotClass;
                        use ::godot::register::private::method::ClassMethodInfo;
                        use ::godot::sys;
                        type Sig = ((),);
                        let method_name = StringName::from("foo");
                        unsafe extern "C" fn varcall_fn(
                            _method_data: *mut std::ffi::c_void,
                            instance_ptr: sys::GDExtensionClassInstancePtr,
                            args_ptr: *const sys::GDExtensionConstVariantPtr,
                            arg_count: sys::GDExtensionInt,
                            ret: sys::GDExtensionVariantPtr,
                            err: *mut sys::GDExtensionCallError,
                        ) {
                            let call_ctx = ::godot::meta::CallContext::func("TestNode", "foo");
                            ::godot::private::handle_varcall_panic(&call_ctx, &mut *err, || {
                                <Sig as ::godot::meta::VarcallSignatureTuple>::in_varcall(
                                    instance_ptr,
                                    &call_ctx,
                                    args_ptr,
                                    arg_count,
                                    ret,
                                    err,
                                    |_, params| {
                                        let () = params;
                                        TestNode::foo()
                                    },
                                )
                            });
                        };
                        unsafe extern "C" fn ptrcall_fn(
                            _method_data: *mut std::ffi::c_void,
                            instance_ptr: sys::GDExtensionClassInstancePtr,
                            args_ptr: *const sys::GDExtensionConstTypePtr,
                            ret: sys::GDExtensionTypePtr,
                        ) {
                            let call_ctx = ::godot::meta::CallContext::func("TestNode", "foo");
                            let _success = ::godot::private::handle_panic(
                                || &call_ctx,
                                || {
                                    <Sig as ::godot::meta::PtrcallSignatureTuple>::in_ptrcall(
                                        instance_ptr,
                                        &call_ctx,
                                        args_ptr,
                                        ret,
                                        |_, params| {
                                            let () = params;
                                            TestNode::foo()
                                        },
                                        sys::PtrcallType::Standard,
                                    )
                                },
                            );
                        };
                        let method_info = unsafe {
                            ClassMethodInfo::from_signature::<TestNode, Sig>(
                                method_name,
                                Some(varcall_fn),
                                Some(ptrcall_fn),
                                ::godot::global::MethodFlags::NORMAL
                                    | ::godot::global::MethodFlags::STATIC,
                                &[],
                            )
                        };
                        {
                            if false {
                                format_args!("   Register fn:   {}::{}", "TestNode", "foo");
                            }
                        };
                        method_info.register_extension_class_method();
                    };
                });
                __registration_constants_TestNode
                    .lock()
                    .unwrap()
                    .push(|| {});
            }
        }
        __inner_init
    };
    #[cfg(target_family = "wasm")]
    {
        core::panicking::panic_fmt(core::const_format_args!(
            "not yet implemented: {}",
            core::format_args!("wasm not yet implemented")
        ));
    }
};

const _: () = {
    #[allow(non_upper_case_globals)]
    #[used]
    #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
    #[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "android", link_section = ".init_array")]
    #[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
    #[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "linux", link_section = ".init_array")]
    #[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
    static __init: extern "C" fn() = {
        #[cfg_attr(target_os = "android", link_section = ".text.startup")]
        #[cfg_attr(target_os = "linux", link_section = ".text.startup")]
        extern "C" fn __inner_init() {
            let mut guard = ::godot::private::__godot_rust_plugin___GODOT_PLUGIN_REGISTRY
                .lock()
                .unwrap();
            guard.push(
                (::godot::private::ClassPlugin {
                    class_name: <TestNode as ::godot::obj::GodotClass>::class_name(),
                    item: ::godot::private::PluginItem::InherentImpl(
                        ::godot::private::InherentImpl {
                            register_methods_constants_fn: ::godot::private::ErasedRegisterFn {
                                raw: ::godot::private::callbacks::register_user_methods_constants::<
                                    TestNode,
                                >,
                            },
                            register_rpcs_fn: Some(::godot::private::ErasedRegisterRpcsFn {
                                raw: ::godot::private::callbacks::register_user_rpcs::<TestNode>,
                            }),
                        },
                    ),
                    init_level: <TestNode as ::godot::obj::GodotClass>::INIT_LEVEL,
                }),
            );
        }
        __inner_init
    };
    #[cfg(target_family = "wasm")]
    godot_ffi::gensym! {
        godot_ffi::plugin_add_inner_wasm!()
    }
};




// Recursive expansion of godot_api macro
// =======================================

impl TestNode {
    fn bar() {
        godot_print!("[TestNode] called 'bar'");
    }
}
const _: () = {
    #[allow(non_upper_case_globals)]
    #[used]
    #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
    #[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
    #[cfg_attr(target_os = "android", link_section = ".init_array")]
    #[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
    #[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "linux", link_section = ".init_array")]
    #[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
    #[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
    static __init: extern "C" fn() = {
        #[cfg_attr(target_os = "android", link_section = ".text.startup")]
        #[cfg_attr(target_os = "linux", link_section = ".text.startup")]
        extern "C" fn __inner_init() {
            {
                __registration_methods_TestNode.lock().unwrap().push(|| {
                    {
                        use ::godot::builtin::{StringName, Variant};
                        use ::godot::obj::GodotClass;
                        use ::godot::register::private::method::ClassMethodInfo;
                        use ::godot::sys;
                        type Sig = ((),);
                        let method_name = StringName::from("bar");
                        unsafe extern "C" fn varcall_fn(
                            _method_data: *mut std::ffi::c_void,
                            instance_ptr: sys::GDExtensionClassInstancePtr,
                            args_ptr: *const sys::GDExtensionConstVariantPtr,
                            arg_count: sys::GDExtensionInt,
                            ret: sys::GDExtensionVariantPtr,
                            err: *mut sys::GDExtensionCallError,
                        ) {
                            let call_ctx = ::godot::meta::CallContext::func("TestNode", "bar");
                            ::godot::private::handle_varcall_panic(&call_ctx, &mut *err, || {
                                <Sig as ::godot::meta::VarcallSignatureTuple>::in_varcall(
                                    instance_ptr,
                                    &call_ctx,
                                    args_ptr,
                                    arg_count,
                                    ret,
                                    err,
                                    |_, params| {
                                        let () = params;
                                        TestNode::bar()
                                    },
                                )
                            });
                        };
                        unsafe extern "C" fn ptrcall_fn(
                            _method_data: *mut std::ffi::c_void,
                            instance_ptr: sys::GDExtensionClassInstancePtr,
                            args_ptr: *const sys::GDExtensionConstTypePtr,
                            ret: sys::GDExtensionTypePtr,
                        ) {
                            let call_ctx = ::godot::meta::CallContext::func("TestNode", "bar");
                            let _success = ::godot::private::handle_panic(
                                || &call_ctx,
                                || {
                                    <Sig as ::godot::meta::PtrcallSignatureTuple>::in_ptrcall(
                                        instance_ptr,
                                        &call_ctx,
                                        args_ptr,
                                        ret,
                                        |_, params| {
                                            let () = params;
                                            TestNode::bar()
                                        },
                                        sys::PtrcallType::Standard,
                                    )
                                },
                            );
                        };
                        let method_info = unsafe {
                            ClassMethodInfo::from_signature::<TestNode, Sig>(
                                method_name,
                                Some(varcall_fn),
                                Some(ptrcall_fn),
                                ::godot::global::MethodFlags::NORMAL
                                    | ::godot::global::MethodFlags::STATIC,
                                &[],
                            )
                        };
                        {
                            if false {
                                format_args!("   Register fn:   {}::{}", "TestNode", "bar");
                            }
                        };
                        method_info.register_extension_class_method();
                    };
                });
                __registration_constants_TestNode
                    .lock()
                    .unwrap()
                    .push(|| {});
            }
        }
        __inner_init
    };
    #[cfg(target_family = "wasm")]
    {
        core::panicking::panic_fmt(core::const_format_args!(
            "not yet implemented: {}",
            core::format_args!("wasm not yet implemented")
        ));
    }
};

@0x53A
Copy link
Contributor Author

0x53A commented Oct 23, 2024

It should be possible to get rid of the explicit secondary attribute by keeping static local state in between macro invocations, I'll look at that later

@0x53A
Copy link
Contributor Author

0x53A commented Oct 23, 2024

One limitation currently is that you can have multiple impl blocks, but only in the same file.

The Vec<fn()> could be moved to a Map<String, Vec<fn()>> (with key = classname) inside gdext, similar to the PLUGIN_REGISTRY, then you could theoretically have multiple impl blocks, spread over multiple files.

@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-927

@Bromeon
Copy link
Member

Bromeon commented Oct 23, 2024

Wow, impressive 😮 thanks a lot!


It should be possible to get rid of the explicit secondary attribute by keeping static local state in between macro invocations, I'll look at that later

Let's avoid that, stateful macros are a hack that may cease to work in the future, or with sandboxed macros (c.f. experimental projects like https://github.com/dtolnay/watt).

It's a very minor QoL impediment to write "secondary", I don't see it as an issue in practice.

Are multiple #[godot_api(secondary)] blocks supported?


The Vec<fn()> could be moved to a Map<String, Vec<fn()>> (with key = classname) inside gdext, similar to the PLUGIN_REGISTRY, then you could theoretically have multiple impl blocks, spread over multiple files.

Imo it's fine to have a first version with this limitation, and look into this feature in a separate PR. That way, the amount of changes to review and check isn't too big 🙂

@0x53A 0x53A marked this pull request as ready for review October 26, 2024 20:27
@0x53A
Copy link
Contributor Author

0x53A commented Oct 26, 2024

It's still missing the docs, but I think it's ready for bikeshedding - a review would be appreciated!

Are multiple #[godot_api(secondary)] blocks supported?

yes, unlimited, unless there's a limit w.r.t. the linker etc

@0x53A 0x53A changed the title [WIP] Multiple #[godot_api] impl blocks Multiple #[godot_api] impl blocks Oct 26, 2024
@Bromeon
Copy link
Member

Bromeon commented Nov 7, 2024

Thanks! I'm a bit busy with preparing v0.2 release, so review may have to wait a bit, but I haven't forgotten! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple #[godot_api] impl blocks
3 participants