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
=== modified file 'src/call.h'
--- src/call.h 2015-08-05 18:57:44 +0000
+++ src/call.h 2015-09-18 18:43:09 +0000
@@ -27,7 +27,7 @@
2727
28// Call a V8 function from C++28// Call a V8 function from C++
29template <typename... Args>29template <typename... Args>
30v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func, Args... args)30v8::Local<v8::Value> call_v8_with_receiver(v8::Isolate* isolate, v8::Local<v8::Object> receiver, v8::Local<v8::Function> func, Args... args)
31{31{
32 v8::EscapableHandleScope scope(isolate);32 v8::EscapableHandleScope scope(isolate);
3333
@@ -36,9 +36,16 @@
36 // +1 for when arg_count == 036 // +1 for when arg_count == 0
37 v8::Local<v8::Value> v8_args[arg_count + 1] = {to_v8(isolate, args)...};37 v8::Local<v8::Value> v8_args[arg_count + 1] = {to_v8(isolate, args)...};
3838
39 v8::Local<v8::Value> result = func->Call(isolate->GetCurrentContext()->Global(), arg_count, v8_args);39 v8::Local<v8::Value> result = func->Call(receiver, arg_count, v8_args);
4040
41 return scope.Escape(result);41 return scope.Escape(result);
42}42}
4343
44// Call a V8 function from C++
45template <typename... Args>
46v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func, Args... args)
47{
48 return call_v8_with_receiver(isolate, isolate->GetCurrentContext()->Global(), func, args...);
49}
50
44} // namespace v8cpp51} // namespace v8cpp
4552
=== modified file 'tests/functions/module.cpp'
--- tests/functions/module.cpp 2015-08-05 18:57:44 +0000
+++ tests/functions/module.cpp 2015-09-18 18:43:09 +0000
@@ -31,12 +31,19 @@
31 v8cpp::Class<TestCaller> testcaller(isolate);31 v8cpp::Class<TestCaller> testcaller(isolate);
32 testcaller32 testcaller
33 .set_constructor<Local<Function>>()33 .set_constructor<Local<Function>>()
34 .add_method("get_shared", &TestCaller::get_shared)
35 .add_method("call_me_with_shared", &TestCaller::call_me_with_shared)
34 .add_method("call_me", &TestCaller::call_me);36 .add_method("call_me", &TestCaller::call_me);
3537
38 // Prepare Shared binding
39 v8cpp::Class<Shared> shared(isolate);
40 shared.add_method("get_value", &Shared::get_value);
41
36 // Prepare module42 // Prepare module
37 v8cpp::Module module(isolate);43 v8cpp::Module module(isolate);
3844
39 module.add_class("TestCaller", testcaller);45 module.add_class("TestCaller", testcaller);
46 module.add_class("Shared", shared);
40 module.add_function("simple_function", &simple_function);47 module.add_function("simple_function", &simple_function);
41 module.add_function("complex_function", &complex_function);48 module.add_function("complex_function", &complex_function);
4249
4350
=== modified file 'tests/functions/test.cpp'
--- tests/functions/test.cpp 2015-07-08 09:29:21 +0000
+++ tests/functions/test.cpp 2015-09-18 18:43:09 +0000
@@ -43,6 +43,23 @@
43 EXPECT_EQ(callback_result, "hello world");43 EXPECT_EQ(callback_result, "hello world");
44}44}
4545
46TEST(Test, call_to_js_with_self)
47{
48 auto callback_result = v8cpp::run_script<std::string>(
49 R"(
50 var module = require("./test-functions-module");
51
52 var caller = new module.TestCaller(function() {})
53
54 var s = caller.get_shared()
55 caller.call_me_with_shared(s, function() {
56 return this.get_value()
57 });
58 )");
59
60 EXPECT_EQ(callback_result, "hello");
61}
62
46TEST(Test, call_from_js)63TEST(Test, call_from_js)
47{64{
48 auto result = v8cpp::run_script<std::string>(65 auto result = v8cpp::run_script<std::string>(
4966
=== modified file 'tests/functions/test.h'
--- tests/functions/test.h 2015-07-08 06:20:26 +0000
+++ tests/functions/test.h 2015-09-18 18:43:09 +0000
@@ -22,6 +22,15 @@
2222
23#include <v8-cpp.h>23#include <v8-cpp.h>
2424
25class Shared
26{
27public:
28 std::string get_value() const
29 {
30 return std::string("hello");
31 }
32};
33
25class TestCaller34class TestCaller
26{35{
27public:36public:
@@ -37,6 +46,19 @@
37 return v8cpp::from_v8<std::string>(v8::Isolate::GetCurrent(), result);46 return v8cpp::from_v8<std::string>(v8::Isolate::GetCurrent(), result);
38 }47 }
3948
49 std::shared_ptr<Shared> get_shared()
50 {
51 auto s = std::shared_ptr<Shared>(new Shared());
52 return s;
53 }
54
55 v8::Local<v8::Value>
56 call_me_with_shared(v8::Local<v8::Object> receiver,
57 v8::Local<v8::Function> to_be_called_with_shared)
58 {
59 return v8cpp::call_v8_with_receiver(v8::Isolate::GetCurrent(), receiver, to_be_called_with_shared);
60 }
61
40private:62private:
41 v8::Persistent<v8::Function> cb_;63 v8::Persistent<v8::Function> cb_;
42};64};

Subscribers

People subscribed via source and target branches