Skip to content

Support for string_view slots#17

Open
feliwir wants to merge 1 commit into
matusnovak:masterfrom
feliwir:support-stringview
Open

Support for string_view slots#17
feliwir wants to merge 1 commit into
matusnovak:masterfrom
feliwir:support-stringview

Conversation

@feliwir
Copy link
Copy Markdown

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


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
Copy Markdown
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
Copy Markdown
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.

2 participants