Skip to content

Commit

Permalink
Change how a gd_self method's signature is passed around internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoff12358 committed Oct 1, 2023
1 parent 9f05617 commit 3d71798
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
17 changes: 12 additions & 5 deletions godot-macros/src/class/data_models/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,13 @@ fn make_forwarding_closure(class_name: &Ident, signature_info: &SignatureInfo) -
}
}

fn get_signature_info(signature: &venial::Function, gd_self: bool) -> SignatureInfo {
fn get_signature_info(signature: &venial::Function, has_gd_self: bool) -> SignatureInfo {
let method_name = signature.name.clone();
let mut receiver_type = ReceiverType::Static;
let mut receiver_type = if has_gd_self {
ReceiverType::GdSelf
} else {
ReceiverType::Static
};
let mut param_idents: Vec<Ident> = Vec::new();
let mut param_types = Vec::new();
let ret_type = match &signature.return_ty {
Expand All @@ -206,9 +210,12 @@ fn get_signature_info(signature: &venial::Function, gd_self: bool) -> SignatureI
for (arg, _) in &signature.params.inner {
match arg {
venial::FnParam::Receiver(recv) => {
receiver_type = if gd_self {
ReceiverType::GdSelf
} else if recv.tk_mut.is_some() {
if receiver_type == ReceiverType::GdSelf {
// This shouldn't happen, as when has_gd_self is true the first function parameter should have been removed.
// And the first parameter should be the only one that can be a Receiver.
panic!("has_gd_self is true for a signature starting with a Receiver param.");
}
receiver_type = if recv.tk_mut.is_some() {
ReceiverType::Mut
} else if recv.tk_ref.is_some() {
ReceiverType::Ref
Expand Down
12 changes: 4 additions & 8 deletions godot-macros/src/class/godot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;
use quote::spanned::Spanned;
use venial::{
Attribute, AttributeValue, Constant, Declaration, Error, FnParam, FnReceiverParam, Function,
Impl, ImplMember, TyExpr,
Attribute, AttributeValue, Constant, Declaration, Error, FnParam, Function, Impl, ImplMember,
TyExpr,
};

use crate::class::{make_method_registration, make_virtual_method_callback, FuncDefinition};
Expand Down Expand Up @@ -242,13 +242,9 @@ fn process_godot_fns(decl: &mut Impl) -> Result<(Vec<FuncDefinition>, Vec<Functi
if *has_gd_self {
if sig.params.is_empty() {
return attr.bail("with attribute key `gd_self`, the method must have a first parameter of type Gd<Self>", method);
} else {
sig.params.inner.remove(0);
}
sig.params[0].0 = FnParam::Receiver(FnReceiverParam {
attributes: vec![],
tk_ref: Some(proc_macro2::Punct::new('&', proc_macro2::Spacing::Alone)),
tk_mut: None,
tk_self: Ident::new("self", proc_macro2::Span::call_site()),
});
}
func_definitions.push(FuncDefinition {
func: sig,
Expand Down
2 changes: 1 addition & 1 deletion itest/godot/ManualFfiTests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func test_func_rename():
assert_eq(func_rename.has_method("spell_static"), true)
assert_eq(func_rename.spell_static(), "static")

var gd_self_reference: GdSelfReference
var gd_self_reference
func update_self_reference(value):
gd_self_reference.update_internal(value)

Expand Down
2 changes: 1 addition & 1 deletion itest/rust/src/register_tests/func_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl GdSelfReference {
"update_internal_signal".into(),
&[new_internal.to_variant()],
);
return self.internal_value;
self.internal_value
}

#[func(gd_self)]
Expand Down

0 comments on commit 3d71798

Please sign in to comment.