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

Merged
merged 1 commit into from
Nov 28, 2024

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 force-pushed the dev-multiple-impl-blocks branch from d5c73e6 to 6adbdc3 Compare October 26, 2024 21:19
@0x53A 0x53A changed the title [WIP] Multiple #[godot_api] impl blocks Multiple #[godot_api] impl blocks Oct 26, 2024
@0x53A 0x53A force-pushed the dev-multiple-impl-blocks branch 2 times, most recently from a0df6ed to 2a5660f Compare November 6, 2024 23:17
@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! 🙂

@fpdotmonkey
Copy link
Contributor

I can confirm that as of 2a5660f, the automated tests pass on Linux.

@Bromeon Bromeon added feature Adds functionality to the library c: register Register classes, functions and other symbols to GDScript labels Nov 23, 2024
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, still impressed how quickly you figured this out! 👍

Added some remarks, most of them smaller bikeshedding things. As a general style rule, comments should typically start with uppercase and end with a period (unless they're really just keywords).

Would also need a rebase.

itest/rust/src/object_tests/object_test.rs Outdated Show resolved Hide resolved
itest/rust/src/object_tests/object_test.rs Outdated Show resolved Hide resolved
itest/rust/src/object_tests/object_test.rs Outdated Show resolved Hide resolved
itest/rust/src/object_tests/object_test.rs Outdated Show resolved Hide resolved
godot-macros/src/lib.rs Outdated Show resolved Hide resolved
itest/rust/src/object_tests/object_test.rs Outdated Show resolved Hide resolved
godot-macros/src/class/data_models/inherent_impl.rs Outdated Show resolved Hide resolved
godot-macros/src/class/data_models/inherent_impl.rs Outdated Show resolved Hide resolved
godot-macros/src/class/data_models/inherent_impl.rs Outdated Show resolved Hide resolved
godot-ffi/src/plugins.rs Outdated Show resolved Hide resolved
@0x53A 0x53A force-pushed the dev-multiple-impl-blocks branch 3 times, most recently from 296da8e to c82a957 Compare November 27, 2024 17:35
@0x53A
Copy link
Contributor Author

0x53A commented Nov 27, 2024

I think (hope) I've caught all review comments.

@0x53A 0x53A force-pushed the dev-multiple-impl-blocks branch from c82a957 to ecda5ea Compare November 27, 2024 17:39
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

godot-macros/src/class/godot_api.rs Outdated Show resolved Hide resolved
godot-macros/src/class/godot_api.rs Show resolved Hide resolved
itest/rust/src/register_tests/multiple_impl_blocks_test.rs Outdated Show resolved Hide resolved
itest/rust/src/register_tests/multiple_impl_blocks_test.rs Outdated Show resolved Hide resolved
godot-macros/src/lib.rs Outdated Show resolved Hide resolved
godot-macros/src/class/data_models/inherent_impl.rs Outdated Show resolved Hide resolved
@0x53A 0x53A force-pushed the dev-multiple-impl-blocks branch from bfb578f to 8fb7059 Compare November 28, 2024 05:25
@0x53A
Copy link
Contributor Author

0x53A commented Nov 28, 2024

rebased once more, relevant diff with the changes since your review is ecda5ea..6ba99ba

All but one blocks must have the key 'secondary'.
@0x53A 0x53A force-pushed the dev-multiple-impl-blocks branch from 8fb7059 to 6ba99ba Compare November 28, 2024 05:30
@Bromeon Bromeon added this pull request to the merge queue Nov 28, 2024
Merged via the queue into godot-rust:master with commit c6b8b0e Nov 28, 2024
15 checks passed
@Bromeon
Copy link
Member

Bromeon commented Nov 28, 2024

Thanks a lot for this great addition! 💪

@0x53A
Copy link
Contributor Author

0x53A commented Nov 28, 2024

Thanks for merging it. Without promises about timelines (I accidentally started working on a filesystem), I'd like to work on removing the requirement for secondary in a followup PR.

@0x53A 0x53A deleted the dev-multiple-impl-blocks branch December 4, 2024 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: register Register classes, functions and other symbols to GDScript feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple #[godot_api] impl blocks
4 participants