Skip to content

Commit

Permalink
do not use deprecated V8 API functions
Browse files Browse the repository at this point in the history
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 V8 versions in Travis script and Makefile

Related to #50 and #66
  • Loading branch information
pmed committed Feb 10, 2018
1 parent 365e81a commit c56a717
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 251 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ compiler:
- clang

env:
- V8_VERSION=4.10
- V8_VERSION=5.2
- V8_VERSION=5.4
- V8_VERSION=5.9
- V8_VERSION=6.0
- V8_VERSION=6.4

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

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

script: make

after_success: LD_LIBRARY_PATH=. ./v8pp_test -v --run-tests test/console.js test/file.js
after_success: LD_LIBRARY_PATH=.:/opt/libv8-${V8_VERSION}/lib ./v8pp_test -v --run-tests test/console.js test/file.js
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CXX ?= c++
CXXFLAGS += -Wall -Wextra -std=c++11 -fPIC -DV8PP_ISOLATE_DATA_SLOT=0
CXXFLAGS += -Wall -Wextra -std=c++11 -fPIC -fno-rtti -DV8PP_ISOLATE_DATA_SLOT=0
LDFLAGS += -shared
AR = ar
ARFLAGS = rcs

INCLUDES = -I. -I./v8pp -isystem./v8/include -isystem./v8 -isystem/usr -isystem/usr/lib
LIBS = -L./v8/lib -lv8 -lv8_libplatform -lv8_base -lv8_libbase -licui18n -licuuc -L. -Wl,-whole-archive -lv8pp -Wl,-no-whole-archive -ldl -lpthread
INCLUDES = -I. -I./v8pp -isystem./v8/include -isystem./v8 -isystem/usr -isystem/usr/lib -isystem/opt/libv8-${V8_VERSION}/include
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

.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
Expand Down
2 changes: 1 addition & 1 deletion plugins/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void log(v8::FunctionCallbackInfo<v8::Value> const& args)
std::cout << std::endl;
}

v8::Handle<v8::Value> init(v8::Isolate* isolate)
v8::Local<v8::Value> init(v8::Isolate* isolate)
{
v8pp::module m(isolate);
m.set("log", &log);
Expand Down
4 changes: 2 additions & 2 deletions plugins/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class file_reader : public file_base
return stream_.good();
}

v8::Handle<v8::Value> getline(v8::Isolate* isolate)
v8::Local<v8::Value> getline(v8::Isolate* isolate)
{
if ( stream_.good() && ! stream_.eof())
{
Expand All @@ -92,7 +92,7 @@ class file_reader : public file_base
}
};

v8::Handle<v8::Value> init(v8::Isolate* isolate)
v8::Local<v8::Value> init(v8::Isolate* isolate)
{
v8::EscapableHandleScope scope(isolate);

Expand Down
4 changes: 2 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ int main(int argc, char const * argv[])
}
}

v8::V8::InitializeICU();
//v8::V8::InitializeExternalStartupData(argv[0]);
//v8::V8::InitializeICU();
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform(v8::platform::CreateDefaultPlatform());
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
Expand Down
4 changes: 2 additions & 2 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ T run_script(v8pp::context& context, std::string const& source)

v8::HandleScope scope(isolate);
v8::TryCatch try_catch;
v8::Handle<v8::Value> result = context.run_script(source);
v8::Local<v8::Value> result = context.run_script(source);
if (try_catch.HasCaught())
{
std::string const msg = v8pp::from_v8<std::string>(isolate,
try_catch.Exception()->ToString());
try_catch.Exception()->ToString(isolate->GetCurrentContext()).ToLocalChecked());
throw std::runtime_error(msg);
}
return v8pp::from_v8<T>(isolate, result);
Expand Down
10 changes: 5 additions & 5 deletions test/test_call_v8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ void test_call_v8()

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

check_eq("no args",
v8pp::call_v8(isolate, fun, fun)->Int32Value(), 0);
v8pp::call_v8(isolate, fun, fun)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 0);
check_eq("1 arg",
v8pp::call_v8(isolate, fun, fun, 1)->Int32Value(), 1);
v8pp::call_v8(isolate, fun, fun, 1)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 1);
check_eq("2 args",
v8pp::call_v8(isolate, fun, fun, true, 2.2)->Int32Value(), 2);
v8pp::call_v8(isolate, fun, fun, true, 2.2)->Int32Value(isolate->GetCurrentContext()).ToChecked(), 2);
check_eq("3 args",
v8pp::call_v8(isolate, fun, fun, 1, true, "abc")->Int32Value(), 3);
v8pp::call_v8(isolate, fun, fun, 1, true, "abc")->Int32Value(isolate->GetCurrentContext()).ToChecked(), 3);
}
8 changes: 4 additions & 4 deletions test/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct factory<Y, v8pp::shared_ptr_traits>
template<typename Traits>
static int extern_fun(v8::FunctionCallbackInfo<v8::Value> const& args)
{
int x = args[0]->Int32Value();
int x = args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
auto self = v8pp::class_<X, Traits>::unwrap_object(args.GetIsolate(), args.This());
if (self) x += self->var;
return x;
Expand Down Expand Up @@ -156,18 +156,18 @@ void test_class_()

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

v8::Handle<v8::Object> y1_obj =
v8::Local<v8::Object> y1_obj =
v8pp::class_<Y, Traits>::reference_external(context.isolate(), y1);
check("y1", v8pp::from_v8<decltype(y1)>(isolate, y1_obj) == y1);
check("y1_obj", v8pp::to_v8(isolate, y1) == y1_obj);

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

v8::Handle<v8::Object> y3_obj =
v8::Local<v8::Object> y3_obj =
v8pp::class_<Y, Traits>::create_object(context.isolate(), -3);
auto y3 = v8pp::class_<Y, Traits>::unwrap_object(isolate, y3_obj);
check("y3", v8pp::from_v8<decltype(y3)>(isolate, y3_obj) == y3);
Expand Down
2 changes: 1 addition & 1 deletion test/test_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ void test_context()
v8pp::context context;

v8::HandleScope scope(context.isolate());
int const r = context.run_script("42")->Int32Value();
int const r = context.run_script("42")->Int32Value(context.isolate()->GetCurrentContext()).ToChecked();
check_eq("run_script", r, 42);
}
14 changes: 7 additions & 7 deletions test/test_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ template<>
struct convert<person>
{
using from_type = person;
using to_type = v8::Handle<v8::Object>;
using to_type = v8::Local<v8::Object>;

static bool is_valid(v8::Isolate*, v8::Handle<v8::Value> value)
static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
{
return !value.IsEmpty() && value->IsObject();
}
Expand All @@ -86,16 +86,16 @@ struct convert<person>
{
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::Object> obj = v8::Object::New(isolate);
obj->Set(v8pp::to_v8(isolate, "name"), v8pp::to_v8(isolate, p.name));
obj->Set(v8pp::to_v8(isolate, "age"), v8pp::to_v8(isolate, p.age));
obj->Set(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "name"), v8pp::to_v8(isolate, p.name));
obj->Set(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "age"), v8pp::to_v8(isolate, p.age));
/* Simpler after #include <v8pp/object.hpp>
set_option(isolate, obj, "name", p.name);
set_option(isolate, obj, "age", p.age);
*/
return scope.Escape(obj);
}

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

person result;
result.name = v8pp::from_v8<std::string>(isolate,
obj->Get(v8pp::to_v8(isolate, "name")));
obj->Get(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "name")).ToLocalChecked());
result.age = v8pp::from_v8<int>(isolate,
obj->Get(v8pp::to_v8(isolate, "age")));
obj->Get(isolate->GetCurrentContext(), v8pp::to_v8(isolate, "age")).ToLocalChecked());

/* Simpler after #include <v8pp/object.hpp>
get_option(isolate, obj, "name", result.name);
Expand Down
2 changes: 1 addition & 1 deletion test/test_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void test_factory()

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

v8pp::call_v8(isolate, fun, fun);
check_eq("all ctors called", ctor_types, 0x0F);
Expand Down
4 changes: 2 additions & 2 deletions test/test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void test_json()
str = v8pp::json_str(isolate, v);
v = v8pp::json_parse(isolate, "42");
check_eq("int string", str, "42");
check_eq("int parse", v->Int32Value(), 42);
check_eq("int parse", v->Int32Value(isolate->GetCurrentContext()).ToChecked(), 42);

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

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

str = v8pp::json_str(isolate, arr);
v = v8pp::json_parse(isolate, str);
Expand Down
2 changes: 1 addition & 1 deletion test/test_ptr_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void test_ptr_traits()
v8::Isolate* isolate = context.isolate();
v8::HandleScope scope(isolate);

v8::Handle<v8::Function> fun = v8::Function::New(isolate, test_create_destroy);
v8::Local<v8::Function> fun = v8::Function::New(isolate->GetCurrentContext(), test_create_destroy).ToLocalChecked();
v8pp::call_v8(isolate, fun, fun);
check_eq("all ctors called", ctor_types, 0x0F);
check_eq("ctor count", ctor_count, 6);
Expand Down
2 changes: 1 addition & 1 deletion test/test_throw_ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace {

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

Expand Down
2 changes: 1 addition & 1 deletion v8pp/call_from_v8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct call_from_v8_traits
>::type;

template<size_t Index, typename Traits>
static decltype(arg_convert<Index, Traits>::from_v8(std::declval<v8::Isolate*>(), std::declval<v8::Handle<v8::Value>>()))
static decltype(arg_convert<Index, Traits>::from_v8(std::declval<v8::Isolate*>(), std::declval<v8::Local<v8::Value>>()))
arg_from_v8(v8::FunctionCallbackInfo<v8::Value> const& args)
{
return arg_convert<Index, Traits>::from_v8(args.GetIsolate(), args[Index - Offset]);
Expand Down
8 changes: 4 additions & 4 deletions v8pp/call_v8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ namespace v8pp {
/// @param recv V8 object used as `this` in the function
/// @param args... C++ arguments to convert to JS arguments using to_v8
template<typename ...Args>
v8::Handle<v8::Value> call_v8(v8::Isolate* isolate, v8::Handle<v8::Function> func,
v8::Handle<v8::Value> recv, Args&&... args)
v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func,
v8::Local<v8::Value> recv, Args&&... args)
{
v8::EscapableHandleScope scope(isolate);

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

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

return scope.Escape(result);
}
Expand Down
29 changes: 14 additions & 15 deletions v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class object_registry final : public class_info
return result;
}

v8::Handle<v8::Object> wrap_object(pointer_type const& object, bool call_dtor)
v8::Local<v8::Object> wrap_object(pointer_type const& object, bool call_dtor)
{
auto it = objects_.find(object);
if (it != objects_.end())
Expand All @@ -233,13 +233,13 @@ class object_registry final : public class_info
v8::EscapableHandleScope scope(isolate_);

v8::Local<v8::Object> obj = class_function_template()
->GetFunction()->NewInstance();
->GetFunction(isolate_->GetCurrentContext()).ToLocalChecked()->NewInstance();

obj->SetAlignedPointerInInternalField(0, Traits::pointer_id(object));
obj->SetAlignedPointerInInternalField(1, this);
obj->SetAlignedPointerInInternalField(2, call_dtor? /*dtor_*/this : nullptr);

v8::UniquePersistent<v8::Object> pobj(isolate_, obj);
v8::Global<v8::Object> pobj(isolate_, obj);
pobj.SetWeak(this, [](v8::WeakCallbackInfo<object_registry> const& data)
{
object_id object = data.GetInternalField(0);
Expand All @@ -251,7 +251,7 @@ class object_registry final : public class_info
return scope.Escape(obj);
}

v8::Handle<v8::Object> wrap_object(v8::FunctionCallbackInfo<v8::Value> const& args)
v8::Local<v8::Object> wrap_object(v8::FunctionCallbackInfo<v8::Value> const& args)
{
if (!ctor_)
{
Expand All @@ -267,7 +267,7 @@ class object_registry final : public class_info

while (value->IsObject())
{
v8::Handle<v8::Object> obj = value->ToObject();
v8::Local<v8::Object> obj = value.As<v8::Object>();
if (obj->InternalFieldCount() == 3)
{
object_id id = obj->GetAlignedPointerFromInternalField(0);
Expand All @@ -291,7 +291,7 @@ class object_registry final : public class_info
}

private:
void reset_object(std::pair<pointer_type const, v8::UniquePersistent<v8::Object>>& object)
void reset_object(std::pair<pointer_type const, v8::Global<v8::Object>>& object)
{
bool call_dtor = true;
if (!object.second.IsNearDeath())
Expand Down Expand Up @@ -328,11 +328,11 @@ class object_registry final : public class_info
std::vector<base_class_info> bases_;
std::vector<object_registry*> derivatives_;

std::unordered_map<pointer_type, v8::UniquePersistent<v8::Object>> objects_;
std::unordered_map<pointer_type, v8::Global<v8::Object>> objects_;

v8::Isolate* isolate_;
v8::UniquePersistent<v8::FunctionTemplate> func_;
v8::UniquePersistent<v8::FunctionTemplate> js_func_;
v8::Global<v8::FunctionTemplate> func_;
v8::Global<v8::FunctionTemplate> js_func_;

ctor_function ctor_;
dtor_function dtor_;
Expand Down Expand Up @@ -600,7 +600,7 @@ class class_

/// Create JavaScript object which references externally created C++ class.
/// It will not take ownership of the C++ pointer.
static v8::Handle<v8::Object> reference_external(v8::Isolate* isolate,
static v8::Local<v8::Object> reference_external(v8::Isolate* isolate,
object_pointer_type const& ext)
{
using namespace detail;
Expand All @@ -617,15 +617,14 @@ class class_
/// As reference_external but delete memory for C++ object
/// when JavaScript object is deleted. You must use `factory<T>::create()`
/// to allocate `ext`
static v8::Handle<v8::Object> import_external(v8::Isolate* isolate, object_pointer_type const& ext)
static v8::Local<v8::Object> import_external(v8::Isolate* isolate, object_pointer_type const& ext)
{
using namespace detail;
return classes::find<Traits>(isolate, type_id<T>()).wrap_object(ext, true);
}

/// Get wrapped object from V8 value, may return nullptr on fail.
static object_pointer_type unwrap_object(v8::Isolate* isolate,
v8::Handle<v8::Value> value)
static object_pointer_type unwrap_object(v8::Isolate* isolate, v8::Local<v8::Value> value)
{
using namespace detail;
return Traits::template static_pointer_cast<T>(
Expand All @@ -634,14 +633,14 @@ class class_

/// Create a wrapped C++ object and import it into JavaScript
template<typename ...Args>
static v8::Handle<v8::Object> create_object(v8::Isolate* isolate, Args... args)
static v8::Local<v8::Object> create_object(v8::Isolate* isolate, Args... args)
{
return import_external(isolate,
factory<T, Traits>::create(isolate, std::forward<Args>(args)...));
}

/// Find V8 object handle for a wrapped C++ object, may return empty handle on fail.
static v8::Handle<v8::Object> find_object(v8::Isolate* isolate,
static v8::Local<v8::Object> find_object(v8::Isolate* isolate,
object_const_pointer_type const& obj)
{
using namespace detail;
Expand Down
2 changes: 1 addition & 1 deletion v8pp/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#endif

#define V8PP_PLUGIN_INIT(isolate) extern "C" V8PP_EXPORT \
v8::Handle<v8::Value> V8PP_PLUGIN_INIT_PROC_NAME(isolate)
v8::Local<v8::Value> V8PP_PLUGIN_INIT_PROC_NAME(isolate)

#ifndef V8PP_HEADER_ONLY
#define V8PP_HEADER_ONLY 1
Expand Down
Loading

0 comments on commit c56a717

Please sign in to comment.