Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 157 lines (115 sloc) 4.312 kb
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
1 #include "ffi.h"
2
3 Persistent<FunctionTemplate> CallbackInfo::callback_template;
4 pthread_t CallbackInfo::g_mainthread;
5 pthread_mutex_t CallbackInfo::g_queue_mutex;
6 std::queue<ThreadedCallbackInvokation *> CallbackInfo::g_queue;
ce2a9b4 @TooTallNate ev_async -> uv_async. This officially breaks 0.4.x compat.
TooTallNate authored
7 uv_async_t CallbackInfo::g_async;
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
8
9
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
10 CallbackInfo::CallbackInfo(Handle<Function> func, void *closure, void *code) {
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
11 m_function = Persistent<Function>::New(func);
12 m_closure = closure;
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
13 this->code = code;
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
14 }
15
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
16 CallbackInfo::~CallbackInfo() {
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
17 ffi_closure_free(m_closure);
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
18 m_function.Dispose();
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
19 }
20
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
21 void CallbackInfo::DispatchToV8(CallbackInfo *self, void *retval, void **parameters) {
22 HandleScope scope;
2df33f1 @TooTallNate Add TryCatch to CallbackInfo instances.
TooTallNate authored
23
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
24 Handle<Value> argv[2];
25 argv[0] = Pointer::WrapPointer((unsigned char *)retval);
26 argv[1] = Pointer::WrapPointer((unsigned char *)parameters);
2df33f1 @TooTallNate Add TryCatch to CallbackInfo instances.
TooTallNate authored
27
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
28 TryCatch try_catch;
2df33f1 @TooTallNate Add TryCatch to CallbackInfo instances.
TooTallNate authored
29
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
30 self->m_function->Call(self->m_this, 2, argv);
2df33f1 @TooTallNate Add TryCatch to CallbackInfo instances.
TooTallNate authored
31
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
32 if (try_catch.HasCaught()) {
33 FatalException(try_catch);
34 }
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
35 }
36
ce2a9b4 @TooTallNate ev_async -> uv_async. This officially breaks 0.4.x compat.
TooTallNate authored
37 void CallbackInfo::WatcherCallback(uv_async_t *w, int revents) {
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
38 pthread_mutex_lock(&g_queue_mutex);
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
39
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
40 while (!g_queue.empty()) {
41 ThreadedCallbackInvokation *inv = g_queue.front();
42 g_queue.pop();
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
43
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
44 DispatchToV8(inv->m_cbinfo, inv->m_retval, inv->m_parameters);
45 inv->SignalDoneExecuting();
46 }
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
47
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
48 pthread_mutex_unlock(&g_queue_mutex);
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
49 }
50
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
51 void CallbackInfo::Initialize(Handle<Object> target) {
52 HandleScope scope;
bd33583 @TooTallNate Remove *ALL* trailing whitespace.
TooTallNate authored
53
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
54 if (callback_template.IsEmpty()) {
55 callback_template = Persistent<FunctionTemplate>::New(MakeTemplate());
56 }
bd33583 @TooTallNate Remove *ALL* trailing whitespace.
TooTallNate authored
57
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
58 Handle<FunctionTemplate> t = callback_template;
bd33583 @TooTallNate Remove *ALL* trailing whitespace.
TooTallNate authored
59
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
60 target->Set(String::NewSymbol("CallbackInfo"), t->GetFunction());
bd33583 @TooTallNate Remove *ALL* trailing whitespace.
TooTallNate authored
61
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
62 // initialize our threaded invokation stuff
63 g_mainthread = pthread_self();
ce2a9b4 @TooTallNate ev_async -> uv_async. This officially breaks 0.4.x compat.
TooTallNate authored
64 uv_async_init(uv_default_loop(), &g_async, CallbackInfo::WatcherCallback);
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
65 pthread_mutex_init(&g_queue_mutex, NULL);
ce2a9b4 @TooTallNate ev_async -> uv_async. This officially breaks 0.4.x compat.
TooTallNate authored
66
67 // allow the event loop to exit while this is running
68 uv_unref(uv_default_loop());
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
69 }
70
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
71 Handle<Value> CallbackInfo::New(const Arguments& args) {
72 HandleScope scope;
73
74 if (args.Length() < 2) {
75 return ThrowException(String::New("Not enough arguments."));
76 }
77
78 // Args: cif pointer, JS function
79 // TODO: Check args
80 Pointer *cif = ObjectWrap::Unwrap<Pointer>(args[0]->ToObject());
81 Local<Function> callback = Local<Function>::Cast(args[1]);
82 ffi_closure *closure;
83 ffi_status status;
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
84 void *code;
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
85
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
86 closure = (ffi_closure *)ffi_closure_alloc(sizeof(ffi_closure), &code);
87
88 if (!closure) {
89 return ThrowException(String::New("ffi_closure_alloc() Returned Error"));
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
90 }
91
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
92 CallbackInfo *self = new CallbackInfo(callback, closure, code);
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
93
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
94 status = ffi_prep_closure_loc(
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
95 closure,
96 (ffi_cif *)cif->GetPointer(),
97 Invoke,
b3b880a @TooTallNate Use the ffi_closure_alloc() api for CallbackInfo.
TooTallNate authored
98 (void *)self,
99 code
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
100 );
101
102 if (status != FFI_OK) {
103 delete self;
104 return ThrowException(String::New("ffi_prep_closure() Returned Error"));
105 }
106
107 self->Wrap(args.This());
108 self->m_this = args.This();
109
110 return scope.Close(args.This());
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
111 }
112
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
113 Handle<FunctionTemplate> CallbackInfo::MakeTemplate() {
114 HandleScope scope;
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
115
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
116 Handle<FunctionTemplate> t = FunctionTemplate::New(New);
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
117
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
118 Local<ObjectTemplate> inst = t->InstanceTemplate();
119 inst->SetInternalFieldCount(1);
120 inst->SetAccessor(String::NewSymbol("pointer"), GetPointer);
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
121
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
122 return scope.Close(t);
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
123 }
124
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
125 void CallbackInfo::Invoke(ffi_cif *cif, void *retval, void **parameters, void *user_data) {
126 CallbackInfo *self = (CallbackInfo *)user_data;
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
127
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
128 // are we executing from another thread?
129 if (pthread_equal(pthread_self(), g_mainthread)) {
130 DispatchToV8(self, retval, parameters);
131 } else {
132 // create a temporary storage area for our invokation parameters
133 ThreadedCallbackInvokation *inv = new ThreadedCallbackInvokation(self, retval, parameters);
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
134
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
135 // push it to the queue -- threadsafe
136 pthread_mutex_lock(&g_queue_mutex);
137 g_queue.push(inv);
138 pthread_mutex_unlock(&g_queue_mutex);
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
139
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
140 // send a message to our main thread to wake up the WatchCallback loop
ce2a9b4 @TooTallNate ev_async -> uv_async. This officially breaks 0.4.x compat.
TooTallNate authored
141 uv_async_send(&g_async);
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
142
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
143 // wait for signal from calling thread
144 inv->WaitForExecution();
f65ba37 @TooTallNate Fixing some whitespace issues.
TooTallNate authored
145
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
146 delete inv;
147 }
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
148 }
149
cf91238 @TooTallNate callback_info.cc rewrite. Whitespace and formatting mostly.
TooTallNate authored
150 Handle<Value> CallbackInfo::GetPointer(Local<String> name, const AccessorInfo& info) {
151 HandleScope scope;
152
153 CallbackInfo *self = ObjectWrap::Unwrap<CallbackInfo>(info.Holder());
154 Handle<Value> ptr = Pointer::WrapPointer((unsigned char *)self->m_closure);
155 return scope.Close(ptr);
ab0b75a @rbranson Tree (Re)organization; Added type aliases for common C types; StructT…
rbranson authored
156 }
Something went wrong with that request. Please try again.