Merge lp:~abreu-alexandre/v8-cpp/call-to-v8-with-custom-receiver into lp:v8-cpp

Proposed by Alexandre Abreu
Status: Merged
Approved by: Marcus Tomlinson
Approved revision: 30
Merged at revision: 30
Proposed branch: lp:~abreu-alexandre/v8-cpp/call-to-v8-with-custom-receiver
Merge into: lp:v8-cpp
Diff against target: 122 lines (+55/-2)
4 files modified
src/call.h (+9/-2)
tests/functions/module.cpp (+7/-0)
tests/functions/test.cpp (+17/-0)
tests/functions/test.h (+22/-0)
To merge this branch: bzr merge lp:~abreu-alexandre/v8-cpp/call-to-v8-with-custom-receiver
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Approve
Review via email: mp+271701@code.launchpad.net

Commit message

Allow call to v8 be with a different object receiver than global

Description of the change

Allow call to v8 be with a different object receiver than global

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

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/call.h'
2--- src/call.h 2015-08-05 18:57:44 +0000
3+++ src/call.h 2015-09-18 18:43:09 +0000
4@@ -27,7 +27,7 @@
5
6 // Call a V8 function from C++
7 template <typename... Args>
8-v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func, Args... args)
9+v8::Local<v8::Value> call_v8_with_receiver(v8::Isolate* isolate, v8::Local<v8::Object> receiver, v8::Local<v8::Function> func, Args... args)
10 {
11 v8::EscapableHandleScope scope(isolate);
12
13@@ -36,9 +36,16 @@
14 // +1 for when arg_count == 0
15 v8::Local<v8::Value> v8_args[arg_count + 1] = {to_v8(isolate, args)...};
16
17- v8::Local<v8::Value> result = func->Call(isolate->GetCurrentContext()->Global(), arg_count, v8_args);
18+ v8::Local<v8::Value> result = func->Call(receiver, arg_count, v8_args);
19
20 return scope.Escape(result);
21 }
22
23+// Call a V8 function from C++
24+template <typename... Args>
25+v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func, Args... args)
26+{
27+ return call_v8_with_receiver(isolate, isolate->GetCurrentContext()->Global(), func, args...);
28+}
29+
30 } // namespace v8cpp
31
32=== modified file 'tests/functions/module.cpp'
33--- tests/functions/module.cpp 2015-08-05 18:57:44 +0000
34+++ tests/functions/module.cpp 2015-09-18 18:43:09 +0000
35@@ -31,12 +31,19 @@
36 v8cpp::Class<TestCaller> testcaller(isolate);
37 testcaller
38 .set_constructor<Local<Function>>()
39+ .add_method("get_shared", &TestCaller::get_shared)
40+ .add_method("call_me_with_shared", &TestCaller::call_me_with_shared)
41 .add_method("call_me", &TestCaller::call_me);
42
43+ // Prepare Shared binding
44+ v8cpp::Class<Shared> shared(isolate);
45+ shared.add_method("get_value", &Shared::get_value);
46+
47 // Prepare module
48 v8cpp::Module module(isolate);
49
50 module.add_class("TestCaller", testcaller);
51+ module.add_class("Shared", shared);
52 module.add_function("simple_function", &simple_function);
53 module.add_function("complex_function", &complex_function);
54
55
56=== modified file 'tests/functions/test.cpp'
57--- tests/functions/test.cpp 2015-07-08 09:29:21 +0000
58+++ tests/functions/test.cpp 2015-09-18 18:43:09 +0000
59@@ -43,6 +43,23 @@
60 EXPECT_EQ(callback_result, "hello world");
61 }
62
63+TEST(Test, call_to_js_with_self)
64+{
65+ auto callback_result = v8cpp::run_script<std::string>(
66+ R"(
67+ var module = require("./test-functions-module");
68+
69+ var caller = new module.TestCaller(function() {})
70+
71+ var s = caller.get_shared()
72+ caller.call_me_with_shared(s, function() {
73+ return this.get_value()
74+ });
75+ )");
76+
77+ EXPECT_EQ(callback_result, "hello");
78+}
79+
80 TEST(Test, call_from_js)
81 {
82 auto result = v8cpp::run_script<std::string>(
83
84=== modified file 'tests/functions/test.h'
85--- tests/functions/test.h 2015-07-08 06:20:26 +0000
86+++ tests/functions/test.h 2015-09-18 18:43:09 +0000
87@@ -22,6 +22,15 @@
88
89 #include <v8-cpp.h>
90
91+class Shared
92+{
93+public:
94+ std::string get_value() const
95+ {
96+ return std::string("hello");
97+ }
98+};
99+
100 class TestCaller
101 {
102 public:
103@@ -37,6 +46,19 @@
104 return v8cpp::from_v8<std::string>(v8::Isolate::GetCurrent(), result);
105 }
106
107+ std::shared_ptr<Shared> get_shared()
108+ {
109+ auto s = std::shared_ptr<Shared>(new Shared());
110+ return s;
111+ }
112+
113+ v8::Local<v8::Value>
114+ call_me_with_shared(v8::Local<v8::Object> receiver,
115+ v8::Local<v8::Function> to_be_called_with_shared)
116+ {
117+ return v8cpp::call_v8_with_receiver(v8::Isolate::GetCurrent(), receiver, to_be_called_with_shared);
118+ }
119+
120 private:
121 v8::Persistent<v8::Function> cb_;
122 };

Subscribers

People subscribed via source and target branches