forked from mariano/node-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.cc
53 lines (42 loc) · 1.27 KB
/
events.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2011 Mariano Iglesias <[email protected]>
#include "./events.h"
#if !NODE_VERSION_AT_LEAST(0, 5, 0)
v8::Persistent<v8::String> node_db::EventEmitter::syEmit;
#endif
node_db::EventEmitter::EventEmitter() : node::ObjectWrap() {
}
void node_db::EventEmitter::Init() {
#if !NODE_VERSION_AT_LEAST(0, 5, 0)
syEmit = NODE_PERSISTENT_SYMBOL("emit");
#endif
}
bool node_db::EventEmitter::Emit(const char* event, int argc, v8::Handle<v8::Value> argv[]) {
v8::HandleScope scope;
int nArgc = argc + 1;
v8::Handle<v8::Value>* nArgv = new v8::Handle<v8::Value>[nArgc];
if (nArgv == NULL) {
return false;
}
nArgv[0] = v8::String::New(event);
for (int i=0; i < argc; i++) {
nArgv[i + 1] = argv[i];
}
#if NODE_VERSION_AT_LEAST(0, 5, 0)
node::MakeCallback(this->handle_, "emit", nArgc, nArgv);
#else
v8::Local<v8::Value> emit_v = this->handle_->Get(syEmit);
if (!emit_v->IsFunction()) {
return false;
}
v8::Local<v8::Function> emit = v8::Local<v8::Function>::Cast(emit_v);
v8::TryCatch try_catch;
emit->Call(this->handle_, nArgc, nArgv);
#endif
delete [] nArgv;
#if !NODE_VERSION_AT_LEAST(0, 5, 0)
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
#endif
return true;
}