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

Use GDExtension to_string in Node #92827

Merged
merged 1 commit into from
Jun 12, 2024

Conversation

raulsntos
Copy link
Member

Before this PR, when a GDExtension class that derives from Node implements _to_string like this:

String _to_string() const {
    return "MyClass";
}

Godot will print @MyNode@2:MyClass instead of just MyClass like it does for other Object-derived classes.

Matches the Object::to_string implementation:

String Object::to_string() {
if (script_instance) {
bool valid;
String ret = script_instance->to_string(&valid);
if (valid) {
return ret;
}
}
if (_extension && _extension->to_string) {
String ret;
GDExtensionBool is_valid;
_extension->to_string(_extension_instance, &is_valid, &ret);
return ret;
}
return "<" + get_class() + "#" + itos(get_instance_id()) + ">";
}

@raulsntos raulsntos added this to the 4.x milestone Jun 6, 2024
@raulsntos raulsntos requested a review from a team as a code owner June 6, 2024 05:51
@dsnopek dsnopek requested a review from a team June 10, 2024 14:03
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.

Looks good to me!

Comment on lines +2594 to +2600
if (_get_extension() && _get_extension()->to_string) {
String ret;
GDExtensionBool is_valid;
_get_extension()->to_string(_get_extension_instance(), &is_valid, &ret);
return ret;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

is_valid has neither a defined input value (uninitialized), nor is its output checked -- is that intended?

Copy link
Contributor

@dsnopek dsnopek Jun 10, 2024

Choose a reason for hiding this comment

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

Those are good questions! However, this code exactly matches what we already have in Object::to_string(), so it isn't making things any worse. :-) If we do end up evaluating how is_valid should be used, then it should be fixed in both places.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarification!

Since we'll likely forget to update one of the two places in that case, should we add a comment in both? (Or directly reuse a function, not sure how easy it would be).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I just copied Object::to_string but I also thought it was weird that is_valid is unused. We should probably check before returning, like we do with valid in scripts:

if (script_instance) {
bool valid;
String ret = script_instance->to_string(&valid);
if (valid) {
return ret;
}
}

Note that valid is also uninitialized. I guess this variable is always assigned by to_string so there's no need to initialize it, but I'm not too familiar with C++ so if we should always initialize it just let me know.

Copy link
Member

Choose a reason for hiding this comment

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

At least the default implementations of to_string() seem to properly set r_valid to false before errors, and true on success.

That's a common pattern for calling methods on ScriptInstance or GDExtension, though it's hard to guarantee that it's always safe. The tradeoff between avoiding one potentially unneeded initialization and risking using uninitialized memory needs to be assessed (especially whether not initializing these actually makes a difference to performance at all).

It's a much bigger scope than just this PR though. For this I'd suggest just putting a comment in both implementations that they should be kept in sync (not for what to do with valid specifically, but the whole method).

Matches the `Object::to_string` implementation.
@akien-mga akien-mga modified the milestones: 4.x, 4.3 Jun 11, 2024
@akien-mga akien-mga merged commit 5bab95a into godotengine:master Jun 12, 2024
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.

4 participants