From fae59e871ada19073463d55cef9233b4d690fdfb Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 4 Nov 2018 10:56:51 +0100 Subject: [PATCH] Remove libuv dependencies --- src/addon.cpp | 54 +++++++++++++++++++-------------------------------- uWebSockets | 2 +- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/addon.cpp b/src/addon.cpp index a06dcd43..81e04a8a 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -1,4 +1,4 @@ -/* This addon should depend on nothing but raw, vanilla Google V8 and µWebSockets. */ +/* We are only allowed to depend on µWS and V8 in this layer. */ #include "App.h" #include using namespace v8; @@ -7,6 +7,8 @@ Persistent resTemplate; Persistent reqTemplate; #include +#include +std::vector>> nextTickQueue; class NativeString { char *data; @@ -121,15 +123,6 @@ void uWS_App_listen(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(args.Holder()); } -// should absolutely not depend on libuv here! -#ifndef ADDON_IS_HOST -#include -uv_check_t check; -#endif - -#include -std::vector>> nextTickQueue; - // we need to override process.nextTick to avoid horrible loss of performance by Node.js void nextTick(const FunctionCallbackInfo &args) { nextTickQueue.push_back(Persistent>(isolate, Local::Cast(args[0]))); @@ -158,34 +151,28 @@ void uWS_App(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(localApp); } -// we should absolutely not depend on libuv here -// move this over to depend on µWS's loop post features +void emptyNextTickQueue(Isolate *isolate) { + if (nextTickQueue.size()) { + HandleScope hs(isolate); + + for (Persistent> &f : nextTickQueue) { + Local::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr); + f.Reset(); + } + + nextTickQueue.clear(); + } +} void Main(Local exports) { isolate = exports->GetIsolate(); -#ifndef ADDON_IS_HOST - /* uWS.nextTick is executed in uv_check_t */ - uv_loop_t *loop = uv_default_loop(); - uv_check_init(loop, &check); - check.data = isolate; - uv_check_start(&check, [](uv_check_t *check) { - - if (nextTickQueue.size()) { - Isolate *isolate = (Isolate *) check->data; - HandleScope hs(isolate); - - for (Persistent> &f : nextTickQueue) { - Local::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr); - f.Reset(); - } - - nextTickQueue.clear(); - } + /* Register our own nextTick handler */ + /* We should probably also do this in pre, just to be sure */ + uWS::Loop::defaultLoop()->setPostHandler([isolate](uWS::Loop *) { + emptyNextTickQueue(isolate); }); - uv_unref((uv_handle_t *) &check); -#endif /*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url); reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/ @@ -215,8 +202,7 @@ void Main(Local exports) { reqTemplate.Reset(isolate, reqObjectLocal); } -/* This is the only part where we are allowed/forced to add Node.js specific code. - * We do this because we are forced to, and we need a node module version added */ +/* This is required when building as a Node.js addon */ #ifndef ADDON_IS_HOST #include NODE_MODULE(uWS, Main) diff --git a/uWebSockets b/uWebSockets index 0d09d1db..894609e6 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 0d09d1db02de7c51eb72b71925e86ff45aaaa160 +Subproject commit 894609e6381b03d592889b906314544c58fc2511