This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in the middle putting in event code. broken.
- Loading branch information
Showing
8 changed files
with
146 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "events.h" | ||
|
||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <strings.h> | ||
#include <errno.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
#include <arpa/inet.h> /* inet_ntop */ | ||
#include <netinet/in.h> /* sockaddr_in, sockaddr_in6 */ | ||
|
||
using namespace v8; | ||
using namespace node; | ||
|
||
Persistent<FunctionTemplate> EventEmitter::constructor_template; | ||
|
||
void | ||
EventEmitter::Initialize (v8::Handle<v8::Object> target) | ||
{ | ||
HandleScope scope; | ||
|
||
Local<FunctionTemplate> t = FunctionTemplate::New(); | ||
constructor_template = Persistent<FunctionTemplate>::New(t); | ||
|
||
// All prototype methods are defined in events.js | ||
|
||
target->Set(String::NewSymbol("EventEmitter"), constructor_template->GetFunction()); | ||
} | ||
|
||
bool | ||
EventEmitter::Emit (const char *type, int argc, Handle<Value> argv[]) | ||
{ | ||
Local<Value> emit_v = handle_->Get(String::NewSymbol("emit")); | ||
assert(emit_v->IsFunction()); | ||
Local<Function> emit = Local<Function>::Cast(emit_v); | ||
|
||
Local<Array> event_args = Array::New(argc); | ||
for (int i = 0; i < argc; i++) { | ||
event_args->Set(Integer::New(i), argv[i]); | ||
} | ||
|
||
Handle<Value> emit_argv[2] = { String::NewSymbol(type), event_args }; | ||
|
||
TryCatch try_catch; | ||
|
||
emit->Call(handle_, 2, emit_argv); | ||
|
||
if (try_catch.HasCaught()) { | ||
FatalException(try_catch); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef node_events_h | ||
#define node_events_h | ||
|
||
#include "node.h" | ||
#include <v8.h> | ||
|
||
namespace node { | ||
|
||
class EventEmitter : public ObjectWrap { | ||
public: | ||
static void Initialize (v8::Handle<v8::Object> target); | ||
static v8::Persistent<v8::FunctionTemplate> constructor_template; | ||
|
||
bool Emit (const char *type, int argc, v8::Handle<v8::Value> argv[]); | ||
|
||
EventEmitter (v8::Handle<v8::Object> handle) | ||
: ObjectWrap(handle) { }; | ||
}; | ||
|
||
} // namespace node | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(function () { | ||
|
||
// node.EventEmitter is defined in src/events.cc | ||
var emitter = node.EventEmitter.prototype; | ||
|
||
emitter.addListener = function (type, listener) { | ||
if (!this._events) this._events = {}; | ||
if (!this._events.hasOwnProperty(type)) this._events[type] = []; | ||
this._events[type].push(listener); | ||
}; | ||
|
||
emitter.listeners = function (type, listener) { | ||
if (!this._events) this._events = {}; | ||
if (!this._events.hasOwnProperty(type)) this._events[type] = []; | ||
return this._events[type]; | ||
}; | ||
|
||
/* This function is called often from C++. | ||
* See events.cc | ||
*/ | ||
emitter.emit = function (type, args) { | ||
if (this["on" + type] instanceof Function) { | ||
this["on" + type].apply(this, args); | ||
} | ||
if (!this._events) return; | ||
if (!this._events.hasOwnProperty(type)) return; | ||
for (var i = 0; i < this._events[type].length; i++) { | ||
var listener = this._events[type][i]; | ||
listener.apply(this, args); | ||
} | ||
}; | ||
|
||
})(); // end annonymous namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters