From 840e665433b46a1ffd1a0c196297f2a840505324 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 4 Nov 2018 08:55:35 +0100 Subject: [PATCH] Drop Node.js 8, compile for V8 7.1+ --- Makefile | 2 -- src/addon.cpp | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 6507915..80ef015 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,12 @@ CPP_OSX := -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup default: make targets - NODE=targets/node-v8.1.2 ABI=57 make `(uname -s)` NODE=targets/node-v9.2.0 ABI=59 make `(uname -s)` NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)` NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)` for f in dist/*.node; do chmod +x $$f; done targets: mkdir targets - curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets diff --git a/src/addon.cpp b/src/addon.cpp index 8d9f28d..a06dcd4 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -1,10 +1,12 @@ -/* We depend only on raw & vanilla libuv, V8 and OpenSSL. - * There is to be no dependencies on anything Node.js in here. */ -#include +/* This addon should depend on nothing but raw, vanilla Google V8 and µWebSockets. */ +#include "App.h" #include +using namespace v8; +Isolate *isolate; +Persistent resTemplate; +Persistent reqTemplate; #include -using namespace v8; class NativeString { char *data; @@ -12,12 +14,12 @@ class NativeString { char utf8ValueMemory[sizeof(String::Utf8Value)]; String::Utf8Value *utf8Value = nullptr; public: - NativeString(const Local &value) { + NativeString(Isolate *isolate, const Local &value) { if (value->IsUndefined()) { data = nullptr; length = 0; } else if (value->IsString()) { - utf8Value = new (utf8ValueMemory) String::Utf8Value(value); + utf8Value = new (utf8ValueMemory) String::Utf8Value(isolate, value); data = (**utf8Value); length = utf8Value->length(); } else if (value->IsTypedArray()) { @@ -46,17 +48,12 @@ public: } }; -#include "App.h" -Isolate *isolate; -Persistent resTemplate; -Persistent reqTemplate; - void res_end(const FunctionCallbackInfo &args) { // you might want to do extra work here to swap to tryEnd if passed a Buffer? // or always use tryEnd and simply grab the object as persistent? - NativeString data(args[0]); + NativeString data(args.GetIsolate(), args[0]); ((uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength())); // Return this @@ -65,8 +62,8 @@ void res_end(const FunctionCallbackInfo &args) { void res_writeHeader(const FunctionCallbackInfo &args) { // get string - NativeString header(args[0]); - NativeString value(args[1]); + NativeString header(args.GetIsolate(), args[0]); + NativeString value(args.GetIsolate(), args[1]); ((uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength())); @@ -75,7 +72,7 @@ void res_writeHeader(const FunctionCallbackInfo &args) { void req_getHeader(const FunctionCallbackInfo &args) { // get string - NativeString data(args[0]); + NativeString data(args.GetIsolate(), args[0]); char *buf = data.getData(); int length = data.getLength(); std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length)); @@ -86,7 +83,7 @@ void req_getHeader(const FunctionCallbackInfo &args) { void uWS_App_get(const FunctionCallbackInfo &args) { uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); - NativeString nativeString(args[0]); + NativeString nativeString(args.GetIsolate(), args[0]); Persistent *pf = new Persistent(); pf->Reset(args.GetIsolate(), Local::Cast(args[1])); @@ -113,7 +110,7 @@ void uWS_App_get(const FunctionCallbackInfo &args) { void uWS_App_listen(const FunctionCallbackInfo &args) { uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); - int port = args[0]->Uint32Value(); + int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked(); app->listen(port, [&args](auto *token) { Local argv[] = {Boolean::New(isolate, token)}; @@ -124,7 +121,12 @@ 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; @@ -156,10 +158,14 @@ 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 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); @@ -179,6 +185,7 @@ void Main(Local exports) { } }); 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);*/ @@ -210,5 +217,7 @@ void Main(Local exports) { /* 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 */ +#ifndef ADDON_IS_HOST #include NODE_MODULE(uWS, Main) +#endif