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

GDExtension: create_instance_func can be NULL to inhibit construction from GDScript #65542

Closed

Conversation

Bromeon
Copy link
Contributor

@Bromeon Bromeon commented Sep 8, 2022

This is a small quality-of-life change that's useful for classes which should either not be instantiated (e.g. static methods) or only instantiated from the GDExtension binding. The GDScript constructor call MyClass.new() now causes an error, which has the nice side effect that forgetting to specify create_instance_func leads to a clear error rather than a half-constructed object.

Currently, the code already checks for the condition:

(ti->native_extension && !ti->native_extension->create_instance)

so the API header comment /* this one is mandatory */ is wrong. However, the ClassDB code doesn't act upon the null pointer, instead it constructs a "zombie" instance with initialized base but uninitialized extension.


The check at the bottom of the function,

if (ti->native_extension && ti->native_extension->create_instance)

could now theoretically be simplified to:

if (ti->native_extension)

however I'm not sure if that's desired, as another refactoring might quickly introduce UB (null pointer dereferencing).

@Bromeon Bromeon requested a review from a team as a code owner September 8, 2022 20:28
@Bromeon Bromeon force-pushed the feature/failing-extension-creation branch 2 times, most recently from 718dc73 to 7fb4a64 Compare September 8, 2022 20:40
@Bromeon
Copy link
Contributor Author

Bromeon commented Sep 9, 2022

The 2nd commit reuses duplicate code in a common method.

@Bromeon Bromeon force-pushed the feature/failing-extension-creation branch 3 times, most recently from 2b731ef to 3f483eb Compare September 9, 2022 10:58
@YuriSizov YuriSizov added this to the 4.x milestone Sep 9, 2022
@YuriSizov YuriSizov requested a review from a team September 9, 2022 12:08
@Bromeon Bromeon force-pushed the feature/failing-extension-creation branch 2 times, most recently from 8da855a to c7fea96 Compare September 9, 2022 13:37
@Bromeon
Copy link
Contributor Author

Bromeon commented Sep 9, 2022

Fixed a few issues, encountered a null pointer dereference of obj in the test runner in this CI run:

// Create object instance for test.
Object *obj = ClassDB::instantiate(script->get_native()->get_name());
Ref<RefCounted> obj_ref;
if (obj->is_ref_counted()) {
obj_ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
}
obj->set_script(script);
GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance());

I'd need to check why this happens exactly. The first commit passed CI, so probably I introduced a mistake during the refactoring.

Edit: fixed, bad parentheses in boolean condition

@Bromeon Bromeon force-pushed the feature/failing-extension-creation branch from c7fea96 to 300ecb0 Compare September 10, 2022 06:13
@Bromeon Bromeon marked this pull request as draft September 11, 2022 06:52
@Bromeon
Copy link
Contributor Author

Bromeon commented Sep 11, 2022

There's a pending related discussion:

Can we support a void *user_data argument in create_instance_func, providing different ways to construct an instance from GDExtension? If yes, the create_instance_func could also return a null pointer to signal "not constructible this way" (e.g. no default constructor).

@Bromeon Bromeon marked this pull request as ready for review September 11, 2022 07:53
… from GDScript

This can be useful for classes which should either not be instantiated (e.g. only static methods)
or only instantiated from the GDExtension binding.

In practice, this causes a descriptive error message if `MyClass.new()` is used in GDScript.
@Bromeon Bromeon requested review from a team as code owners September 19, 2022 14:09
@Bromeon Bromeon force-pushed the feature/failing-extension-creation branch from a011fab to b24bb58 Compare September 19, 2022 14:11
@Bromeon
Copy link
Contributor Author

Bromeon commented Sep 19, 2022

Oof, sorry for the excessive tagging -- I merged latest master and it included all commits from people in the meantime 😕

Should be fixed now, but unfortunately the review requests don't go away.

@groud
Copy link
Member

groud commented Dec 1, 2022

PR review meeting: may be superseeded by #66979. If that's the case, we could close this PR.

@Bromeon Bromeon closed this Apr 3, 2024
@KoBeWi KoBeWi removed this from the 4.x milestone Apr 3, 2024
@KoBeWi KoBeWi added the archived label Apr 3, 2024
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.

4 participants