Commit bb15108e by King Koopa

Use NAN for future support.

parent fe69599b
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
"targets": [ "targets": [
{ {
"target_name": "<(module_name)", "target_name": "<(module_name)",
"include_dirs": ["<!(node -e \"require('nan')\")"],
"conditions": [ "conditions": [
["sqlite != 'internal'", { ["sqlite != 'internal'", {
"libraries": [ "libraries": [
......
...@@ -35,7 +35,11 @@ ...@@ -35,7 +35,11 @@
"url": "git://github.com/mapbox/node-sqlite3.git" "url": "git://github.com/mapbox/node-sqlite3.git"
}, },
"dependencies": { "dependencies": {
"node-pre-gyp": "~0.2.5" "node-pre-gyp": "~0.2.5",
"nan": "~0.8.0"
},
"devDependencies": {
"set-immediate": "~0.1.1"
}, },
"bundledDependencies": [ "bundledDependencies": [
"node-pre-gyp" "node-pre-gyp"
...@@ -46,7 +50,7 @@ ...@@ -46,7 +50,7 @@
"scripts": { "scripts": {
"install": "node-pre-gyp install --fallback-to-build", "install": "node-pre-gyp install --fallback-to-build",
"pretest": "node test/support/createdb.js", "pretest": "node test/support/createdb.js",
"test": "mocha -R spec --timeout 200000" "test": "mocha -R spec --timeout 200000 -s 20000"
}, },
"licenses": [ "licenses": [
{ {
...@@ -54,4 +58,4 @@ ...@@ -54,4 +58,4 @@
} }
], ],
"main": "./lib/sqlite3" "main": "./lib/sqlite3"
} }
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <queue> #include <queue>
#include <sqlite3.h> #include <sqlite3.h>
#include "nan.h"
#include "async.h" #include "async.h"
using namespace v8; using namespace v8;
...@@ -25,7 +26,7 @@ public: ...@@ -25,7 +26,7 @@ public:
static inline bool HasInstance(Handle<Value> val) { static inline bool HasInstance(Handle<Value> val) {
if (!val->IsObject()) return false; if (!val->IsObject()) return false;
Local<Object> obj = val->ToObject(); Local<Object> obj = val->ToObject();
return constructor_template->HasInstance(obj); return NanPersistentToLocal(constructor_template)->HasInstance(obj);
} }
struct Baton { struct Baton {
...@@ -39,7 +40,7 @@ public: ...@@ -39,7 +40,7 @@ public:
db(db_), status(SQLITE_OK) { db(db_), status(SQLITE_OK) {
db->Ref(); db->Ref();
request.data = this; request.data = this;
callback = Persistent<Function>::New(cb_); NanAssignPersistent(Function, callback, cb_);
} }
virtual ~Baton() { virtual ~Baton() {
db->Unref(); db->Unref();
...@@ -99,7 +100,7 @@ public: ...@@ -99,7 +100,7 @@ public:
protected: protected:
Database() : ObjectWrap(), Database() : ObjectWrap(),
handle(NULL), _handle(NULL),
open(false), open(false),
locked(false), locked(false),
pending(0), pending(0),
...@@ -112,43 +113,43 @@ protected: ...@@ -112,43 +113,43 @@ protected:
~Database() { ~Database() {
RemoveCallbacks(); RemoveCallbacks();
sqlite3_close(handle); sqlite3_close(_handle);
handle = NULL; _handle = NULL;
open = false; open = false;
} }
static Handle<Value> New(const Arguments& args); static NAN_METHOD(New);
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 Handle<Value> OpenGetter(Local<String> str, const AccessorInfo& accessor); static NAN_GETTER(OpenGetter);
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 Handle<Value> Exec(const Arguments& args); static NAN_METHOD(Exec);
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 Handle<Value> Wait(const Arguments& args); static NAN_METHOD(Wait);
static void Work_Wait(Baton* baton); static void Work_Wait(Baton* baton);
static Handle<Value> Close(const Arguments& args); static NAN_METHOD(Close);
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 Handle<Value> LoadExtension(const Arguments& args); static NAN_METHOD(LoadExtension);
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 Handle<Value> Serialize(const Arguments& args); static NAN_METHOD(Serialize);
static Handle<Value> Parallelize(const Arguments& args); static NAN_METHOD(Parallelize);
static Handle<Value> Configure(const Arguments& args); static NAN_METHOD(Configure);
static void SetBusyTimeout(Baton* baton); static void SetBusyTimeout(Baton* baton);
...@@ -167,7 +168,7 @@ protected: ...@@ -167,7 +168,7 @@ protected:
void RemoveCallbacks(); void RemoveCallbacks();
protected: protected:
sqlite3* handle; sqlite3* _handle;
bool open; bool open;
bool locked; bool locked;
......
...@@ -7,35 +7,27 @@ const char* sqlite_authorizer_string(int type); ...@@ -7,35 +7,27 @@ const char* sqlite_authorizer_string(int type);
#define REQUIRE_ARGUMENTS(n) \ #define REQUIRE_ARGUMENTS(n) \
if (args.Length() < (n)) { \ if (args.Length() < (n)) { \
return ThrowException( \ return NanThrowTypeError("Expected " #n "arguments"); \
Exception::TypeError(String::New("Expected " #n "arguments")) \
); \
} }
#define REQUIRE_ARGUMENT_EXTERNAL(i, var) \ #define REQUIRE_ARGUMENT_EXTERNAL(i, var) \
if (args.Length() <= (i) || !args[i]->IsExternal()) { \ if (args.Length() <= (i) || !args[i]->IsExternal()) { \
return ThrowException( \ return NanThrowTypeError("Argument " #i " invalid"); \
Exception::TypeError(String::New("Argument " #i " invalid")) \
); \
} \ } \
Local<External> var = Local<External>::Cast(args[i]); Local<External> var = Local<External>::Cast(args[i]);
#define REQUIRE_ARGUMENT_FUNCTION(i, var) \ #define REQUIRE_ARGUMENT_FUNCTION(i, var) \
if (args.Length() <= (i) || !args[i]->IsFunction()) { \ if (args.Length() <= (i) || !args[i]->IsFunction()) { \
return ThrowException(Exception::TypeError( \ return NanThrowTypeError("Argument " #i " must be a function"); \
String::New("Argument " #i " must be a function")) \
); \
} \ } \
Local<Function> var = Local<Function>::Cast(args[i]); Local<Function> var = Local<Function>::Cast(args[i]);
#define REQUIRE_ARGUMENT_STRING(i, var) \ #define REQUIRE_ARGUMENT_STRING(i, var) \
if (args.Length() <= (i) || !args[i]->IsString()) { \ if (args.Length() <= (i) || !args[i]->IsString()) { \
return ThrowException(Exception::TypeError( \ return NanThrowTypeError("Argument " #i " must be a string"); \
String::New("Argument " #i " must be a string")) \
); \
} \ } \
String::Utf8Value var(args[i]->ToString()); String::Utf8Value var(args[i]->ToString());
...@@ -44,9 +36,7 @@ const char* sqlite_authorizer_string(int type); ...@@ -44,9 +36,7 @@ const char* sqlite_authorizer_string(int type);
Local<Function> var; \ Local<Function> var; \
if (args.Length() > i && !args[i]->IsUndefined()) { \ if (args.Length() > i && !args[i]->IsUndefined()) { \
if (!args[i]->IsFunction()) { \ if (!args[i]->IsFunction()) { \
return ThrowException(Exception::TypeError( \ return NanThrowTypeError("Argument " #i " must be a function"); \
String::New("Argument " #i " must be a function")) \
); \
} \ } \
var = Local<Function>::Cast(args[i]); \ var = Local<Function>::Cast(args[i]); \
} }
...@@ -61,9 +51,7 @@ const char* sqlite_authorizer_string(int type); ...@@ -61,9 +51,7 @@ const char* sqlite_authorizer_string(int type);
var = args[i]->Int32Value(); \ var = args[i]->Int32Value(); \
} \ } \
else { \ else { \
return ThrowException(Exception::TypeError( \ return NanThrowTypeError("Argument " #i " must be an integer"); \
String::New("Argument " #i " must be an integer")) \
); \
} }
...@@ -103,8 +91,8 @@ const char* sqlite_authorizer_string(int type); ...@@ -103,8 +91,8 @@ const char* sqlite_authorizer_string(int type);
) \ ) \
); \ ); \
Local<Object> name ##_obj = name->ToObject(); \ Local<Object> name ##_obj = name->ToObject(); \
name ##_obj->Set(NODE_PSYMBOL("errno"), Integer::New(errno)); \ name ##_obj->Set(String::NewSymbol("errno"), Integer::New(errno)); \
name ##_obj->Set(NODE_PSYMBOL("code"), \ name ##_obj->Set(String::NewSymbol("code"), \
String::NewSymbol(sqlite_code_string(errno))); String::NewSymbol(sqlite_code_string(errno)));
...@@ -121,8 +109,8 @@ const char* sqlite_authorizer_string(int type); ...@@ -121,8 +109,8 @@ const char* sqlite_authorizer_string(int type);
FatalException(try_catch); \ FatalException(try_catch); \
} } } }
#define WORK_DEFINITION(name) \ #define WORK_DEFINITION(name) \
static Handle<Value> name(const Arguments& args); \ static NAN_METHOD(name); \
static void Work_Begin##name(Baton* baton); \ static void Work_Begin##name(Baton* baton); \
static void Work_##name(uv_work_t* req); \ static void Work_##name(uv_work_t* req); \
static void Work_After##name(uv_work_t* req); static void Work_After##name(uv_work_t* req);
...@@ -164,4 +152,3 @@ const char* sqlite_authorizer_string(int type); ...@@ -164,4 +152,3 @@ const char* sqlite_authorizer_string(int type);
} }
#endif #endif
...@@ -103,4 +103,4 @@ const char* sqlite_authorizer_string(int type) { ...@@ -103,4 +103,4 @@ const char* sqlite_authorizer_string(int type) {
} }
} }
NODE_MODULE(node_sqlite3, RegisterModule); NODE_MODULE(node_sqlite3, RegisterModule)
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <sqlite3.h> #include <sqlite3.h>
#include "nan.h"
using namespace v8; using namespace v8;
using namespace node; using namespace node;
...@@ -76,7 +77,7 @@ public: ...@@ -76,7 +77,7 @@ public:
static Persistent<FunctionTemplate> constructor_template; static Persistent<FunctionTemplate> constructor_template;
static void Init(Handle<Object> target); static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments& args); static NAN_METHOD(New);
struct Baton { struct Baton {
uv_work_t request; uv_work_t request;
...@@ -87,7 +88,7 @@ public: ...@@ -87,7 +88,7 @@ public:
Baton(Statement* stmt_, Handle<Function> cb_) : stmt(stmt_) { Baton(Statement* stmt_, Handle<Function> cb_) : stmt(stmt_) {
stmt->Ref(); stmt->Ref();
request.data = this; request.data = this;
callback = Persistent<Function>::New(cb_); NanAssignPersistent(Function, callback, cb_);
} }
virtual ~Baton() { virtual ~Baton() {
for (unsigned int i = 0; i < parameters.size(); i++) { for (unsigned int i = 0; i < parameters.size(); i++) {
...@@ -183,7 +184,7 @@ public: ...@@ -183,7 +184,7 @@ public:
Statement(Database* db_) : ObjectWrap(), Statement(Database* db_) : ObjectWrap(),
db(db_), db(db_),
handle(NULL), _handle(NULL),
status(SQLITE_OK), status(SQLITE_OK),
prepared(false), prepared(false),
locked(true), locked(true),
...@@ -202,7 +203,7 @@ public: ...@@ -202,7 +203,7 @@ public:
WORK_DEFINITION(Each); WORK_DEFINITION(Each);
WORK_DEFINITION(Reset); WORK_DEFINITION(Reset);
static Handle<Value> Finalize(const Arguments& args); static NAN_METHOD(Finalize);
protected: protected:
static void Work_BeginPrepare(Database::Baton* baton); static void Work_BeginPrepare(Database::Baton* baton);
...@@ -216,8 +217,8 @@ protected: ...@@ -216,8 +217,8 @@ protected:
void Finalize(); void Finalize();
template <class T> inline Values::Field* BindParameter(const Handle<Value> source, T pos); template <class T> inline Values::Field* BindParameter(const Handle<Value> source, T pos);
template <class T> T* Bind(const Arguments& args, int start = 0, int end = -1); template <class T> T* Bind(_NAN_METHOD_ARGS, 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 Local<Object> RowToJS(Row* row);
...@@ -229,7 +230,7 @@ protected: ...@@ -229,7 +230,7 @@ protected:
protected: protected:
Database* db; Database* db;
sqlite3_stmt* handle; sqlite3_stmt* _handle;
int status; int status;
std::string message; std::string message;
......
require('set-immediate');
var sqlite3 = require('..'); var sqlite3 = require('..');
var assert = require('assert'); var assert = require('assert');
var helper = require('./support/helper'); var helper = require('./support/helper');
...@@ -30,7 +31,7 @@ describe('cache', function() { ...@@ -30,7 +31,7 @@ describe('cache', function() {
var db1, db2; var db1, db2;
db1 = new sqlite3.cached.Database(filename, function(err) { db1 = new sqlite3.cached.Database(filename, function(err) {
if (err) throw err; if (err) throw err;
process.nextTick(function() { setImmediate(function() {
db2 = new sqlite3.cached.Database(filename, function(err) { db2 = new sqlite3.cached.Database(filename, function(err) {
done(); done();
......
require('set-immediate');
var sqlite3 = require('..'); var sqlite3 = require('..');
var assert = require('assert'); var assert = require('assert');
...@@ -31,7 +32,7 @@ describe('profiling', function() { ...@@ -31,7 +32,7 @@ describe('profiling', function() {
assert.ok(!create); assert.ok(!create);
db.run("CREATE TABLE foo (id int)", function(err) { db.run("CREATE TABLE foo (id int)", function(err) {
if (err) throw err; if (err) throw err;
process.nextTick(function() { setImmediate(function() {
assert.ok(create); assert.ok(create);
done(); done();
}); });
...@@ -43,7 +44,7 @@ describe('profiling', function() { ...@@ -43,7 +44,7 @@ describe('profiling', function() {
assert.ok(!select); assert.ok(!select);
db.run("SELECT * FROM foo", function(err) { db.run("SELECT * FROM foo", function(err) {
if (err) throw err; if (err) throw err;
process.nextTick(function() { setImmediate(function() {
assert.ok(select); assert.ok(select);
done(); done();
}); });
......
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