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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/wrenbind17/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ namespace wrenbind17 {
return type == WREN_TYPE_STRING;
}

template <> inline bool ReturnValue::is<std::string_view>() const {
return type == WREN_TYPE_STRING;
}

template <> inline std::nullptr_t ReturnValue::as<std::nullptr_t>() {
if (!is<std::nullptr_t>()) {
throw BadCast("Return value is not a null");
Expand Down
5 changes: 5 additions & 0 deletions include/wrenbind17/caller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ namespace wrenbind17 {
}
};

template <>
inline void ForeginMethodReturnHelper<std::string_view>::push(WrenVM* vm, int index, std::string_view ret) {
PushHelper<std::string_view>::f(vm, index, ret);
}

template <>
inline void ForeginMethodReturnHelper<const std::string&>::push(WrenVM* vm, int index, const std::string& ret) {
PushHelper<const std::string&>::f(vm, index, ret);
Expand Down
10 changes: 10 additions & 0 deletions include/wrenbind17/pop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <wren.hpp>

#include <string>
#include <string_view>
#include <memory>

#include "object.hpp"
Expand Down Expand Up @@ -54,6 +55,10 @@ namespace wrenbind17 {
return wrenGetSlotType(vm, idx) == WrenType::WREN_TYPE_STRING;
}

template <> inline bool is<std::string_view>(WrenVM* vm, const int idx) {
return wrenGetSlotType(vm, idx) == WrenType::WREN_TYPE_STRING;
}

template <> inline bool is<std::nullptr_t>(WrenVM* vm, const int idx) {
return wrenGetSlotType(vm, idx) == WrenType::WREN_TYPE_NULL;
}
Expand Down Expand Up @@ -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.

}

template <> inline std::nullptr_t getSlot(WrenVM* vm, int idx) {
validate<WrenType::WREN_TYPE_NULL>(vm, idx);
return nullptr;
Expand Down
7 changes: 7 additions & 0 deletions include/wrenbind17/push.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <wren.hpp>

#include <string>
#include <string_view>

#include "exception.hpp"
#include "object.hpp"
Expand Down Expand Up @@ -199,6 +200,12 @@ namespace wrenbind17 {
}
};

template <> struct PushHelper<std::string_view> {
static inline void f(WrenVM* vm, int idx, std::string_view value) {
wrenSetSlotString(vm, idx, value.data());
}
};

template <> struct PushHelper<const std::string> {
static inline void f(WrenVM* vm, int idx, const std::string value) {
wrenSetSlotString(vm, idx, value.c_str());
Expand Down