Commit de8888ef by Mohamed Akram

Convert NAN to N-API using tool

parent fdceaef8
...@@ -7,10 +7,21 @@ ...@@ -7,10 +7,21 @@
"targets": [ "targets": [
{ {
"target_name": "<(module_name)", "target_name": "<(module_name)",
"include_dirs": ["<!(node -e \"require('nan')\")"], "cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"xcode_settings": { "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
},
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")","<!(node -e \"require('nan')\")"],
"conditions": [ "conditions": [
["sqlite != 'internal'", { ["sqlite != 'internal'", {
"include_dirs": [ "<(sqlite)/include" ], "include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")", "<(sqlite)/include" ],
"libraries": [ "libraries": [
"-l<(sqlite_libname)" "-l<(sqlite_libname)"
], ],
...@@ -26,6 +37,7 @@ ...@@ -26,6 +37,7 @@
}, },
{ {
"dependencies": [ "dependencies": [
"<!(node -p 'require(\"node-addon-api\").gyp')",
"deps/sqlite3.gyp:sqlite3" "deps/sqlite3.gyp:sqlite3"
] ]
} }
...@@ -40,8 +52,18 @@ ...@@ -40,8 +52,18 @@
}, },
{ {
"target_name": "action_after_build", "target_name": "action_after_build",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"xcode_settings": { "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
},
"type": "none", "type": "none",
"dependencies": [ "<(module_name)" ], "dependencies": [
"<!(node -p 'require(\"node-addon-api\").gyp')", "<(module_name)" ],
"copies": [ "copies": [
{ {
"files": [ "<(PRODUCT_DIR)/<(module_name).node" ], "files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
"url": "git://github.com/mapbox/node-sqlite3.git" "url": "git://github.com/mapbox/node-sqlite3.git"
}, },
"dependencies": { "dependencies": {
"nan": "^2.12.1", "node-addon-api": "2.0.0",
"node-pre-gyp": "^0.11.0" "node-pre-gyp": "^0.11.0"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -8,10 +8,11 @@ ...@@ -8,10 +8,11 @@
#include <set> #include <set>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
#include <uv.h>
using namespace v8; using namespace Napi;
using namespace node; using namespace Napi;
namespace node_sqlite3 { namespace node_sqlite3 {
...@@ -92,19 +93,19 @@ namespace node_sqlite3 { ...@@ -92,19 +93,19 @@ namespace node_sqlite3 {
* backup.finish(); * backup.finish();
* *
*/ */
class Backup : public Nan::ObjectWrap { class Backup : public Napi::ObjectWrap<Backup> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static NAN_METHOD(New); static Napi::Value New(const Napi::CallbackInfo& info);
struct Baton { struct Baton {
uv_work_t request; uv_work_t request;
Backup* backup; Backup* backup;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
Baton(Backup* backup_, Local<Function> cb_) : backup(backup_) { Baton(Backup* backup_, Napi::Function cb_) : backup(backup_) {
backup->Ref(); backup->Ref();
request.data = this; request.data = this;
callback.Reset(cb_); callback.Reset(cb_);
...@@ -121,7 +122,7 @@ public: ...@@ -121,7 +122,7 @@ public:
std::string sourceName; std::string sourceName;
std::string destName; std::string destName;
bool filenameIsDest; bool filenameIsDest;
InitializeBaton(Database* db_, Local<Function> cb_, Backup* backup_) : InitializeBaton(Database* db_, Napi::Function cb_, Backup* backup_) :
Baton(db_, cb_), backup(backup_), filenameIsDest(true) { Baton(db_, cb_), backup(backup_), filenameIsDest(true) {
backup->Ref(); backup->Ref();
} }
...@@ -137,7 +138,7 @@ public: ...@@ -137,7 +138,7 @@ public:
struct StepBaton : Baton { struct StepBaton : Baton {
int pages; int pages;
std::set<int> retryErrorsSet; std::set<int> retryErrorsSet;
StepBaton(Backup* backup_, Local<Function> cb_, int pages_) : StepBaton(Backup* backup_, Napi::Function cb_, int pages_) :
Baton(backup_, cb_), pages(pages_) {} Baton(backup_, cb_), pages(pages_) {}
}; };
...@@ -149,7 +150,7 @@ public: ...@@ -149,7 +150,7 @@ public:
Baton* baton; Baton* baton;
}; };
Backup(Database* db_) : Nan::ObjectWrap(), Backup(Database* db_) : Napi::ObjectWrap<Backup>(),
db(db_), db(db_),
_handle(NULL), _handle(NULL),
_otherDb(NULL), _otherDb(NULL),
...@@ -173,16 +174,16 @@ public: ...@@ -173,16 +174,16 @@ public:
WORK_DEFINITION(Step); WORK_DEFINITION(Step);
WORK_DEFINITION(Finish); WORK_DEFINITION(Finish);
static NAN_GETTER(IdleGetter); Napi::Value IdleGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(CompletedGetter); Napi::Value CompletedGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(FailedGetter); Napi::Value FailedGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(PageCountGetter); Napi::Value PageCountGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(RemainingGetter); Napi::Value RemainingGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(FatalErrorGetter); Napi::Value FatalErrorGetter(const Napi::CallbackInfo& info);
static NAN_GETTER(RetryErrorGetter); Napi::Value RetryErrorGetter(const Napi::CallbackInfo& info);
static NAN_SETTER(FatalErrorSetter); void FatalErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
static NAN_SETTER(RetryErrorSetter); void RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
protected: protected:
static void Work_BeginInitialize(Database::Baton* baton); static void Work_BeginInitialize(Database::Baton* baton);
...@@ -215,7 +216,7 @@ protected: ...@@ -215,7 +216,7 @@ protected:
bool finished; bool finished;
std::queue<Call*> queue; std::queue<Call*> queue;
Nan::Persistent<Array> retryErrors; Napi::Persistent<Array> retryErrors;
}; };
} }
......
...@@ -7,37 +7,39 @@ ...@@ -7,37 +7,39 @@
#include <queue> #include <queue>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
#include <uv.h>
#include "async.h" #include "async.h"
using namespace v8; using namespace Napi;
namespace node_sqlite3 { namespace node_sqlite3 {
class Database; class Database;
class Database : public Nan::ObjectWrap { class Database : public Napi::ObjectWrap<Database> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static inline bool HasInstance(Local<Value> val) { static inline bool HasInstance(Napi::Value val) {
Nan::HandleScope scope; Napi::Env env = val.Env();
if (!val->IsObject()) return false; Napi::HandleScope scope(env);
Local<Object> obj = val.As<Object>(); if (!val.IsObject()) return false;
return Nan::New(constructor_template)->HasInstance(obj); Napi::Object obj = val.As<Napi::Object>();
return Napi::New(env, constructor)->HasInstance(obj);
} }
struct Baton { struct Baton {
uv_work_t request; uv_work_t request;
Database* db; Database* db;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
int status; int status;
std::string message; std::string message;
Baton(Database* db_, Local<Function> cb_) : Baton(Database* db_, Napi::Function cb_) :
db(db_), status(SQLITE_OK) { db(db_), status(SQLITE_OK) {
db->Ref(); db->Ref();
request.data = this; request.data = this;
...@@ -52,19 +54,19 @@ public: ...@@ -52,19 +54,19 @@ public:
struct OpenBaton : Baton { struct OpenBaton : Baton {
std::string filename; std::string filename;
int mode; int mode;
OpenBaton(Database* db_, Local<Function> cb_, const char* filename_, int mode_) : OpenBaton(Database* db_, Napi::Function cb_, const char* filename_, int mode_) :
Baton(db_, cb_), filename(filename_), mode(mode_) {} Baton(db_, cb_), filename(filename_), mode(mode_) {}
}; };
struct ExecBaton : Baton { struct ExecBaton : Baton {
std::string sql; std::string sql;
ExecBaton(Database* db_, Local<Function> cb_, const char* sql_) : ExecBaton(Database* db_, Napi::Function cb_, const char* sql_) :
Baton(db_, cb_), sql(sql_) {} Baton(db_, cb_), sql(sql_) {}
}; };
struct LoadExtensionBaton : Baton { struct LoadExtensionBaton : Baton {
std::string filename; std::string filename;
LoadExtensionBaton(Database* db_, Local<Function> cb_, const char* filename_) : LoadExtensionBaton(Database* db_, Napi::Function cb_, const char* filename_) :
Baton(db_, cb_), filename(filename_) {} Baton(db_, cb_), filename(filename_) {}
}; };
...@@ -101,7 +103,7 @@ public: ...@@ -101,7 +103,7 @@ public:
friend class Backup; friend class Backup;
protected: protected:
Database() : Nan::ObjectWrap(), Database() : Napi::ObjectWrap<Database>(),
_handle(NULL), _handle(NULL),
open(false), open(false),
closing(false), closing(false),
...@@ -120,40 +122,40 @@ protected: ...@@ -120,40 +122,40 @@ protected:
open = false; open = false;
} }
static NAN_METHOD(New); static Napi::Value New(const Napi::CallbackInfo& info);
static void Work_BeginOpen(Baton* baton); static void Work_BeginOpen(Baton* baton);
static void Work_Open(uv_work_t* req); static void Work_Open(uv_work_t* req);
static void Work_AfterOpen(uv_work_t* req); static void Work_AfterOpen(uv_work_t* req);
static NAN_GETTER(OpenGetter); Napi::Value OpenGetter(const Napi::CallbackInfo& info);
void Schedule(Work_Callback callback, Baton* baton, bool exclusive = false); void Schedule(Work_Callback callback, Baton* baton, bool exclusive = false);
void Process(); void Process();
static NAN_METHOD(Exec); static Napi::Value Exec(const Napi::CallbackInfo& info);
static void Work_BeginExec(Baton* baton); static void Work_BeginExec(Baton* baton);
static void Work_Exec(uv_work_t* req); static void Work_Exec(uv_work_t* req);
static void Work_AfterExec(uv_work_t* req); static void Work_AfterExec(uv_work_t* req);
static NAN_METHOD(Wait); static Napi::Value Wait(const Napi::CallbackInfo& info);
static void Work_Wait(Baton* baton); static void Work_Wait(Baton* baton);
static NAN_METHOD(Close); static Napi::Value Close(const Napi::CallbackInfo& info);
static void Work_BeginClose(Baton* baton); static void Work_BeginClose(Baton* baton);
static void Work_Close(uv_work_t* req); static void Work_Close(uv_work_t* req);
static void Work_AfterClose(uv_work_t* req); static void Work_AfterClose(uv_work_t* req);
static NAN_METHOD(LoadExtension); static Napi::Value LoadExtension(const Napi::CallbackInfo& info);
static void Work_BeginLoadExtension(Baton* baton); static void Work_BeginLoadExtension(Baton* baton);
static void Work_LoadExtension(uv_work_t* req); static void Work_LoadExtension(uv_work_t* req);
static void Work_AfterLoadExtension(uv_work_t* req); static void Work_AfterLoadExtension(uv_work_t* req);
static NAN_METHOD(Serialize); static Napi::Value Serialize(const Napi::CallbackInfo& info);
static NAN_METHOD(Parallelize); static Napi::Value Parallelize(const Napi::CallbackInfo& info);
static NAN_METHOD(Configure); static Napi::Value Configure(const Napi::CallbackInfo& info);
static NAN_METHOD(Interrupt); static Napi::Value Interrupt(const Napi::CallbackInfo& info);
static void SetBusyTimeout(Baton* baton); static void SetBusyTimeout(Baton* baton);
......
...@@ -13,12 +13,12 @@ using namespace node_sqlite3; ...@@ -13,12 +13,12 @@ using namespace node_sqlite3;
namespace { namespace {
NAN_MODULE_INIT(RegisterModule) { Napi::Object RegisterModule(Napi::Env env, Napi::Object exports) {
Nan::HandleScope scope; Napi::HandleScope scope(env);
Database::Init(target); Database::Init(env, target, module);
Statement::Init(target); Statement::Init(env, target, module);
Backup::Init(target); Backup::Init(env, target, module);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READONLY, OPEN_READONLY); DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READONLY, OPEN_READONLY);
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READWRITE, OPEN_READWRITE); DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READWRITE, OPEN_READWRITE);
...@@ -108,4 +108,4 @@ const char* sqlite_authorizer_string(int type) { ...@@ -108,4 +108,4 @@ const char* sqlite_authorizer_string(int type) {
} }
} }
NODE_MODULE(node_sqlite3, RegisterModule) NODE_API_MODULE(node_sqlite3, RegisterModule)
...@@ -12,10 +12,11 @@ ...@@ -12,10 +12,11 @@
#include <vector> #include <vector>
#include <sqlite3.h> #include <sqlite3.h>
#include <nan.h> #include <napi.h>
#include <uv.h>
using namespace v8; using namespace Napi;
using namespace node; using namespace Napi;
namespace node_sqlite3 { namespace node_sqlite3 {
...@@ -71,20 +72,20 @@ typedef Row Parameters; ...@@ -71,20 +72,20 @@ typedef Row Parameters;
class Statement : public Nan::ObjectWrap { class Statement : public Napi::ObjectWrap<Statement> {
public: public:
static Nan::Persistent<FunctionTemplate> constructor_template; static Napi::FunctionReference constructor;
static NAN_MODULE_INIT(Init); static Napi::Object Init(Napi::Env env, Napi::Object exports);
static NAN_METHOD(New); static Napi::Value New(const Napi::CallbackInfo& info);
struct Baton { struct Baton {
uv_work_t request; uv_work_t request;
Statement* stmt; Statement* stmt;
Nan::Persistent<Function> callback; Napi::FunctionReference callback;
Parameters parameters; Parameters parameters;
Baton(Statement* stmt_, Local<Function> cb_) : stmt(stmt_) { Baton(Statement* stmt_, Napi::Function cb_) : stmt(stmt_) {
stmt->Ref(); stmt->Ref();
request.data = this; request.data = this;
callback.Reset(cb_); callback.Reset(cb_);
...@@ -100,20 +101,20 @@ public: ...@@ -100,20 +101,20 @@ public:
}; };
struct RowBaton : Baton { struct RowBaton : Baton {
RowBaton(Statement* stmt_, Local<Function> cb_) : RowBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
Row row; Row row;
}; };
struct RunBaton : Baton { struct RunBaton : Baton {
RunBaton(Statement* stmt_, Local<Function> cb_) : RunBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_), inserted_id(0), changes(0) {} Baton(stmt_, cb_), inserted_id(0), changes(0) {}
sqlite3_int64 inserted_id; sqlite3_int64 inserted_id;
int changes; int changes;
}; };
struct RowsBaton : Baton { struct RowsBaton : Baton {
RowsBaton(Statement* stmt_, Local<Function> cb_) : RowsBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
Rows rows; Rows rows;
}; };
...@@ -121,10 +122,10 @@ public: ...@@ -121,10 +122,10 @@ public:
struct Async; struct Async;
struct EachBaton : Baton { struct EachBaton : Baton {
Nan::Persistent<Function> completed; Napi::FunctionReference completed;
Async* async; // Isn't deleted when the baton is deleted. Async* async; // Isn't deleted when the baton is deleted.
EachBaton(Statement* stmt_, Local<Function> cb_) : EachBaton(Statement* stmt_, Napi::Function cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
virtual ~EachBaton() { virtual ~EachBaton() {
completed.Reset(); completed.Reset();
...@@ -134,7 +135,7 @@ public: ...@@ -134,7 +135,7 @@ public:
struct PrepareBaton : Database::Baton { struct PrepareBaton : Database::Baton {
Statement* stmt; Statement* stmt;
std::string sql; std::string sql;
PrepareBaton(Database* db_, Local<Function> cb_, Statement* stmt_) : PrepareBaton(Database* db_, Napi::Function cb_, Statement* stmt_) :
Baton(db_, cb_), stmt(stmt_) { Baton(db_, cb_), stmt(stmt_) {
stmt->Ref(); stmt->Ref();
} }
...@@ -166,8 +167,8 @@ public: ...@@ -166,8 +167,8 @@ public:
// Store the callbacks here because we don't have // Store the callbacks here because we don't have
// access to the baton in the async callback. // access to the baton in the async callback.
Nan::Persistent<Function> item_cb; Napi::FunctionReference item_cb;
Nan::Persistent<Function> completed_cb; Napi::FunctionReference completed_cb;
Async(Statement* st, uv_async_cb async_cb) : Async(Statement* st, uv_async_cb async_cb) :
stmt(st), completed(false), retrieved(0) { stmt(st), completed(false), retrieved(0) {
...@@ -185,7 +186,7 @@ public: ...@@ -185,7 +186,7 @@ public:
} }
}; };
Statement(Database* db_) : Nan::ObjectWrap(), Statement(Database* db_) : Napi::ObjectWrap<Statement>(),
db(db_), db(db_),
_handle(NULL), _handle(NULL),
status(SQLITE_OK), status(SQLITE_OK),
...@@ -206,7 +207,7 @@ public: ...@@ -206,7 +207,7 @@ public:
WORK_DEFINITION(Each); WORK_DEFINITION(Each);
WORK_DEFINITION(Reset); WORK_DEFINITION(Reset);
static NAN_METHOD(Finalize); static Napi::Value Finalize(const Napi::CallbackInfo& info);
protected: protected:
static void Work_BeginPrepare(Database::Baton* baton); static void Work_BeginPrepare(Database::Baton* baton);
...@@ -219,12 +220,12 @@ protected: ...@@ -219,12 +220,12 @@ protected:
static void Finalize(Baton* baton); static void Finalize(Baton* baton);
void Finalize(); void Finalize();
template <class T> inline Values::Field* BindParameter(const Local<Value> source, T pos); template <class T> inline Values::Field* BindParameter(const Napi::Value source, T pos);
template <class T> T* Bind(Nan::NAN_METHOD_ARGS_TYPE info, int start = 0, int end = -1); template <class T> T* Bind(const Napi::CallbackInfo& info, int start = 0, int end = -1);
bool Bind(const Parameters &parameters); bool Bind(const Parameters &parameters);
static void GetRow(Row* row, sqlite3_stmt* stmt); static void GetRow(Row* row, sqlite3_stmt* stmt);
static Local<Object> RowToJS(Row* row); static Napi::Object RowToJS(Row* row);
void Schedule(Work_Callback callback, Baton* baton); void Schedule(Work_Callback callback, Baton* baton);
void Process(); void Process();
void CleanQueue(); void CleanQueue();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment