Skip to content

Commit

Permalink
allow number to string convertion in convert<String>
Browse files Browse the repository at this point in the history
because of JavaScript types coercion.

See issue #177
  • Loading branch information
pmed committed Jan 2, 2022
1 parent b67e0fb commit 4433564
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions test/test_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct convert<person>
{
if (!is_valid(isolate, value))
{
throw std::runtime_error("excpected object");
throw std::runtime_error("expected object");
}

v8::HandleScope scope(isolate);
Expand Down Expand Up @@ -153,6 +153,8 @@ void test_convert()
#ifdef WIN32
test_string_conv(isolate, L"qaz");
#endif
// numeric string
test_string_conv(isolate, "0");

const std::vector<int> vector{ 1, 2, 3 };
test_conv(isolate, vector);
Expand All @@ -170,7 +172,7 @@ void test_convert()
test_conv(isolate, std::map<char, int>{ { 'a', 1 }, { 'b', 2 }, { 'c', 3 } });
test_conv(isolate, std::multimap<int, int>{ { 1, -1 }, { 2, -2 } });
test_conv(isolate, std::unordered_map<char, std::string>{ { 'x', "1" }, { 'y', "2" } });
test_conv(isolate, std::unordered_multimap<std::string, int>{ { "a", 1 }, { "b", 2 } });
test_conv(isolate, std::unordered_multimap<std::string, int>{ { "0", 0 }, { "a", 1 }, { "b", 2 } });

check_eq("initializer list to array",
v8pp::from_v8<std::vector<int>>(isolate, v8pp::to_v8(isolate, { 1, 2, 3 })), vector);
Expand Down
2 changes: 1 addition & 1 deletion v8pp/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct convert<String, typename std::enable_if<detail::is_string<String>::value>

static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
{
return !value.IsEmpty() && value->IsString();
return !value.IsEmpty() && (value->IsString() || value->IsNumber());
}

static from_type from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
Expand Down

0 comments on commit 4433564

Please sign in to comment.