Skip to content

Commit

Permalink
Merge pull request #159 from pmed/string_view
Browse files Browse the repository at this point in the history
Backport string_view for pre-c++17 compilers
  • Loading branch information
pmed authored Jul 25, 2021
2 parents 809041e + 787989e commit 43583ee
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 141 deletions.
12 changes: 7 additions & 5 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char co
return os;
}

inline void check(std::string msg, bool condition)
inline void check(v8pp::string_view msg, bool condition)
{
if (!condition)
{
Expand All @@ -171,7 +171,7 @@ inline void check(std::string msg, bool condition)
}

template<typename T, typename U>
void check_eq(std::string msg, T actual, U expected)
void check_eq(v8pp::string_view msg, T actual, U expected)
{
if (actual != expected)
{
Expand All @@ -182,20 +182,22 @@ void check_eq(std::string msg, T actual, U expected)
}

template<typename Ex, typename F>
void check_ex(std::string msg, F&& f)
void check_ex(v8pp::string_view msg, F&& f)
{
try
{
f();
check(msg + " expected " + v8pp::detail::type_id<Ex>().name() + " exception", false);
std::stringstream ss;
ss << msg << " expected " << v8pp::detail::type_id<Ex>().name() << " exception";
check(ss.str(), false);
}
catch (Ex const&)
{
}
}

template<typename T>
T run_script(v8pp::context& context, std::string const& source)
T run_script(v8pp::context& context, v8pp::string_view source)
{
v8::Isolate* isolate = context.isolate();

Expand Down
15 changes: 13 additions & 2 deletions test/test_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ void test_conv(v8::Isolate* isolate, T value)
{
v8::Local<v8::Value> v8_value = v8pp::to_v8(isolate, value);
auto const value2 = v8pp::from_v8<T>(isolate, v8_value);
check_eq(v8pp::detail::type_id<T>().name(), value2, value);
check(v8pp::detail::type_id<T>().name(), value2 == value);
}

template<typename Char, size_t N>
void test_string_conv(v8::Isolate* isolate, Char const (&str)[N])
{
std::basic_string<Char> const str2(str, 2);

v8pp::basic_string_view<Char> const sv(str);
v8pp::basic_string_view<Char> const sv2(str, 2);

test_conv(isolate, str[0]);
test_conv(isolate, str);
test_conv(isolate, sv2);

check_eq("string literal",
v8pp::from_v8<Char const*>(isolate, v8pp::to_v8(isolate, str)), str);
check_eq("string literal2",
v8pp::from_v8<Char const*>(isolate, v8pp::to_v8(isolate, str, 2)), str2);
check("string view",
v8pp::from_v8<v8pp::basic_string_view<Char>>(isolate, v8pp::to_v8(isolate, sv)) == sv);

Char const* ptr = str;
check_eq("string pointer",
Expand All @@ -58,9 +64,14 @@ struct person
int age;

//for test framework
bool operator==(person const& other) const
{
return name == other.name && age == other.age;
}

bool operator!=(person const& other) const
{
return name != other.name || age != other.age;
return !(*this == other);
}

friend std::ostream& operator<<(std::ostream& os, person const& p)
Expand Down
12 changes: 6 additions & 6 deletions v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class class_
template<typename Method>
typename std::enable_if<
std::is_member_function_pointer<Method>::value, class_&>::type
set(char const *name, Method mem_func, v8::PropertyAttribute attr = v8::None)
set(string_view name, Method mem_func, v8::PropertyAttribute attr = v8::None)
{
using mem_func_type =
typename detail::function_traits<Method>::template pointer_type<T>;
Expand All @@ -258,7 +258,7 @@ class class_
template<typename Function,
typename Func = typename std::decay<Function>::type>
typename std::enable_if<detail::is_callable<Func>::value, class_&>::type
set(char const *name, Function&& func, v8::PropertyAttribute attr = v8::None)
set(string_view name, Function&& func, v8::PropertyAttribute attr = v8::None)
{
v8::Local<v8::Data> wrapped_fun =
wrap_function_template(isolate(), std::forward<Func>(func));
Expand All @@ -272,7 +272,7 @@ class class_
template<typename Attribute>
typename std::enable_if<
std::is_member_object_pointer<Attribute>::value, class_&>::type
set(char const *name, Attribute attribute, bool readonly = false)
set(string_view name, Attribute attribute, bool readonly = false)
{
v8::HandleScope scope(isolate());

Expand All @@ -298,7 +298,7 @@ class class_
template<typename GetMethod, typename SetMethod>
typename std::enable_if<std::is_member_function_pointer<GetMethod>::value
&& std::is_member_function_pointer<SetMethod>::value, class_&>::type
set(char const *name, property_<GetMethod, SetMethod>&& property)
set(string_view name, property_<GetMethod, SetMethod>&& property)
{
v8::HandleScope scope(isolate());

Expand All @@ -324,7 +324,7 @@ class class_

/// Set value as a read-only property
template<typename Value>
class_& set_const(char const* name, Value const& value)
class_& set_const(string_view name, Value const& value)
{
v8::HandleScope scope(isolate());

Expand All @@ -336,7 +336,7 @@ class class_

/// Set a static value
template<typename Value>
class_& set_static(char const* name, Value const& value, bool readonly = false)
class_& set_static(string_view name, Value const& value, bool readonly = false)
{
v8::HandleScope scope(isolate());

Expand Down
2 changes: 1 addition & 1 deletion v8pp/class.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ V8PP_IMPL class_info::class_info(type_info const& type, type_info const& traits)

V8PP_IMPL std::string class_info::class_name() const
{
return "v8pp::class_<" + type.name() + ", " + traits.name() + ">";
return "v8pp::class_<" + std::string(type.name()) + ", " + std::string(traits.name()) + ">";
}

/////////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 3 additions & 4 deletions v8pp/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ context::~context()
}
}

context& context::set(char const* name, v8::Local<v8::Value> value)
context& context::set(string_view name, v8::Local<v8::Value> value)
{
v8::HandleScope scope(isolate_);
to_local(isolate_, impl_)->Global()->Set(isolate_->GetCurrentContext(), to_v8(isolate_, name), value).FromJust();
return *this;
}

context& context::set(char const* name, module& m)
context& context::set(string_view name, v8pp::module& m)
{
return set(name, m.new_instance());
}
Expand All @@ -258,8 +258,7 @@ v8::Local<v8::Value> context::run_file(std::string const& filename)
return run_script(std::string(begin, end), filename);
}

v8::Local<v8::Value> context::run_script(std::string const& source,
std::string const& filename)
v8::Local<v8::Value> context::run_script(string_view source, string_view filename)
{
v8::EscapableHandleScope scope(isolate_);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
Expand Down
9 changes: 4 additions & 5 deletions v8pp/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ class context
v8::Local<v8::Value> run_file(std::string const& filename);

/// The same as run_file but uses string as the script source
v8::Local<v8::Value> run_script(std::string const& source,
std::string const& filename = "");
v8::Local<v8::Value> run_script(string_view source, string_view filename = "");

/// Set a V8 value in the context global object with specified name
context& set(char const* name, v8::Local<v8::Value> value);
context& set(string_view name, v8::Local<v8::Value> value);

/// Set module to the context global object
context& set(char const *name, module& m);
context& set(string_view name, v8pp::module& m);

/// Set class to the context global object
template<typename T, typename Traits>
context& set(char const* name, class_<T, Traits>& cl)
context& set(string_view name, v8pp::class_<T, Traits>& cl)
{
v8::HandleScope scope(isolate_);
cl.class_function_template()->SetClassName(v8pp::to_v8(isolate_, name));
Expand Down
8 changes: 8 additions & 0 deletions v8pp/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
namespace v8pp {

template struct convert<std::string>;
template struct convert<string_view>;
template struct convert<char const*>;

template struct convert<std::u16string>;
template struct convert<u16string_view>;
template struct convert<char16_t const*>;

#ifdef _WIN32
template struct convert<std::wstring>;
template struct convert<wstring_view>;
template struct convert<wchar_t const*>;
#endif

template struct convert<bool>;

template struct convert<char>;
Expand Down
Loading

0 comments on commit 43583ee

Please sign in to comment.