From 777123e6ac82a83bb2e68f146a07b13e3a3d3089 Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Wed, 7 Nov 2018 17:07:40 +0000 Subject: [PATCH 1/4] Migration to n-api --- binding.gyp | 25 +++++++--- package-lock.json | 18 ++++++++ package.json | 2 +- src/binding.cc | 113 ++++++++++++++++++++-------------------------- 4 files changed, 87 insertions(+), 71 deletions(-) create mode 100644 package-lock.json diff --git a/binding.gyp b/binding.gyp index 615800b..427c238 100644 --- a/binding.gyp +++ b/binding.gyp @@ -6,7 +6,24 @@ 'targets': [ { 'target_name': 'iconv', - 'include_dirs': [' @@ -26,89 +27,72 @@ #define ICONV_CONST #endif // ICONV_CONST -namespace -{ - -using v8::Array; -using v8::Boolean; -using v8::FunctionTemplate; -using v8::Handle; -using v8::Integer; -using v8::Local; -using v8::Null; -using v8::Object; -using v8::ObjectTemplate; -using v8::String; -using v8::Value; +using namespace Napi; -struct Iconv +struct Iconv: Napi::ObjectWrap { - static Nan::Persistent object_template; - iconv_t conv_; + static Napi::FunctionReference object_template; // iconv_constructor - Iconv(iconv_t conv) - { - conv_ = conv; - } + Iconv(const Napi::CallbackInfo& info): + Napi::ObjectWrap(info) { + conv_ = info[0].As>().Data(); + } + iconv_t conv_; ~Iconv() { iconv_close(conv_); } - static void WeakCallback(const Nan::WeakCallbackInfo& data) - { - delete data.GetParameter(); + static void Init(Napi::Env env) { + Napi::Function func = DefineClass(env, "Iconv", {}); + Iconv::object_template = Napi::Persistent(func); + Iconv::object_template.SuppressDestruct(); } - static void Initialize(Handle obj) + static Napi::Object Initialize(Napi::Env env, Napi::Object exports) { - Local t = Nan::New(); - t->SetInternalFieldCount(1); - object_template.Reset(t); - obj->Set(Nan::New("make").ToLocalChecked(), - Nan::New(Make)->GetFunction()); - obj->Set(Nan::New("convert").ToLocalChecked(), - Nan::New(Convert)->GetFunction()); + Iconv::Init(env); + exports.Set(Napi::String::New(env, "make"), + Napi::Function::New(env, Make)); + exports.Set(Napi::String::New(env, "convert"), + Napi::Function::New(env, Convert)); #define EXPORT_ERRNO(err) \ - obj->Set(Nan::New(#err).ToLocalChecked(), Nan::New(err)) + exports.Set(Napi::String::New(env, #err), Napi::Number::New(env, err)) EXPORT_ERRNO(EINVAL); EXPORT_ERRNO(EILSEQ); EXPORT_ERRNO(E2BIG); #undef EXPORT_ERRNO + return exports; } - static NAN_METHOD(Make) + static Napi::Value Make(const Napi::CallbackInfo& info) { - Nan::Utf8String from_encoding(info[0]); - Nan::Utf8String to_encoding(info[1]); - iconv_t conv = iconv_open(*to_encoding, *from_encoding); + std::string from_encoding = info[0].As(); + std::string to_encoding = info[1].As(); + iconv_t conv = iconv_open(to_encoding.c_str(), from_encoding.c_str()); if (conv == reinterpret_cast(-1)) { - return info.GetReturnValue().SetNull(); + return info.Env().Null(); } - Iconv* iv = new Iconv(conv); - Local obj = - Nan::New(object_template)->NewInstance(); - Nan::SetInternalFieldPointer(obj, 0, iv); - Nan::Persistent persistent(obj); - persistent.SetWeak(iv, WeakCallback, Nan::WeakCallbackType::kParameter); - info.GetReturnValue().Set(obj); + Napi::Value param = Napi::External::New(info.Env(), conv); + return Iconv::object_template.New({param}); } - static NAN_METHOD(Convert) + static Napi::Value Convert(const Napi::CallbackInfo& info) { - Iconv* iv = static_cast( - Nan::GetInternalFieldPointer(info[0].As(), 0)); - const bool is_flush = Nan::To(info[8]).FromJust(); + Napi::Env env = info.Env(); + Iconv* iv = Iconv::Unwrap(info[0].As()); + + const bool is_flush = info[8].As().Value(); ICONV_CONST char* input_buf = - is_flush ? NULL : node::Buffer::Data(info[1].As()); - size_t input_start = Nan::To(info[2]).FromJust(); - size_t input_size = Nan::To(info[3]).FromJust(); - char* output_buf = node::Buffer::Data(info[4].As()); - size_t output_start = Nan::To(info[5]).FromJust(); - size_t output_size = Nan::To(info[6]).FromJust(); - Local rc = info[7].As(); + is_flush ? NULL : info[1].As>().Data(); + size_t input_start = info[2].As().Uint32Value(); + size_t input_size = info[3].As().Uint32Value(); + char* output_buf = info[4].As>().Data(); + size_t output_start = info[5].As().Uint32Value(); + size_t output_size = info[6].As().Uint32Value(); + Napi::Array rc = info[7].As(); if (input_buf != NULL) input_buf += input_start; output_buf += output_start; size_t input_consumed = input_size; @@ -124,18 +108,21 @@ struct Iconv } input_consumed -= input_size; output_consumed -= output_size; - rc->Set(0, Nan::New(static_cast(input_consumed))); - rc->Set(1, Nan::New(static_cast(output_consumed))); - info.GetReturnValue().Set(errorno); + rc.Set(static_cast(0), Napi::Number::New(env, static_cast(input_consumed))); + rc.Set(static_cast(1), Napi::Number::New(env, static_cast(output_consumed))); + return Napi::Number::New(env, errorno); } // Forbid implicit copying. Iconv(const Iconv&); void operator=(const Iconv&); + }; -Nan::Persistent Iconv::object_template; +Napi::FunctionReference Iconv::object_template; // iconv_constructor -} // namespace +Napi::Object InitAll(Napi::Env env, Napi::Object exports) { + return Iconv::Initialize(env, exports); +} -NODE_MODULE(iconv, Iconv::Initialize); +NODE_API_MODULE(iconv, InitAll) From 1fb6ea218e5ad612af51a9be44ad12afbbf9c556 Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Thu, 8 Nov 2018 08:00:19 +0000 Subject: [PATCH 2/4] Migration to n-api: safe convertion to uint32_t --- src/binding.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 98ab4f6..196260e 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -87,11 +87,11 @@ struct Iconv: Napi::ObjectWrap const bool is_flush = info[8].As().Value(); ICONV_CONST char* input_buf = is_flush ? NULL : info[1].As>().Data(); - size_t input_start = info[2].As().Uint32Value(); - size_t input_size = info[3].As().Uint32Value(); + size_t input_start = info[2].IsNumber() ? info[2].As().Uint32Value() : static_cast(0); + size_t input_size = info[3].IsNumber() ? info[3].As().Uint32Value() : static_cast(0); char* output_buf = info[4].As>().Data(); - size_t output_start = info[5].As().Uint32Value(); - size_t output_size = info[6].As().Uint32Value(); + size_t output_start = info[5].IsNumber() ? info[5].As().Uint32Value() : static_cast(0); + size_t output_size = info[6].IsNumber() ? info[6].As().Uint32Value() : static_cast(0); Napi::Array rc = info[7].As(); if (input_buf != NULL) input_buf += input_start; output_buf += output_start; From 1ded1215bf5728bf2823028a317946798139f9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 15 Dec 2018 12:19:10 +0100 Subject: [PATCH 3/4] src: remove usage of v8::Handle It doesn't exist if V8_IMMINENT_DEPRECATION_WARNINGS is enabled. --- src/binding.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 7fc0be2..398c7d7 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -32,7 +32,6 @@ namespace using v8::Array; using v8::Boolean; using v8::FunctionTemplate; -using v8::Handle; using v8::Integer; using v8::Local; using v8::Null; @@ -62,7 +61,7 @@ struct Iconv delete data.GetParameter(); } - static void Initialize(Handle obj) + static void Initialize(Local obj) { Local t = Nan::New(); t->SetInternalFieldCount(1); From 0607d66c5a533c89a560cb574f0ff73a851069d8 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 17 Dec 2018 17:00:43 +0100 Subject: [PATCH 4/4] package: 2.3.1 -> 2.3.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d7de4a7..d0dfe8f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "iconv", "main": "./lib/iconv", - "version": "2.3.1", + "version": "2.3.2", "description": "Text recoding in JavaScript for fun and profit!", "homepage": "https://github.com/bnoordhuis/node-iconv", "repository": {