Skip to content

Commit 1ab6d8e

Browse files
committed
do not use deprecated V8 API functions
Updated implementation to V8 version 5.9: - use `v8::Local` instead of `v8::Handle` - use `v8::Global` instead of `v8::PersistentHandle` - use functions returning `MaybeLocal` Updated tests and examples. Updated V8 versions in Travis script and Makefile, switched to `xenial` image (although it hasn't been officially released yet) Related to pmed#50 and pmed#66
1 parent 365e81a commit 1ab6d8e

33 files changed

+226
-258
lines changed

.travis.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
language: c++
22

33
sudo: required
4-
dist: trusty
4+
dist: xenial
55

66
compiler:
77
- gcc
88
- clang
99

1010
env:
11-
- V8_VERSION=4.10
12-
- V8_VERSION=5.2
13-
- V8_VERSION=5.4
11+
- V8_VERSION=5.9
12+
- V8_VERSION=6.0
13+
- V8_VERSION=6.4
1414

1515
before_install:
1616
- sudo add-apt-repository ppa:pinepain/libv8-"$V8_VERSION" -y
1717
- sudo apt-get update -q
1818
- sudo apt-get install libv8-"$V8_VERSION"-dev -y
1919

2020
install:
21-
- if [ "$CXX" == "clang++" ]; then export CXXFLAGS="-stdlib=libstdc++"; fi
21+
# - if [ "$CXX" == "clang++" ]; then export CXXFLAGS="-stdlib=libstdc++"; fi
2222

2323
script: make
2424

25-
after_success: LD_LIBRARY_PATH=. ./v8pp_test -v --run-tests test/console.js test/file.js
25+
after_success: LD_LIBRARY_PATH=.:/opt/libv8-${V8_VERSION}/lib ./v8pp_test -v --run-tests test/console.js test/file.js

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
CXX ?= c++
2-
CXXFLAGS += -Wall -Wextra -std=c++11 -fPIC -DV8PP_ISOLATE_DATA_SLOT=0
2+
CXXFLAGS += -Wall -Wextra -std=c++11 -fPIC -fno-rtti -DV8PP_ISOLATE_DATA_SLOT=0
33
LDFLAGS += -shared
44
AR = ar
55
ARFLAGS = rcs
66

7-
INCLUDES = -I. -I./v8pp -isystem./v8/include -isystem./v8 -isystem/usr -isystem/usr/lib
8-
LIBS = -L./v8/lib -lv8 -lv8_libplatform -lv8_base -lv8_libbase -licui18n -licuuc -L. -Wl,-whole-archive -lv8pp -Wl,-no-whole-archive -ldl -lpthread
7+
INCLUDES = -I. -I./v8pp -isystem./v8/include -isystem./v8 -isystem/usr -isystem/usr/lib -isystem/opt/libv8-${V8_VERSION}/include
8+
LIBS = -L./v8/lib -L/opt/libv8-${V8_VERSION}/lib -lv8 -lv8_libplatform -lv8_libbase -licui18n -licuuc -L. -Wl,-whole-archive -lv8pp -Wl,-no-whole-archive -ldl -lpthread
99

1010
.cpp.o:
1111
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

examples/01 hello world/hello.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ char const* Method() {
1818
void init(Handle<Object> exports) {
1919
v8pp::module addon(Isolate::GetCurrent());
2020
addon.set("hello", &Method);
21-
exports->SetPrototype(addon.new_instance());
21+
exports->SetPrototype(Isolate::GetCurrent()->GetCurrentContext(), addon.new_instance());
2222
}
2323

24-
NODE_MODULE(addon, init)
24+
NODE_MODULE(addon, init)

examples/02 arguments/addon.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ double Add(double arg1, double arg2) {
1818
void Init(Handle<Object> exports) {
1919
v8pp::module addon(Isolate::GetCurrent());
2020
addon.set("add", &Add);
21-
exports->SetPrototype(addon.new_instance());
21+
exports->SetPrototype(Isolate::GetCurrent()->GetCurrentContext(), addon.new_instance());
2222
}
2323

24-
NODE_MODULE(addon, Init)
24+
NODE_MODULE(addon, Init)

examples/06 wrapped objects/myobject.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void MyObject::Init(Handle<Object> exports) {
2828
v8pp::module addon(isolate);
2929
addon.set("MyObject", MyObject_class);
3030

31-
exports->SetPrototype(addon.new_instance());
31+
exports->SetPrototype(isolate->GetCurrentContext(), addon.new_instance());
3232
node::AtExit([](void* param)
3333
{
3434
v8pp::cleanup(static_cast<Isolate*>(param));

examples/08 passing wrapped objects/addon.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void InitAll(Handle<Object> exports) {
3434
addon.set("MyObject", MyObject_class);
3535
addon.set("createObject", &CreateObject);
3636
addon.set("add", &Add);
37-
exports->SetPrototype(addon.new_instance());
37+
exports->SetPrototype(isolate->GetCurrentContext(), addon.new_instance());
3838
node::AtExit([](void* param)
3939
{
4040
v8pp::cleanup(static_cast<Isolate*>(param));

plugins/console.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void log(v8::FunctionCallbackInfo<v8::Value> const& args)
2525
std::cout << std::endl;
2626
}
2727

28-
v8::Handle<v8::Value> init(v8::Isolate* isolate)
28+
v8::Local<v8::Value> init(v8::Isolate* isolate)
2929
{
3030
v8pp::module m(isolate);
3131
m.set("log", &log);

plugins/file.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class file_reader : public file_base
7777
return stream_.good();
7878
}
7979

80-
v8::Handle<v8::Value> getline(v8::Isolate* isolate)
80+
v8::Local<v8::Value> getline(v8::Isolate* isolate)
8181
{
8282
if ( stream_.good() && ! stream_.eof())
8383
{
@@ -92,7 +92,7 @@ class file_reader : public file_base
9292
}
9393
};
9494

95-
v8::Handle<v8::Value> init(v8::Isolate* isolate)
95+
v8::Local<v8::Value> init(v8::Isolate* isolate)
9696
{
9797
v8::EscapableHandleScope scope(isolate);
9898

test/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ int main(int argc, char const * argv[])
109109
}
110110
}
111111

112-
v8::V8::InitializeICU();
113-
//v8::V8::InitializeExternalStartupData(argv[0]);
112+
//v8::V8::InitializeICU();
113+
v8::V8::InitializeExternalStartupData(argv[0]);
114114
std::unique_ptr<v8::Platform> platform(v8::platform::CreateDefaultPlatform());
115115
v8::V8::InitializePlatform(platform.get());
116116
v8::V8::Initialize();

test/test.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ T run_script(v8pp::context& context, std::string const& source)
116116

117117
v8::HandleScope scope(isolate);
118118
v8::TryCatch try_catch;
119-
v8::Handle<v8::Value> result = context.run_script(source);
119+
v8::Local<v8::Value> result = context.run_script(source);
120120
if (try_catch.HasCaught())
121121
{
122122
std::string const msg = v8pp::from_v8<std::string>(isolate,
123-
try_catch.Exception()->ToString());
123+
try_catch.Exception()->ToString(isolate->GetCurrentContext()).ToLocalChecked());
124124
throw std::runtime_error(msg);
125125
}
126126
return v8pp::from_v8<T>(isolate, result);

test/test_call_v8.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ void test_call_v8()
2222

2323
v8::Isolate* isolate = context.isolate();
2424
v8::HandleScope scope(isolate);
25-
v8::Handle<v8::Function> fun = v8::Function::New(isolate, v8_arg_count);
25+
v8::Local<v8::Function> fun = v8::Function::New(isolate->GetCurrentContext(), v8_arg_count).ToLocalChecked();
2626

2727
check_eq("no args",
28-
v8pp::call_v8(isolate, fun, fun)->Int32Value(), 0);
28+
v8pp::call_v8(isolate, fun, fun)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 0);
2929
check_eq("1 arg",
30-
v8pp::call_v8(isolate, fun, fun, 1)->Int32Value(), 1);
30+
v8pp::call_v8(isolate, fun, fun, 1)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 1);
3131
check_eq("2 args",
32-
v8pp::call_v8(isolate, fun, fun, true, 2.2)->Int32Value(), 2);
32+
v8pp::call_v8(isolate, fun, fun, true, 2.2)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 2);
3333
check_eq("3 args",
34-
v8pp::call_v8(isolate, fun, fun, 1, true, "abc")->Int32Value(), 3);
34+
v8pp::call_v8(isolate, fun, fun, 1, true, "abc")->Int32Value(isolate->GetCurrentContext()).ToChecked(), 3);
3535
}

test/test_class.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct factory<Y, v8pp::shared_ptr_traits>
7676
template<typename Traits>
7777
static int extern_fun(v8::FunctionCallbackInfo<v8::Value> const& args)
7878
{
79-
int x = args[0]->Int32Value();
79+
int x = args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
8080
auto self = v8pp::class_<X, Traits>::unwrap_object(args.GetIsolate(), args.This());
8181
if (self) x += self->var;
8282
return x;
@@ -156,18 +156,18 @@ void test_class_()
156156

157157
auto y1 = v8pp::factory<Y, Traits>::create(isolate, -1);
158158

159-
v8::Handle<v8::Object> y1_obj =
159+
v8::Local<v8::Object> y1_obj =
160160
v8pp::class_<Y, Traits>::reference_external(context.isolate(), y1);
161161
check("y1", v8pp::from_v8<decltype(y1)>(isolate, y1_obj) == y1);
162162
check("y1_obj", v8pp::to_v8(isolate, y1) == y1_obj);
163163

164164
auto y2 = v8pp::factory<Y, Traits>::create(isolate, -2);
165-
v8::Handle<v8::Object> y2_obj =
165+
v8::Local<v8::Object> y2_obj =
166166
v8pp::class_<Y, Traits>::import_external(context.isolate(), y2);
167167
check("y2", v8pp::from_v8<decltype(y2)>(isolate, y2_obj) == y2);
168168
check("y2_obj", v8pp::to_v8(isolate, y2) == y2_obj);
169169

170-
v8::Handle<v8::Object> y3_obj =
170+
v8::Local<v8::Object> y3_obj =
171171
v8pp::class_<Y, Traits>::create_object(context.isolate(), -3);
172172
auto y3 = v8pp::class_<Y, Traits>::unwrap_object(isolate, y3_obj);
173173
check("y3", v8pp::from_v8<decltype(y3)>(isolate, y3_obj) == y3);

test/test_context.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ void test_context()
1515
v8pp::context context;
1616

1717
v8::HandleScope scope(context.isolate());
18-
int const r = context.run_script("42")->Int32Value();
18+
int const r = context.run_script("42")->Int32Value(context.isolate()->GetCurrentContext()).ToChecked();
1919
check_eq("run_script", r, 42);
2020
}

test/test_convert.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ template<>
7575
struct convert<person>
7676
{
7777
using from_type = person;
78-
using to_type = v8::Handle<v8::Object>;
78+
using to_type = v8::Local<v8::Object>;
7979

80-
static bool is_valid(v8::Isolate*, v8::Handle<v8::Value> value)
80+
static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
8181
{
8282
return !value.IsEmpty() && value->IsObject();
8383
}
@@ -86,16 +86,16 @@ struct convert<person>
8686
{
8787
v8::EscapableHandleScope scope(isolate);
8888
v8::Local<v8::Object> obj = v8::Object::New(isolate);
89-
obj->Set(v8pp::to_v8(isolate, "name"), v8pp::to_v8(isolate, p.name));
90-
obj->Set(v8pp::to_v8(isolate, "age"), v8pp::to_v8(isolate, p.age));
89+
obj->Set(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "name"), v8pp::to_v8(isolate, p.name));
90+
obj->Set(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "age"), v8pp::to_v8(isolate, p.age));
9191
/* Simpler after #include <v8pp/object.hpp>
9292
set_option(isolate, obj, "name", p.name);
9393
set_option(isolate, obj, "age", p.age);
9494
*/
9595
return scope.Escape(obj);
9696
}
9797

98-
static from_type from_v8(v8::Isolate* isolate, v8::Handle<v8::Value> value)
98+
static from_type from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
9999
{
100100
if (!is_valid(isolate, value))
101101
{
@@ -107,9 +107,9 @@ struct convert<person>
107107

108108
person result;
109109
result.name = v8pp::from_v8<std::string>(isolate,
110-
obj->Get(v8pp::to_v8(isolate, "name")));
110+
obj->Get(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "name")).ToLocalChecked());
111111
result.age = v8pp::from_v8<int>(isolate,
112-
obj->Get(v8pp::to_v8(isolate, "age")));
112+
obj->Get(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "age")).ToLocalChecked());
113113

114114
/* Simpler after #include <v8pp/object.hpp>
115115
get_option(isolate, obj, "name", result.name);

test/test_factory.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void test_factory()
128128

129129
v8::Isolate* isolate = context.isolate();
130130
v8::HandleScope scope(isolate);
131-
v8::Handle<v8::Function> fun = v8::Function::New(isolate, test_factories);
131+
v8::Local<v8::Function> fun = v8::Function::New(isolate->GetCurrentContext(), test_factories).ToLocalChecked();
132132

133133
v8pp::call_v8(isolate, fun, fun);
134134
check_eq("all ctors called", ctor_types, 0x0F);

test/test_json.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_json()
3131
str = v8pp::json_str(isolate, v);
3232
v = v8pp::json_parse(isolate, "42");
3333
check_eq("int string", str, "42");
34-
check_eq("int parse", v->Int32Value(), 42);
34+
check_eq("int parse", v->Int32Value(isolate->GetCurrentContext()).ToChecked(), 42);
3535

3636
v8::Local<v8::Object> obj = v8::Object::New(isolate);
3737
v8pp::set_option(isolate, obj, "x", 1);
@@ -44,7 +44,7 @@ void test_json()
4444
check_eq("object parse", v8pp::json_str(isolate, v), str);
4545

4646
v8::Local<v8::Array> arr = v8::Array::New(isolate, 1);
47-
arr->Set(0, obj);
47+
arr->Set(isolate->GetCurrentContext(), 0, obj);
4848

4949
str = v8pp::json_str(isolate, arr);
5050
v = v8pp::json_parse(isolate, str);

test/test_ptr_traits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void test_ptr_traits()
181181
v8::Isolate* isolate = context.isolate();
182182
v8::HandleScope scope(isolate);
183183

184-
v8::Handle<v8::Function> fun = v8::Function::New(isolate, test_create_destroy);
184+
v8::Local<v8::Function> fun = v8::Function::New(isolate->GetCurrentContext(), test_create_destroy).ToLocalChecked();
185185
v8pp::call_v8(isolate, fun, fun);
186186
check_eq("all ctors called", ctor_types, 0x0F);
187187
check_eq("ctor count", ctor_count, 6);

test/test_throw_ex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace {
1313

1414
void test(v8pp::context& context, std::string const& type,
15-
v8::Local<v8::Value>(*exception_ctor)(v8::Handle<v8::String>))
15+
v8::Local<v8::Value>(*exception_ctor)(v8::Local<v8::String>))
1616
{
1717
v8::Isolate* isolate = context.isolate();
1818

v8pp/call_from_v8.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct call_from_v8_traits
5858
>::type;
5959

6060
template<size_t Index, typename Traits>
61-
static decltype(arg_convert<Index, Traits>::from_v8(std::declval<v8::Isolate*>(), std::declval<v8::Handle<v8::Value>>()))
61+
static decltype(arg_convert<Index, Traits>::from_v8(std::declval<v8::Isolate*>(), std::declval<v8::Local<v8::Value>>()))
6262
arg_from_v8(v8::FunctionCallbackInfo<v8::Value> const& args)
6363
{
6464
return arg_convert<Index, Traits>::from_v8(args.GetIsolate(), args[Index - Offset]);

v8pp/call_v8.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ namespace v8pp {
2121
/// @param recv V8 object used as `this` in the function
2222
/// @param args... C++ arguments to convert to JS arguments using to_v8
2323
template<typename ...Args>
24-
v8::Handle<v8::Value> call_v8(v8::Isolate* isolate, v8::Handle<v8::Function> func,
25-
v8::Handle<v8::Value> recv, Args&&... args)
24+
v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func,
25+
v8::Local<v8::Value> recv, Args&&... args)
2626
{
2727
v8::EscapableHandleScope scope(isolate);
2828

2929
int const arg_count = sizeof...(Args);
3030
// +1 to allocate array for arg_count == 0
31-
v8::Handle<v8::Value> v8_args[arg_count + 1] =
31+
v8::Local<v8::Value> v8_args[arg_count + 1] =
3232
{
3333
to_v8(isolate, std::forward<Args>(args))...
3434
};
3535

36-
v8::Local<v8::Value> result = func->Call(recv, arg_count, v8_args);
36+
v8::Local<v8::Value> result = func->Call(isolate->GetCurrentContext(), recv, arg_count, v8_args).ToLocalChecked();
3737

3838
return scope.Escape(result);
3939
}

0 commit comments

Comments
 (0)