Merge lp:~abreu-alexandre/v8-cpp/set-list-convert into lp:v8-cpp

Proposed by Alexandre Abreu
Status: Merged
Approved by: Marcus Tomlinson
Approved revision: 36
Merged at revision: 35
Proposed branch: lp:~abreu-alexandre/v8-cpp/set-list-convert
Merge into: lp:v8-cpp
Diff against target: 253 lines (+179/-1)
5 files modified
src/internal/convert.h (+101/-1)
tests/errors/test.cpp (+16/-0)
tests/functions/module.cpp (+7/-0)
tests/functions/test.cpp (+27/-0)
tests/functions/test.h (+28/-0)
To merge this branch: bzr merge lp:~abreu-alexandre/v8-cpp/set-list-convert
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Approve
Review via email: mp+274740@code.launchpad.net

Commit message

Add list/set overloads for convert funcs

Description of the change

Add list/set overloads for convert funcs

To post a comment you must log in.
Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Could you please add checks for list and set to the conversion_errors test in tests/errors/test.cpp:150.

Left a few inline comments as well.

review: Needs Fixing
36. By Alexandre Abreu

Fixes

Revision history for this message
Alexandre Abreu (abreu-alexandre) wrote :

updated

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Awesome, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/internal/convert.h'
2--- src/internal/convert.h 2015-10-09 10:43:41 +0000
3+++ src/internal/convert.h 2015-10-19 13:21:02 +0000
4@@ -19,9 +19,11 @@
5 #pragma once
6
7 #include <climits>
8-#include <vector>
9+#include <list>
10 #include <map>
11 #include <memory>
12+#include <set>
13+#include <vector>
14
15 #include <v8.h>
16
17@@ -276,6 +278,98 @@
18 }
19 };
20
21+// List converter
22+template <typename T, typename Alloc>
23+struct Convert<std::list<T, Alloc>>
24+{
25+ using FromType = std::list<T, Alloc>;
26+ using ToType = v8::Local<v8::Array>;
27+
28+ static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
29+ {
30+ return !value.IsEmpty() && value->IsArray();
31+ }
32+
33+ static FromType from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
34+ {
35+ if (!is_valid(isolate, value))
36+ {
37+ throw std::invalid_argument("expected list value");
38+ }
39+
40+ v8::HandleScope scope(isolate);
41+ v8::Local<v8::Array> array = value.As<v8::Array>();
42+
43+ FromType result;
44+ for (uint32_t i = 0, count = array->Length(); i < count; ++i)
45+ {
46+ result.push_back(Convert<T>::from_v8(isolate, array->Get(i)));
47+ }
48+ return result;
49+ }
50+
51+ static ToType to_v8(v8::Isolate* isolate, FromType const& value)
52+ {
53+ v8::EscapableHandleScope scope(isolate);
54+
55+ uint32_t size = static_cast<uint32_t>(value.size());
56+ v8::Local<v8::Array> result = v8::Array::New(isolate, size);
57+ size_t i = 0;
58+ for (auto & e: value)
59+ {
60+ result->Set(i, Convert<T>::to_v8(isolate, e));
61+ ++i;
62+ }
63+ return scope.Escape(result);
64+ }
65+};
66+
67+// Set converter
68+template <typename T, typename Alloc>
69+struct Convert<std::set<T, Alloc>>
70+{
71+ using FromType = std::set<T, Alloc>;
72+ using ToType = v8::Local<v8::Array>;
73+
74+ static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
75+ {
76+ return !value.IsEmpty() && value->IsArray();
77+ }
78+
79+ static FromType from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
80+ {
81+ if (!is_valid(isolate, value))
82+ {
83+ throw std::invalid_argument("expected set value");
84+ }
85+
86+ v8::HandleScope scope(isolate);
87+ v8::Local<v8::Array> array = value.As<v8::Array>();
88+
89+ FromType result;
90+ for (uint32_t i = 0, count = array->Length(); i < count; ++i)
91+ {
92+ result.insert(Convert<T>::from_v8(isolate, array->Get(i)));
93+ }
94+ return result;
95+ }
96+
97+ static ToType to_v8(v8::Isolate* isolate, FromType const& value)
98+ {
99+ v8::EscapableHandleScope scope(isolate);
100+
101+ uint32_t size = static_cast<uint32_t>(value.size());
102+ v8::Local<v8::Array> result = v8::Array::New(isolate, size);
103+ size_t i = 0;
104+ for (auto & e: value)
105+ {
106+ result->Set(i, Convert<T>::to_v8(isolate, e));
107+ ++i;
108+ }
109+ return scope.Escape(result);
110+ }
111+};
112+
113 // Map converter
114 template <typename Key, typename Value, typename Less, typename Alloc>
115 struct Convert<std::map<Key, Value, Less, Alloc>>
116@@ -384,6 +478,12 @@
117 template <typename T, typename Alloc>
118 struct IsExportedClass<std::vector<T, Alloc>> : std::false_type {};
119
120+template <typename T, typename Alloc>
121+struct IsExportedClass<std::list<T, Alloc>> : std::false_type {};
122+
123+template <typename T, typename Alloc>
124+struct IsExportedClass<std::set<T, Alloc>> : std::false_type {};
125+
126 template <typename Key, typename Value, typename Less, typename Alloc>
127 struct IsExportedClass<std::map<Key, Value, Less, Alloc>> : std::false_type {};
128
129
130=== modified file 'tests/errors/test.cpp'
131--- tests/errors/test.cpp 2015-10-09 10:43:41 +0000
132+++ tests/errors/test.cpp 2015-10-19 13:21:02 +0000
133@@ -213,6 +213,22 @@
134 {
135 EXPECT_STREQ(e.what(), "expected an exported object");
136 }
137+ try
138+ {
139+ v8cpp::run_script<std::list<char>>("1");
140+ }
141+ catch (std::exception const& e)
142+ {
143+ EXPECT_STREQ(e.what(), "expected list value");
144+ }
145+ try
146+ {
147+ v8cpp::run_script<std::set<char>>("1");
148+ }
149+ catch (std::exception const& e)
150+ {
151+ EXPECT_STREQ(e.what(), "expected set value");
152+ }
153 }
154
155 TEST(Test, throw_from_module)
156
157=== modified file 'tests/functions/module.cpp'
158--- tests/functions/module.cpp 2015-10-09 10:43:41 +0000
159+++ tests/functions/module.cpp 2015-10-19 13:21:02 +0000
160@@ -18,6 +18,10 @@
161
162 #include "test.h"
163
164+#include <set>
165+#include <vector>
166+#include <list>
167+
168 #include <v8-cpp.h>
169
170 using namespace v8;
171@@ -45,7 +49,10 @@
172 module.add_class("TestCaller", testcaller);
173 module.add_class("Shared", shared);
174 module.add_function("simple_function", &simple_function);
175+
176+ module.add_function("complex_list_function", &complex_list_function);
177 module.add_function("complex_function", &complex_function);
178+ module.add_function("complex_set_function", &complex_set_function);
179
180 exports->SetPrototype(module.create_prototype());
181 }
182
183=== modified file 'tests/functions/test.cpp'
184--- tests/functions/test.cpp 2015-10-09 10:43:41 +0000
185+++ tests/functions/test.cpp 2015-10-19 13:21:02 +0000
186@@ -81,4 +81,31 @@
187 EXPECT_FLOAT_EQ(result2[1], 3);
188 EXPECT_FLOAT_EQ(result2[2], 2.1);
189 EXPECT_FLOAT_EQ(result2[3], 0);
190+
191+ auto result3 = v8cpp::run_script<std::list<float>>(
192+ R"(
193+ var module = require("./test-functions-module");
194+ module.complex_list_function(4, "3", 2.1, false);
195+ )");
196+
197+ ASSERT_EQ(result3.size(), 4);
198+ EXPECT_FLOAT_EQ(result3.front(), 4);
199+ result3.pop_front();
200+ EXPECT_FLOAT_EQ(result3.front(), 3);
201+ result3.pop_front();
202+ EXPECT_FLOAT_EQ(result3.front(), 2.1);
203+ result3.pop_front();
204+ EXPECT_FLOAT_EQ(result3.front(), 0);
205+ result3.pop_front();
206+
207+ auto result4 = v8cpp::run_script<std::set<std::string>>(
208+ R"(
209+ var module = require("./test-functions-module");
210+ module.complex_set_function(4, "3", false);
211+ )");
212+
213+ ASSERT_EQ(result4.size(), 3);
214+ EXPECT_FLOAT_EQ(result4.count("4"), 1);
215+ EXPECT_FLOAT_EQ(result4.count("3"), 1);
216+ EXPECT_FLOAT_EQ(result4.count("0"), 1);
217 }
218
219=== modified file 'tests/functions/test.h'
220--- tests/functions/test.h 2015-10-09 10:43:41 +0000
221+++ tests/functions/test.h 2015-10-19 13:21:02 +0000
222@@ -82,3 +82,31 @@
223 num_list.push_back(forth);
224 return num_list;
225 }
226+
227+std::list<float> complex_list_function(int first, std::string const& second, float third, bool forth)
228+{
229+ EXPECT_EQ(first, 4);
230+ EXPECT_EQ(second, "3");
231+ EXPECT_FLOAT_EQ(third, 2.1);
232+ EXPECT_EQ(forth, false);
233+
234+ std::list<float> nums;
235+ nums.push_back(first);
236+ nums.push_back(stoi(second));
237+ nums.push_back(third);
238+ nums.push_back(forth);
239+ return nums;
240+}
241+
242+std::set<std::string> complex_set_function(int first, std::string const& second, bool third)
243+{
244+ EXPECT_EQ(first, 4);
245+ EXPECT_EQ(second, "3");
246+ EXPECT_EQ(third, false);
247+
248+ std::set<std::string> nums;
249+ nums.insert(std::to_string(first));
250+ nums.insert(second);
251+ nums.insert(std::to_string(third));
252+ return nums;
253+}

Subscribers

People subscribed via source and target branches