forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_events.cc
More file actions
214 lines (150 loc) · 4.89 KB
/
node_events.cc
File metadata and controls
214 lines (150 loc) · 4.89 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#include <node_events.h>
#include <node.h>
#define RECORD 0
namespace node {
using namespace v8;
Persistent<FunctionTemplate> EventEmitter::constructor_template;
static Persistent<String> events_symbol;
void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
HandleScope scope;
constructor_template = Persistent<FunctionTemplate>::New(ctemplate);
constructor_template->SetClassName(String::NewSymbol("EventEmitter"));
events_symbol = NODE_PSYMBOL("_events");
// All other prototype methods are defined in events.js
}
bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
HandleScope scope;
// HandleScope not needed here because only called from one of the two
// functions below
Local<Value> events_v = handle_->Get(events_symbol);
if (!events_v->IsObject()) return false;
Local<Object> events = events_v->ToObject();
Local<Value> listeners_v = events->Get(event);
TryCatch try_catch;
if (listeners_v->IsFunction()) {
// Optimized one-listener case
Local<Function> listener = Local<Function>::Cast(listeners_v);
listener->Call(handle_, argc, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
return false;
}
} else if (listeners_v->IsArray()) {
Local<Array> listeners = Local<Array>::Cast(listeners_v->ToObject()->Clone());
for (uint32_t i = 0; i < listeners->Length(); i++) {
Local<Value> listener_v = listeners->Get(i);
if (!listener_v->IsFunction()) continue;
Local<Function> listener = Local<Function>::Cast(listener_v);
listener->Call(handle_, argc, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
return false;
}
}
} else {
return false;
}
return true;
}
// EventSource
EventSource* EventSource::current_source;
Local<Value> EventSource::MakeCallback(int argc, Handle<Value> argv[]) {
HandleScope scope;
Local<Value> callback_v = handle_->Get(String::NewSymbol("callback"));
if (!callback_v->IsFunction()) return Local<Value>();
Local<Function> callback = Local<Function>::Cast(callback_v);
// TODO DTrace probe here.
TryCatch try_catch;
assert(current_source == NULL);
current_source = this;
Local<Value> ret = callback->Call(handle_, argc, argv);
current_source = NULL;
if (try_catch.HasCaught()) {
// TODO Look for 'uncaughtException' handlers.
// Here we print the stack trace from try_catch
ReportException(try_catch, true);
// Then we print the stored stacktrace plus our ancestors stacks.
PrintStack();
// XXX Stop whatever activity might have been going on?
exit(1);
}
// Call nextTick callbacks?
return scope.Close(ret);
}
void EventSource::PrintStack(int count) {
if (trace_.IsEmpty()) return;
// Print from for this EventSource
fprintf(stderr, " ---------------------------\n");
int length = trace_->GetFrameCount();
for (int i = 0; i < length; i++) {
Local<StackFrame> frame = trace_->GetFrame(i);
String::Utf8Value script_name(frame->GetScriptName());
String::Utf8Value function_name(frame->GetFunctionName());
int column = frame->GetColumn();
int line_number = frame->GetLineNumber();
// how do you cast Value to StackFrame ?
// print frame somehow...
/*
fprintf(stderr,
" at %s (%s:%d:%d)\n",
*function_name,
*script_name,
line_number,
column);
*/
fprintf(stderr,
" at %s %d\n",
*script_name,
line_number);
}
// Recursively print up to kAncestorStackLimit ancestor traces...
if (parent_source_.IsEmpty() == false && count < kAncestorStackLimit) {
EventSource *parent = ObjectWrap::Unwrap<EventSource>(parent_source_);
parent->PrintStack(count + 1);
}
}
void EventSource::Active() {
Ref();
RecordStack();
// TODO DTrace probe here.
}
void EventSource::Inactive() {
DeleteParent();
ClearStack();
Unref();
}
void EventSource::ClearStack() {
if (!trace_.IsEmpty()) {
trace_.Dispose();
trace_.Clear();
}
}
void EventSource::RecordStack() {
HandleScope scope;
ClearStack();
// Assume inside HandleScope
#if RECORD
Local<StackTrace> trace =
StackTrace::CurrentStackTrace(kFrameLimit, StackTrace::kOverview);
trace_ = Persistent<StackTrace>::New(trace);
// Set parent.
if (current_source) {
parent_source_ = Persistent<Object>::New(current_source->handle_);
parent_source_.MakeWeak(this, WeakParent);
}
#endif
}
void EventSource::WeakParent(Persistent<Value> object, void* data) {
EventSource *s = static_cast<EventSource*>(data);
assert(s->parent_source_->StrictEquals(object));
s->DeleteParent();
}
void EventSource::DeleteParent() {
if (!parent_source_.IsEmpty()) {
parent_source_.ClearWeak();
parent_source_.Dispose();
parent_source_.Clear();
}
}
} // namespace node