From b59e0041b3ee31420c69b0e3c0efe45a310686ec Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 10 Oct 2011 22:03:27 -0500 Subject: [PATCH] native bindings compatible with v0.5.x --- lib/native/index.js | 15 ++++++++++- src/binding.cc | 64 ++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/lib/native/index.js b/lib/native/index.js index 1b6b245a..02d99df9 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -2,12 +2,25 @@ var EventEmitter = require('events').EventEmitter; var utils = require(__dirname + "/../utils"); -var binding = require(__dirname + '/../../build/default/binding'); +var binding; + +try{ + //v0.5.x + binding = require(__dirname + '/../../build/Release/binding.node'); +} catch(e) { + //v0.4.x + binding = require(__dirname + '/../../build/default/binding'); +} + var Connection = binding.Connection; var types = require(__dirname + "/../types"); var NativeQuery = require(__dirname + '/query'); +var EventEmitter = require('events').EventEmitter; var p = Connection.prototype; +for(var k in EventEmitter.prototype) { + p[k] = EventEmitter.prototype[k]; +} var nativeConnect = p.connect; diff --git a/src/binding.cc b/src/binding.cc index 3ec55e7a..5485642d 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1,11 +1,10 @@ #include #include -#include #include #include #include -#define LOG(msg) printf("%s\n",msg) +#define LOG(msg) printf("%s\n",msg); #define TRACE(msg) //printf("%s\n", msg); @@ -14,11 +13,6 @@ using namespace v8; using namespace node; -static Persistent connect_symbol; -static Persistent error_symbol; -static Persistent ready_symbol; -static Persistent row_symbol; -static Persistent notice_symbol; static Persistent severity_symbol; static Persistent code_symbol; static Persistent detail_symbol; @@ -35,8 +29,9 @@ static Persistent value_symbol; static Persistent type_symbol; static Persistent channel_symbol; static Persistent payload_symbol; +static Persistent emit_symbol; -class Connection : public EventEmitter { +class Connection : public ObjectWrap { public: @@ -47,15 +42,10 @@ public: HandleScope scope; Local t = FunctionTemplate::New(New); - t->Inherit(EventEmitter::constructor_template); t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(String::NewSymbol("Connection")); - connect_symbol = NODE_PSYMBOL("connect"); - error_symbol = NODE_PSYMBOL("_error"); - ready_symbol = NODE_PSYMBOL("_readyForQuery"); - notice_symbol = NODE_PSYMBOL("notice"); - row_symbol = NODE_PSYMBOL("_row"); + emit_symbol = NODE_PSYMBOL("emit"); severity_symbol = NODE_PSYMBOL("severity"); code_symbol = NODE_PSYMBOL("code"); detail_symbol = NODE_PSYMBOL("detail"); @@ -229,7 +219,7 @@ public: ev_io write_watcher_; PGconn *connection_; bool connecting_; - Connection () : EventEmitter () + Connection () : ObjectWrap () { connection_ = NULL; connecting_ = false; @@ -348,7 +338,7 @@ protected: { HandleScope scope; Handle notice = String::New(message); - Emit(notice_symbol, 1, ¬ice); + Emit("notice", ¬ice); } //called to process io_events from libev @@ -386,7 +376,7 @@ protected: } //might have fired from notification if(didHandleResult) { - Emit(ready_symbol, 0, NULL); + Emit("_readyForQuery"); } } @@ -396,7 +386,7 @@ protected: result->Set(channel_symbol, String::New(notify->relname)); result->Set(payload_symbol, String::New(notify->extra)); Handle res = (Handle)result; - Emit((Handle)String::New("notification"), 1, &res); + Emit("notification", &res); PQfreemem(notify); } @@ -464,7 +454,7 @@ protected: //not sure about what to dealloc or scope#Close here Handle e = (Handle)row; - Emit(row_symbol, 1, &e); + Emit("_row", &e); } } @@ -487,7 +477,7 @@ protected: AttachErrorField(result, msg, line_symbol, PG_DIAG_SOURCE_LINE); AttachErrorField(result, msg, routine_symbol, PG_DIAG_SOURCE_FUNCTION); Handle m = msg; - Emit(error_symbol, 1, &m); + Emit("_error", &m); } void AttachErrorField(const PGresult *result, const Local msg, const Persistent symbol, int fieldcode) @@ -506,6 +496,33 @@ protected: } private: + //EventEmitter was removed from c++ in node v0.5.x + void Emit(char* message) { + HandleScope scope; + Handle args[1] = { String::New(message) }; + Emit(1, args); + } + + void Emit(char* message, Handle* arg) { + HandleScope scope; + Handle args[2] = { String::New(message), *arg }; + Emit(2, args); + } + + void Emit(int length, Handle *args) { + HandleScope scope; + + Local emit_v = this->handle_->Get(emit_symbol); + assert(emit_v->IsFunction()); + Local emit_f = emit_v.As(); + + TryCatch tc; + emit_f->Call(this->handle_, length, args); + if(tc.HasCaught()) { + FatalException(tc); + } + } + void HandleConnectionIO() { PostgresPollingStatusType status = PQconnectPoll(connection_); @@ -530,7 +547,7 @@ private: TRACE("Polled: PGRES_POLLING_OK"); connecting_ = false; StartRead(); - Emit(connect_symbol, 0, NULL); + Emit("connect"); default: //printf("Unknown polling status: %d\n", status); break; @@ -540,7 +557,7 @@ private: void EmitError(const char *message) { Local exception = Exception::Error(String::New(message)); - Emit(error_symbol, 1, &exception); + Emit("_error", &exception); } void EmitLastError() @@ -624,8 +641,7 @@ private: }; -extern "C" void -init (Handle target) +extern "C" void init (Handle target) { HandleScope scope; Connection::Init(target);