From 205a55c9f20fb43451bb38016a14838d85227abc Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 11 Dec 2019 20:55:44 +0100 Subject: [PATCH] Support nodejs 13 (#13) --- src/DiffWorkerCallback.cpp | 18 +++++------ src/Main.cpp | 64 +++++++++++++++++-------------------- src/PatchWorkerCallback.cpp | 18 +++++------ 3 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/DiffWorkerCallback.cpp b/src/DiffWorkerCallback.cpp index b59c0c5..c851ce6 100644 --- a/src/DiffWorkerCallback.cpp +++ b/src/DiffWorkerCallback.cpp @@ -34,14 +34,14 @@ DiffWorkerCallback::DiffWorkerCallback(Nan::Callback *callback, const std::strin : AsyncProgressWorkerBase(callback) { _oldfile = oldfile; - _newfile = newfile; + _newfile = newfile; _patchfile = patchfile; } -DiffWorkerCallback::~DiffWorkerCallback() +DiffWorkerCallback::~DiffWorkerCallback() { } -void DiffWorkerCallback::Execute(const ExecutionProgress& progress) +void DiffWorkerCallback::Execute(const ExecutionProgress& progress) { char error[1024]; memset(error, 0, sizeof error); @@ -51,28 +51,28 @@ void DiffWorkerCallback::Execute(const ExecutionProgress& progress) data.progressWorker = &progress; bsdiff(error, _oldfile.c_str(), _newfile.c_str(), _patchfile.c_str(), &data, &DiffWorkerCallback::CCallback); - _error = error; + _error = error; } -void DiffWorkerCallback::HandleProgressCallback(const int* data, size_t count) +void DiffWorkerCallback::HandleProgressCallback(const int* data, size_t count) { if(data != nullptr) { Nan::HandleScope scope; v8::Local argv[] = { v8::Number::New(v8::Isolate::GetCurrent(), *data), - v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), _error.c_str()) + Nan::New(_error.c_str()).ToLocalChecked() }; callback->Call(2, argv, this->async_resource); } } -void DiffWorkerCallback::HandleOKCallback() +void DiffWorkerCallback::HandleOKCallback() { - v8::Local argv[] = { + v8::Local argv[] = { v8::Number::New(v8::Isolate::GetCurrent(), 100), - v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), _error.c_str()) + Nan::New(_error.c_str()).ToLocalChecked() }; callback->Call(2, argv, this->async_resource); } diff --git a/src/Main.cpp b/src/Main.cpp index 50bb348..16e0e22 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -29,48 +29,46 @@ extern "C" { #include "c/bsdiff/bsdiff.h" - #include "c/bspatch/bspatch.h" + #include "c/bspatch/bspatch.h" } namespace bsdpNode { using namespace v8; - void diff(const FunctionCallbackInfo& args) + void diff(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - - if(args.Length() < 4 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) + if(args.Length() < 4 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) { - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Invalid arguments."))); + Nan::ThrowError("Invalid arguments."); return; } - + Nan::Callback *callback = new Nan::Callback(args[3].As()); Nan::Utf8String param0(args[0]); - std::string oldfile = std::string(*param0); + std::string oldfile = std::string(*param0); Nan::Utf8String param1(args[1]); - std::string newfile = std::string(*param1); + std::string newfile = std::string(*param1); Nan::Utf8String param2(args[2]); - std::string patchfile = std::string(*param2); + std::string patchfile = std::string(*param2); DiffWorkerCallback* wc = new DiffWorkerCallback(callback, oldfile, newfile, patchfile); Nan::AsyncQueueWorker(wc); } - void diffSync(const FunctionCallbackInfo& args) + void diffSync(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); - - if (!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) + + if (!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) { - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Invalid arguments."))); + Nan::ThrowError("Invalid arguments."); return; } - + Nan::Utf8String oldfile(args[0]); Nan::Utf8String newfile(args[1]); Nan::Utf8String patchfile(args[2]); @@ -79,48 +77,46 @@ namespace bsdpNode { char error[1024]; memset(error, 0, sizeof error); - int ret = bsdiff(error, *oldfile, *newfile, *patchfile, nullptr, nullptr); + int ret = bsdiff(error, *oldfile, *newfile, *patchfile, nullptr, nullptr); - if(ret != 0) - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, error))); + if(ret != 0) + Nan::ThrowError(error); } - void patch(const FunctionCallbackInfo& args) + void patch(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - - if(args.Length() < 4 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) + if(args.Length() < 4 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) { - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Invalid arguments."))); + Nan::ThrowError("Invalid arguments."); return; } - + Nan::Callback *callback = new Nan::Callback(args[3].As()); Nan::Utf8String param0(args[0]); - std::string oldfile = std::string(*param0); + std::string oldfile = std::string(*param0); Nan::Utf8String param1(args[1]); - std::string newfile = std::string(*param1); + std::string newfile = std::string(*param1); Nan::Utf8String param2(args[2]); - std::string patchfile = std::string(*param2); + std::string patchfile = std::string(*param2); PatchWorkerCallback* wc = new PatchWorkerCallback(callback, oldfile, newfile, patchfile); Nan::AsyncQueueWorker(wc); } - void patchSync(const FunctionCallbackInfo& args) + void patchSync(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); - if (!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) + if (!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) { - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Invalid arguments."))); + Nan::ThrowError("Invalid arguments."); return; } - + Nan::Utf8String oldfile(args[0]); Nan::Utf8String newfile(args[1]); Nan::Utf8String patchfile(args[2]); @@ -128,10 +124,10 @@ namespace bsdpNode { char error[1024]; memset(error, 0, sizeof error); - int ret = bspatch(error, *oldfile, *newfile, *patchfile, nullptr, nullptr); + int ret = bspatch(error, *oldfile, *newfile, *patchfile, nullptr, nullptr); if(ret != 0) - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, error))); + Nan::ThrowError(error); } void init(Local exports, Local module) { @@ -143,4 +139,4 @@ namespace bsdpNode { } NODE_MODULE(bsdp, init) -} \ No newline at end of file +} diff --git a/src/PatchWorkerCallback.cpp b/src/PatchWorkerCallback.cpp index 9a70297..bd94da0 100644 --- a/src/PatchWorkerCallback.cpp +++ b/src/PatchWorkerCallback.cpp @@ -34,14 +34,14 @@ PatchWorkerCallback::PatchWorkerCallback(Nan::Callback *callback, const std::str : AsyncProgressWorkerBase(callback) { _oldfile = oldfile; - _newfile = newfile; + _newfile = newfile; _patchfile = patchfile; } -PatchWorkerCallback::~PatchWorkerCallback() +PatchWorkerCallback::~PatchWorkerCallback() { } -void PatchWorkerCallback::Execute(const ExecutionProgress& progress) +void PatchWorkerCallback::Execute(const ExecutionProgress& progress) { char error[1024]; memset(error, 0, sizeof error); @@ -51,28 +51,28 @@ void PatchWorkerCallback::Execute(const ExecutionProgress& progress) data.progressWorker = &progress; bspatch(error, _oldfile.c_str(), _newfile.c_str(), _patchfile.c_str(), &data, &PatchWorkerCallback::CCallback); - _error = error; + _error = error; } -void PatchWorkerCallback::HandleProgressCallback(const int* data, size_t count) +void PatchWorkerCallback::HandleProgressCallback(const int* data, size_t count) { if(data != nullptr) { Nan::HandleScope scope; v8::Local argv[] = { v8::Number::New(v8::Isolate::GetCurrent(), *data), - v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), _error.c_str()) + Nan::New(_error.c_str()).ToLocalChecked() }; callback->Call(2, argv, this->async_resource); } } -void PatchWorkerCallback::HandleOKCallback() +void PatchWorkerCallback::HandleOKCallback() { - v8::Local argv[] = { + v8::Local argv[] = { v8::Number::New(v8::Isolate::GetCurrent(), 100), - v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), _error.c_str()) + Nan::New(_error.c_str()).ToLocalChecked() }; callback->Call(2, argv, this->async_resource); }