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

Add functions for non-ptr style virtual calls in GDExtension #80671

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

fuzzybinary
Copy link
Contributor

@fuzzybinary fuzzybinary commented Aug 16, 2023

This adds two functions to GDExtensionClassCreationInfo2 that allow for developers to supply a generic virtual call function along with user data to be sent to that call.

If get_virutal_call_data_func is not null, extensions call this function to get user data to pass to a supplied call_virtual_func. Both must be provided is one is provided.

If get_virtual_call_data_func is null, Godot falls back to the old get_virtual_func logic.

Fixes #63275

@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch 3 times, most recently from 74e2c62 to 34cadca Compare August 16, 2023 01:23
fuzzybinary added a commit to fuzzybinary/godot_dart that referenced this pull request Aug 16, 2023
@Chaosus Chaosus added this to the 4.x milestone Aug 16, 2023
Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

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

Thanks! Overall, I personally think this is a good approach.

This is something that will definitely need a bunch of discussion, though, perhaps at a meeting. It'd also be really great if we could get folks working on different language bindings (particularly those that were having trouble with the old way) to see if it would work for them.

core/extension/gdextension_interface.h Outdated Show resolved Hide resolved
core/object/make_virtuals.py Outdated Show resolved Hide resolved
@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch 3 times, most recently from 46cd3b0 to c5c0f40 Compare August 18, 2023 18:35
@dsnopek
Copy link
Contributor

dsnopek commented Aug 22, 2023

Thanks, this is looking good!

There's some CI failures from build errors in unrelated code. Rebasing on the latest master may fix that?

@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch from 42845da to 7f6aedb Compare August 24, 2023 02:25
@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch 2 times, most recently from 113f155 to 54f6e9d Compare September 1, 2023 01:49
@fuzzybinary fuzzybinary marked this pull request as ready for review September 1, 2023 01:50
@fuzzybinary fuzzybinary requested review from a team as code owners September 1, 2023 01:50
@fuzzybinary
Copy link
Contributor Author

This should now be ready. I've moved the new members into the GDExtensionClassCreationInfo2 struct.

@AThousandShips I think I fixed the uninitialized ptr return variable. Can you take another look and see if it makes sense to you?

@AThousandShips
Copy link
Member

Will take a look!

Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

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

This is looking great to me! I only have the one naming related note.

I think this should get some more feedback from other GDExtension team members, though, perhaps discussing it at the next meeting.

Also, the CI is failing on building godot-cpp, but I don't think that we can fix that. :-/ It's a circular dependency: godot-cpp will need to be updated for this change, but godot-cpp depends on the Godot change.

I think this will come up again as we make further changes to the new structs before Godot 4.2. I'm not sure what we can do about that? We could maybe have the Godot CI use the 4.1 branch of godot-cpp instead of master?

core/object/make_virtuals.py Outdated Show resolved Hide resolved
@AThousandShips
Copy link
Member

I think the code paths look good now and the CI errors have gone away

@fuzzybinary
Copy link
Contributor Author

Renamed the var, squashed and rebased on master. Should be good for review pending a decision on the godot-cpp build errors.

@dsnopek
Copy link
Contributor

dsnopek commented Sep 1, 2023

PR #81238 should fix the CI. If/when it's merged, this one will just need a rebase.

@fuzzybinary
Copy link
Contributor Author

Rebased -- not sure if the CI error is something on my end?

@fuzzybinary
Copy link
Contributor Author

Renamed variables and rebased onto master.

@dsnopek
Copy link
Contributor

dsnopek commented Sep 10, 2023

@fuzzybinary:

So even if this is more complicated it is for sure giving me higher performance than if we were just sending in the name.

Ah, thanks for sharing how you're using this in your Dart bindings! If you're able to take advantage of the API in order to cache pointers to the underlying Dart functions, I think that's a solid justification for this approach. It allows calls to virtual functions defined in Dart to avoid a lookup on every call, just like with the C++ binding. (Personally, I don't want any language bindings forced to be "second class citizens" just because they can't easily generate new functions with C linkage)

Renamed variables and rebased onto master.

Thanks! The suggestion from the meeting was to also rename the p_userdata argument to p_class_userdata on the other related function pointer typedefs as well, for consistency. So, also on GDExtensionClassCreateInstance and GDExtensionClassFreeInstance too.

@dsnopek dsnopek modified the milestones: 4.x, 4.2 Sep 10, 2023
@fuzzybinary
Copy link
Contributor Author

👍 I can do that, I was thinking it would be better to keep these changes localized to virtuals but I can change it in this PR if we want.

Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

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

Thanks! This is looking great to me :-)

Copy link
Contributor

@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.

Few things to change, mostly clarify the semantics a bit and guide the user to choose between old vs. new approach.

core/extension/gdextension_interface.h Outdated Show resolved Hide resolved
core/extension/gdextension.cpp Outdated Show resolved Hide resolved
@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch 2 times, most recently from 505bfed to 95a3260 Compare September 14, 2023 00:38
@fuzzybinary fuzzybinary force-pushed the gdextension-virtuals branch 2 times, most recently from 45d43b6 to 22db719 Compare September 14, 2023 21:57
@fuzzybinary fuzzybinary requested a review from Bromeon September 15, 2023 22:02
Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

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

Looking good to me!

Copy link
Contributor

@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.

Two smaller things, see comments.

Side note, the names get_virtual_call_data and call_virtual_with_data are a bit difficult to memorize due to different order of the terms. Something like get_virtual_data and call_virtual_data might be a bit easier, although possibly less expressive. It's a tiny issue though and hardly relevant in practice, so if others are OK with the current naming, I won't bikeshed here 🙂

core/extension/gdextension.cpp Outdated Show resolved Hide resolved
@fuzzybinary
Copy link
Contributor Author

Two smaller things, see comments.

Side note, the names get_virtual_call_data and call_virtual_with_data are a bit difficult to memorize due to different order of the terms. Something like get_virtual_data and call_virtual_data might be a bit easier, although possibly less expressive.

Yeah, while easier, it's not actually descriptive of what's happening. Although harder to memorize with the reordering, I do think the parallelism of "get_call_data" -> "call_with_data" is helpful.

I'll rename the structure member to be more in line.

@fuzzybinary
Copy link
Contributor Author

@Bromeon Took one more stab at the documentation. Let me know if that's clearer.

@Bromeon
Copy link
Contributor

Bromeon commented Sep 19, 2023

Mostly good, this is a tiny bit ambiguous due to unclear "precedence":

You should supply either get_virtual_func or get_virtual_call_data_func with call_virtual_with_data_func.

Probably a comma would help a lot:

You should supply either get_virtual_func, or get_virtual_call_data_func with call_virtual_with_data_func.

Other than that, good from my side, thanks for the adaptations!

This adds two functions to `GDExtensionClassCreationInfo` that allow for developers to supply a generic virtual call function along with user data to be sent to that call.

If `get_virutal_call_data_func` is not null, extensions call this function to get user data to pass to a supplied `call_virtual_with_data_func`. Both must be provided is one is provided.

If `get_virtual_call_data_func` is null, Godot falls back to the old `get_virtual_func` logic.

Fixes godotengine#63275

Co-authored-by: David Snopek <dsnopek@gmail.com>
@fuzzybinary
Copy link
Contributor Author

Comma added. Rebased on master.

Copy link
Contributor

@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 for your work! 👍

@Bromeon
Copy link
Contributor

Bromeon commented Sep 20, 2023

There seems to be a memory corruption in the double build, here... Not sure if introduced by this PR?

Edit: re-running made it work. A bit flaky tests, but looks unrelated to this.

@akien-mga akien-mga merged commit 6df12fe into godotengine:master Sep 20, 2023
16 checks passed
@akien-mga
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

Virtual functions called on objects produced by GDExtension doesn't work on languages other than C++
6 participants