Merge lp:~unity-team/v8-cpp/lp-1504309 into lp:v8-cpp

Proposed by Marcus Tomlinson
Status: Merged
Merged at revision: 34
Proposed branch: lp:~unity-team/v8-cpp/lp-1504309
Merge into: lp:v8-cpp
Diff against target: 310 lines (+125/-60)
8 files modified
src/internal/class.h (+13/-2)
src/internal/convert.h (+2/-2)
tests/errors/module.cpp (+2/-0)
tests/errors/test.cpp (+28/-0)
tests/errors/test.h (+12/-0)
tests/objects/module.cpp (+3/-2)
tests/objects/test.cpp (+46/-46)
tests/objects/test.h (+19/-8)
To merge this branch: bzr merge lp:~unity-team/v8-cpp/lp-1504309
Reviewer Review Type Date Requested Status
Alexandre Abreu (community) Approve
Review via email: mp+273964@code.launchpad.net

Commit message

Fixed bug #1504309 and updated the appropriate tests

To post a comment you must log in.
Revision history for this message
Alexandre Abreu (abreu-alexandre) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/internal/class.h'
2--- src/internal/class.h 2015-10-07 14:14:31 +0000
3+++ src/internal/class.h 2015-10-09 10:46:13 +0000
4@@ -170,6 +170,12 @@
5 {
6 v8::HandleScope scope(isolate_);
7
8+ if (value->IsNull())
9+ {
10+ static T nullsptr(nullptr);
11+ return &nullsptr;
12+ }
13+
14 while (value->IsObject())
15 {
16 v8::Local<v8::Object> object = value->ToObject();
17@@ -184,7 +190,7 @@
18 }
19 value = object->GetPrototype();
20 }
21- return nullptr;
22+ throw std::invalid_argument("expected an exported object");
23 }
24
25 template <typename O>
26@@ -192,6 +198,11 @@
27 {
28 v8::HandleScope scope(isolate_);
29
30+ if (value->IsNull())
31+ {
32+ return nullptr;
33+ }
34+
35 while (value->IsObject())
36 {
37 v8::Local<v8::Object> object = value->ToObject();
38@@ -206,7 +217,7 @@
39 }
40 value = object->GetPrototype();
41 }
42- return nullptr;
43+ throw std::invalid_argument("expected an exported object");
44 }
45
46 template <typename P>
47
48=== modified file 'src/internal/convert.h'
49--- src/internal/convert.h 2015-10-07 14:14:31 +0000
50+++ src/internal/convert.h 2015-10-09 10:46:13 +0000
51@@ -405,7 +405,7 @@
52
53 static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
54 {
55- return !value.IsEmpty() && value->IsObject();
56+ return !value.IsEmpty() && (value->IsObject() || value->IsNull());
57 }
58
59 static FromType from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
60@@ -445,7 +445,7 @@
61
62 static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
63 {
64- return !value.IsEmpty() && value->IsObject();
65+ return !value.IsEmpty() && (value->IsObject() || value->IsNull());
66 }
67
68 static FromType from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
69
70=== modified file 'tests/errors/module.cpp'
71--- tests/errors/module.cpp 2015-08-05 18:57:44 +0000
72+++ tests/errors/module.cpp 2015-10-09 10:46:13 +0000
73@@ -32,6 +32,8 @@
74 testclass
75 .set_constructor<int, int>()
76 .add_method("i", &TestClass::i)
77+ .add_method("expect_sptr", &TestClass::expect_sptr)
78+ .add_method("expect_obj", &TestClass::expect_obj)
79 .add_method("throw_ex", &TestClass::throw_ex);
80
81 // Prepare module
82
83=== modified file 'tests/errors/test.cpp'
84--- tests/errors/test.cpp 2015-07-16 14:25:39 +0000
85+++ tests/errors/test.cpp 2015-10-09 10:46:13 +0000
86@@ -117,6 +117,34 @@
87 {
88 EXPECT_STREQ(e.what(), "Uncaught Error: argument count does not match the corresponding C++ function definition");
89 }
90+
91+ try
92+ {
93+ v8cpp::run_script(
94+ R"(
95+ var module = require("./test-errors-module");
96+ var test_object = new module.TestClass(1, 2);
97+ test_object.expect_sptr(function x(){});
98+ )");
99+ }
100+ catch (std::exception const& e)
101+ {
102+ EXPECT_STREQ(e.what(), "Uncaught Error: expected an exported object");
103+ }
104+
105+ try
106+ {
107+ v8cpp::run_script(
108+ R"(
109+ var module = require("./test-errors-module");
110+ var test_object = new module.TestClass(1, 2);
111+ test_object.expect_obj(null);
112+ )");
113+ }
114+ catch (std::exception const& e)
115+ {
116+ EXPECT_STREQ(e.what(), "Uncaught Error: expected an exported object");
117+ }
118 }
119
120 TEST(Test, conversion_errors)
121
122=== modified file 'tests/errors/test.h'
123--- tests/errors/test.h 2015-07-08 06:20:26 +0000
124+++ tests/errors/test.h 2015-10-09 10:46:13 +0000
125@@ -20,6 +20,8 @@
126
127 #include <gtest/gtest.h>
128
129+#include <memory>
130+
131 class TestClass
132 {
133 public:
134@@ -33,6 +35,16 @@
135 return i_;
136 }
137
138+ int expect_sptr(std::shared_ptr<TestClass> x) const
139+ {
140+ return 1;
141+ }
142+
143+ int expect_obj(TestClass x) const
144+ {
145+ return 1;
146+ }
147+
148 void throw_ex()
149 {
150 throw std::runtime_error("BOOM!");
151
152=== modified file 'tests/objects/module.cpp'
153--- tests/objects/module.cpp 2015-09-01 09:33:34 +0000
154+++ tests/objects/module.cpp 2015-10-09 10:46:13 +0000
155@@ -37,8 +37,9 @@
156 .add_method("embedded_class_ptr", &TestClass::embedded_class_ptr)
157 .add_method("embedded_class_ref", &TestClass::embedded_class_ref)
158 .add_method("embedded_class_copy", &TestClass::embedded_class_copy)
159- .add_method("replace_i", &TestClass::replace_i)
160- .add_method("add_i", &TestClass::add_i);
161+ .add_method("remove_ptr", &TestClass::remove_ptr)
162+ .add_method("remove_sptr", &TestClass::remove_sptr)
163+ .add_method("add_ref", &TestClass::add_ref);
164
165 // Prepare EmbeddedTestClass binding
166 v8cpp::Class<EmbeddedTestClass> embeddedtestclass(isolate);
167
168=== modified file 'tests/objects/test.cpp'
169--- tests/objects/test.cpp 2015-09-01 09:33:34 +0000
170+++ tests/objects/test.cpp 2015-10-09 10:46:13 +0000
171@@ -99,55 +99,55 @@
172 var test_object = module.new_TestClass(1, 2);
173 var test_object2 = module.new_TestClass(1, 2);
174
175- test_object.replace_i(test_object2.embedded_class_copy());
176- test_object;
177- )");
178+ test_object.remove_ptr(test_object2.embedded_class_copy());
179+ test_object;
180+ )");
181+
182+ EXPECT_EQ(test_object.i(), 4);
183+
184+ test_object = v8cpp::run_script<TestClass>(
185+ R"(
186+ var module = require("./test-objects-module");
187+ var test_object = module.new_TestClass(1, 2);
188+ var test_object2 = module.new_TestClass(1, 2);
189+
190+ test_object.remove_ptr(null);
191+ test_object;
192+ )");
193+
194+ EXPECT_EQ(test_object.i(), 3);
195+
196+ test_object = v8cpp::run_script<TestClass>(
197+ R"(
198+ var module = require("./test-objects-module");
199+ var test_object = module.new_TestClass(1, 2);
200+ var test_object2 = module.new_TestClass(1, 2);
201+
202+ test_object.remove_sptr(null);
203+ test_object;
204+ )");
205+
206+ EXPECT_EQ(test_object.i(), 3);
207
208 auto result = v8cpp::run_script<int>(
209 R"(
210 var module = require("./test-objects-module");
211 var test_object = module.new_TestClass(1, 2);
212-
213- var embedded_object = test_object.embedded_class_uptr();
214- embedded_object.i();
215- )");
216-
217- EXPECT_EQ(result, -1);
218-
219- auto result2 = v8cpp::run_script<int>(
220- R"(
221- var module = require("./test-objects-module");
222- var test_object = module.new_TestClass(1, 2);
223-
224- var embedded_object = test_object.embedded_class_sptr();
225- embedded_object.i();
226- )");
227-
228- EXPECT_EQ(result2, -1);
229-
230- EXPECT_EQ(test_object.i(), -1);
231-
232- auto result3 = v8cpp::run_script<int>(
233- R"(
234- var module = require("./test-objects-module");
235- var test_object = module.new_TestClass(1, 2);
236- var test_object2 = module.new_TestClass(1, 2);
237-
238- test_object.add_i(test_object.embedded_class_ptr(),
239- test_object2.embedded_class_ref());
240- )");
241-
242- EXPECT_EQ(result3, -2);
243-
244- auto result4 = v8cpp::run_script<int>(
245- R"(
246- var module = require("./test-objects-module");
247- var test_object = module.new_TestClass(1, 2);
248- var test_object2 = module.new_TestClass(1, 2);
249-
250- test_object.add_i(test_object.embedded_class_sptr(),
251- test_object2.embedded_class_uptr());
252- )");
253-
254- EXPECT_EQ(result4, -2);
255+ var test_object2 = module.new_TestClass(1, 2);
256+
257+ test_object.add_ref(test_object2.embedded_class_ref());
258+ )");
259+
260+ EXPECT_EQ(result, 2);
261+
262+ result = v8cpp::run_script<int>(
263+ R"(
264+ var module = require("./test-objects-module");
265+ var test_object = module.new_TestClass(1, 2);
266+ var test_object2 = module.new_TestClass(1, 2);
267+
268+ test_object.add_ref(test_object2.embedded_class_uptr());
269+ )");
270+
271+ EXPECT_EQ(result, 2);
272 }
273
274=== modified file 'tests/objects/test.h'
275--- tests/objects/test.h 2015-09-01 09:33:34 +0000
276+++ tests/objects/test.h 2015-10-09 10:46:13 +0000
277@@ -82,14 +82,25 @@
278 return *embedded_class_;
279 }
280
281- void replace_i(EmbeddedTestClass const& other)
282- {
283- i_ = other.i();
284- }
285-
286- int add_i(EmbeddedTestClass const& other, EmbeddedTestClass const& other2)
287- {
288- return other.i() + other2.i();
289+ void remove_ptr(EmbeddedTestClass* other)
290+ {
291+ if (other)
292+ {
293+ i_ -= other->i();
294+ }
295+ }
296+
297+ void remove_sptr(std::shared_ptr<EmbeddedTestClass> const& other)
298+ {
299+ if (other)
300+ {
301+ i_ -= other->i();
302+ }
303+ }
304+
305+ int add_ref(EmbeddedTestClass const& other)
306+ {
307+ return i_ += other.i();
308 }
309
310 private:

Subscribers

People subscribed via source and target branches

to all changes: