diff --git a/src/AppWrapper.h b/src/AppWrapper.h index 3e7997c..4a32864 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -66,7 +66,7 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { HandleScope hs(isolate); /* Create a new websocket object */ - Local wsObject = getWsInstance(); + Local wsObject = WebSocketWrapper::getWsInstance(); wsObject->SetAlignedPointerInInternalField(0, ws); /* Attach a new V8 object with pointer to us, to us */ diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index 3bd2724..c6cb765 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -1,47 +1,47 @@ -// ws.getBufferedAmount() -// ws.close(lalala) -// ws.? - -/* Helping QtCreator */ #include "App.h" #include #include "Utilities.h" using namespace v8; -// also wrap all of this in some common struct -Persistent wsTemplate[2]; +struct WebSocketWrapper { + static Persistent wsTemplate[2]; -/* WebSocket send */ + // ws.getBufferedAmount() + // ws.close(lalala) + // ws.? -template -void uWS_WebSocket_send(const FunctionCallbackInfo &args) { - NativeString nativeString(args.GetIsolate(), args[0]); + template + static void uWS_WebSocket_send(const FunctionCallbackInfo &args) { + NativeString nativeString(args.GetIsolate(), args[0]); - bool isBinary = args[1]->Int32Value(); + bool isBinary = args[1]->Int32Value(); - bool ok = ((uWS::WebSocket *) args.Holder()->GetAlignedPointerFromInternalField(0))->send( - std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT - ); + bool ok = ((uWS::WebSocket *) args.Holder()->GetAlignedPointerFromInternalField(0))->send( + std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT + ); - args.GetReturnValue().Set(Boolean::New(isolate, ok)); -} - -template -void initWsTemplate() { - Local wsTemplateLocal = FunctionTemplate::New(isolate); - if (SSL) { - wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket")); - } else { - wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket")); + args.GetReturnValue().Set(Boolean::New(isolate, ok)); } - wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); - wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send)); - Local wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); - wsTemplate[SSL].Reset(isolate, wsObjectLocal); -} + template + static void initWsTemplate() { + Local wsTemplateLocal = FunctionTemplate::New(isolate); + if (SSL) { + wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket")); + } else { + wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket")); + } + wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send)); -template -Local getWsInstance() { - return Local::New(isolate, wsTemplate[std::is_same::value])->Clone(); -} + Local wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); + wsTemplate[SSL].Reset(isolate, wsObjectLocal); + } + + template + static Local getWsInstance() { + return Local::New(isolate, wsTemplate[std::is_same::value])->Clone(); + } +}; + +Persistent WebSocketWrapper::wsTemplate[2]; diff --git a/src/addon.cpp b/src/addon.cpp index 724012d..f6c959f 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -76,8 +76,8 @@ void Main(Local exports) { // these inits should probably happen elsewhere (in their respective struct's constructor)? /* The template for websockets */ - initWsTemplate<0>(); - initWsTemplate<1>(); + WebSocketWrapper::initWsTemplate<0>(); + WebSocketWrapper::initWsTemplate<1>(); /* Initialize SSL and non-SSL templates */ initResTemplate<0>();