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

Support for string_view slots #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

feliwir
Copy link

@feliwir feliwir commented Jun 14, 2023

This adresses #16
These changes do work for me, however i have no idea if it includes all places required (there's too much template magic going on for me :P )

Copy link
Owner

@matusnovak matusnovak left a comment

Choose a reason for hiding this comment

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

Yeah, it is a lot of templating magic, but this looks pretty good! If you update the documentation I can merge it.

@@ -255,6 +260,11 @@ namespace wrenbind17 {
return std::string(wrenGetSlotString(vm, idx));
}

template <> inline std::string_view getSlot(WrenVM* vm, int idx) {
validate<WrenType::WREN_TYPE_STRING>(vm, idx);
return std::string_view(wrenGetSlotString(vm, idx));
Copy link
Owner

Choose a reason for hiding this comment

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

This may be very dangerous. The string_view would not own the memory.

Therefore, if you have a C++ function that accepts string_view, and stores the string_view for a future use, then that may cause a segmentation fault. As far as I know Wren only guarantees that the raw bytes of that slot (string) is valid only during the call of the C++ function. The garbage collection may run after calling the function and the string_view points to an unknown memory.

This is fine the other way around. Wren makes a copy of the string/string_view you give it.

I am fine with accepting the risk, but could you update the documentation here? -> https://github.com/matusnovak/wrenbind17/blob/master/docs/content/Tutorial/types.md Maybe adding some section explaining this danger. I don't mind the exact paragraphs, I can leave that to you. Plus a brief comment on this line that yes it is a risk.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for the late response, but i got distracted with other projects and completly forgot about this PR, until i checked out my old project.

I can update the paragraph, but i think std::string_view does never own memory does it?
So the risk should probably be known to the user.

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

Successfully merging this pull request may close these issues.

None yet

2 participants