Skip to content

Commit

Permalink
Fix Node.js 12 support (#499)
Browse files Browse the repository at this point in the history
* Use v8::Local instead of v8::Handle

v8::Handle has been deprecated in favor of v8::Local since V8 4.5 and
has been removed somehwere after V8 7.0. Node.js 12 (current stable)
is upgrading V8 engine to 7.4+ which does not support v8::Handle.

Mass-replace v8::Handle -> v8::Local to avoid that issue. It's supported
even by the ancient Node versions packaged in older Ubuntu releases.

* Use NAN helpers

v8::Object::Set() and v8::FunctionTemplate::GetFunction() have had
their API changed between V8 versions. Upgrading Node.js to 12 breaks
them completely.

Instead of using this API directly we should be using NAN helpers that
encapsulate the changes and provide common API for Maybe types.
  • Loading branch information
ilammy authored Jul 22, 2019
1 parent 7acbe01 commit 4aecf90
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "secure_message.hpp"
#include "secure_session.hpp"

void InitAll(v8::Handle<v8::Object> exports)
void InitAll(v8::Local<v8::Object> exports)
{
jsthemis::Errors::Init(exports);
jsthemis::SecureMessage::Init(exports);
Expand Down
6 changes: 3 additions & 3 deletions src/wrappers/themis/jsthemis/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
v8::Local<v8::Object> globalObj = v8::Context::GetCurrent()->Global(); \
v8::Local<v8::Function> bufferConstructor = v8::Local<v8::Function>::Cast( \
globalObj->Get(v8::String::New("Buffer"))); \
v8::Handle<v8::Value> constructorArgs[3] = {buf->handle_, \
v8::Integer::New(length), \
v8::Integer::New(0)}; \
v8::Local<v8::Value> constructorArgs[3] = {buf->handle_, \
v8::Integer::New(length), \
v8::Integer::New(0)}; \
v8::Local<v8::Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); \
return scope.Close(actualBuffer)

Expand Down
8 changes: 4 additions & 4 deletions src/wrappers/themis/jsthemis/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
namespace jsthemis
{

static inline void ExportStatusCode(v8::Handle<v8::Object>& exports, const char* name, themis_status_t status)
static inline void ExportStatusCode(v8::Local<v8::Object>& exports, const char* name, themis_status_t status)
{
exports->Set(Nan::New(name).ToLocalChecked(), Nan::New(status));
Nan::Set(exports, Nan::New(name).ToLocalChecked(), Nan::New(status));
}

void Errors::Init(v8::Handle<v8::Object> exports)
void Errors::Init(v8::Local<v8::Object> exports)
{
ExportStatusCode(exports, "SUCCESS", THEMIS_SUCCESS);
ExportStatusCode(exports, "FAIL", THEMIS_FAIL);
Expand Down Expand Up @@ -106,7 +106,7 @@ static const char* ErrorDescriptionSecureComparator(themis_status_t status)
static v8::Local<v8::Value> WithStatus(v8::Local<v8::Value> error, themis_status_t status)
{
v8::Local<v8::Object> object = error.As<v8::Object>();
object->Set(Nan::New("code").ToLocalChecked(), Nan::New(status));
Nan::Set(object, Nan::New("code").ToLocalChecked(), Nan::New(status));
return error;
}

Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace jsthemis
namespace Errors
{

void Init(v8::Handle<v8::Object> exports);
void Init(v8::Local<v8::Object> exports);

} // namespace Errors

Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ SecureCellContextImprint::~SecureCellContextImprint()
{
}

void SecureCellContextImprint::Init(v8::Handle<v8::Object> exports)
void SecureCellContextImprint::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureCellContextImprint").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(SecureCellContextImprint::New);
tpl->SetClassName(Nan::New("SecureCellContextImprint").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "encrypt", encrypt);
Nan::SetPrototypeMethod(tpl, "decrypt", decrypt);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureCellContextImprint").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureCellContextImprint::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace jsthemis
class SecureCellContextImprint : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureCellContextImprint(const std::vector<uint8_t>& key);
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_cell_seal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ SecureCellSeal::~SecureCellSeal()
{
}

void SecureCellSeal::Init(v8::Handle<v8::Object> exports)
void SecureCellSeal::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureCellSeal").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("SecureCellSeal").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "encrypt", encrypt);
Nan::SetPrototypeMethod(tpl, "decrypt", decrypt);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureCellSeal").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureCellSeal::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_cell_seal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace jsthemis
class SecureCellSeal : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureCellSeal(const std::vector<uint8_t>& key);
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ SecureCellTokenProtect::~SecureCellTokenProtect()
{
}

void SecureCellTokenProtect::Init(v8::Handle<v8::Object> exports)
void SecureCellTokenProtect::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureCellTokenProtect").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(SecureCellTokenProtect::New);
tpl->SetClassName(Nan::New("SecureCellTokenProtect").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "encrypt", encrypt);
Nan::SetPrototypeMethod(tpl, "decrypt", decrypt);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureCellTokenProtect").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureCellTokenProtect::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace jsthemis
class SecureCellTokenProtect : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureCellTokenProtect(const std::vector<uint8_t>& key);
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ SecureComparator::~SecureComparator()
}
}

void SecureComparator::Init(v8::Handle<v8::Object> exports)
void SecureComparator::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureComparator").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("SecureComparator").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "beginCompare", beginCompare);
Nan::SetPrototypeMethod(tpl, "proceedCompare", proceedCompare);
Nan::SetPrototypeMethod(tpl, "isMatch", isMatch);
Nan::SetPrototypeMethod(tpl, "isCompareComplete", isCompareComplete);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureComparator").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureComparator::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace jsthemis
class SecureComparator : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureComparator(const std::vector<uint8_t>& secret);
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ KeyPair::~KeyPair()
{
}

void KeyPair::Init(v8::Handle<v8::Object> exports)
void KeyPair::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("KeyPair").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(KeyPair::New);
tpl->SetClassName(Nan::New("KeyPair").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "private", private_key);
Nan::SetPrototypeMethod(tpl, "public", public_key);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("KeyPair").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void KeyPair::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_keygen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace jsthemis
class KeyPair : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit KeyPair(const std::vector<uint8_t>& private_key, const std::vector<uint8_t>& public_key);
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ SecureMessage::~SecureMessage()
{
}

void SecureMessage::Init(v8::Handle<v8::Object> exports)
void SecureMessage::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureMessage").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(SecureMessage::New);
tpl->SetClassName(Nan::New("SecureMessage").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "encrypt", SecureMessage::encrypt);
Nan::SetPrototypeMethod(tpl, "decrypt", SecureMessage::decrypt);
Nan::SetPrototypeMethod(tpl, "sign", SecureMessage::sign);
Nan::SetPrototypeMethod(tpl, "verify", SecureMessage::verify);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureMessage").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureMessage::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace jsthemis
class SecureMessage : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureMessage(const std::vector<uint8_t>& private_key,
Expand Down
11 changes: 7 additions & 4 deletions src/wrappers/themis/jsthemis/secure_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,22 @@ SecureSession::~SecureSession()
}
}

void SecureSession::Init(v8::Handle<v8::Object> exports)
void SecureSession::Init(v8::Local<v8::Object> exports)
{
v8::Local<v8::String> className = Nan::New("SecureSession").ToLocalChecked();
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("SecureSession").ToLocalChecked());
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "connectRequest", connectRequest);
Nan::SetPrototypeMethod(tpl, "wrap", wrap);
Nan::SetPrototypeMethod(tpl, "unwrap", unwrap);
Nan::SetPrototypeMethod(tpl, "isEstablished", isEstablished);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("SecureSession").ToLocalChecked(), tpl->GetFunction());
// Export constructor
v8::Local<v8::Function> function = Nan::GetFunction(tpl).ToLocalChecked();
constructor.Reset(function);
Nan::Set(exports, className, function);
}

void SecureSession::New(const Nan::FunctionCallbackInfo<v8::Value>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/jsthemis/secure_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace jsthemis
class SecureSession : public Nan::ObjectWrap
{
public:
static void Init(v8::Handle<v8::Object> exports);
static void Init(v8::Local<v8::Object> exports);

private:
explicit SecureSession(const std::vector<uint8_t>& id,
Expand Down

0 comments on commit 4aecf90

Please sign in to comment.