|
| 1 | +#include "node.h" |
| 2 | +#include "uv.h" |
| 3 | + |
| 4 | +#include <assert.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#ifdef NDEBUG |
| 10 | +#define CHECK(x) do { if (!(x)) abort(); } while (false) |
| 11 | +#else |
| 12 | +#define CHECK assert |
| 13 | +#endif |
| 14 | + |
| 15 | +#define CHECK_EQ(a, b) CHECK((a) == (b)) |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +struct Callback { |
| 20 | + inline Callback(v8::Isolate* isolate, |
| 21 | + v8::Local<v8::Object> object, |
| 22 | + v8::Local<v8::Function> function) |
| 23 | + : object(isolate, object), function(isolate, function) {} |
| 24 | + |
| 25 | + v8::Persistent<v8::Object> object; |
| 26 | + v8::Persistent<v8::Function> function; |
| 27 | +}; |
| 28 | + |
| 29 | +static uv_async_t async_handle; |
| 30 | +static std::vector<Callback*> callbacks; |
| 31 | + |
| 32 | +inline void Prime() { |
| 33 | + uv_ref(reinterpret_cast<uv_handle_t*>(&async_handle)); |
| 34 | + CHECK_EQ(0, uv_async_send(&async_handle)); |
| 35 | +} |
| 36 | + |
| 37 | +inline void AsyncCallback(uv_async_t*) { |
| 38 | + auto isolate = v8::Isolate::GetCurrent(); |
| 39 | + v8::HandleScope handle_scope(isolate); |
| 40 | + auto context = isolate->GetCurrentContext(); |
| 41 | + auto global = context->Global(); |
| 42 | + while (!callbacks.empty()) { |
| 43 | + v8::HandleScope handle_scope(isolate); |
| 44 | + auto callback = callbacks.back(); |
| 45 | + callbacks.pop_back(); |
| 46 | + auto function = v8::Local<v8::Function>::New(isolate, callback->function); |
| 47 | + delete callback; |
| 48 | + if (node::MakeCallback(isolate, global, function, 0, nullptr).IsEmpty()) |
| 49 | + return Prime(); // Have exception, flush remainder on next loop tick. |
| 50 | + } |
| 51 | + uv_unref(reinterpret_cast<uv_handle_t*>(&async_handle)); |
| 52 | +} |
| 53 | + |
| 54 | +inline void OnGC(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 55 | + CHECK(info[0]->IsObject()); |
| 56 | + CHECK(info[1]->IsFunction()); |
| 57 | + auto object = info[0].As<v8::Object>(); |
| 58 | + auto function = info[1].As<v8::Function>(); |
| 59 | + auto callback = new Callback(info.GetIsolate(), object, function); |
| 60 | + auto on_callback = [] (const v8::WeakCallbackInfo<Callback>& data) { |
| 61 | + auto callback = data.GetParameter(); |
| 62 | + callbacks.push_back(callback); |
| 63 | + callback->object.Reset(); |
| 64 | + Prime(); |
| 65 | + }; |
| 66 | + callback->object.SetWeak(callback, on_callback, |
| 67 | + v8::WeakCallbackType::kParameter); |
| 68 | +} |
| 69 | + |
| 70 | +inline void Initialize(v8::Local<v8::Object> exports, |
| 71 | + v8::Local<v8::Value> module, |
| 72 | + v8::Local<v8::Context> context) { |
| 73 | + NODE_SET_METHOD(module->ToObject(context).ToLocalChecked(), "exports", OnGC); |
| 74 | + CHECK_EQ(0, uv_async_init(uv_default_loop(), &async_handle, AsyncCallback)); |
| 75 | + uv_unref(reinterpret_cast<uv_handle_t*>(&async_handle)); |
| 76 | +} |
| 77 | + |
| 78 | +} // anonymous namespace |
| 79 | + |
| 80 | +NODE_MODULE_CONTEXT_AWARE(binding, Initialize) |
0 commit comments