@@ -6,6 +6,11 @@ using namespace v8;
66/* uWS.App.ws('/pattern', behavior) */
77template <typename APP >
88void uWS_App_ws (const FunctionCallbackInfo<Value> &args) {
9+
10+ Isolate *isolate = args.GetIsolate ();
11+
12+ PerContextData *perContextData = (PerContextData *) Local<External>::Cast (args.Data ())->Value ();
13+
914 APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
1015 /* This one is default constructed with defaults */
1116 typename APP ::WebSocketBehavior behavior = {};
@@ -63,15 +68,16 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
6368 }
6469
6570 /* Open handler is NOT optional for the wrapper */
66- behavior.open = [openPf = std::move (openPf)](auto *ws, auto *req) {
71+ behavior.open = [openPf = std::move (openPf), perContextData](auto *ws, auto *req) {
72+ Isolate *isolate = perContextData->isolate ;
6773 HandleScope hs (isolate);
6874
6975 /* Create a new websocket object */
70- Local<Object> wsObject = WebSocketWrapper::getWsInstance <APP >();
76+ Local<Object> wsObject = perContextData-> wsTemplate [getAppTypeIndex <APP >()]. Get (isolate)-> Clone ();
7177 wsObject->SetAlignedPointerInInternalField (0 , ws);
7278
7379 /* Create the HttpRequest wrapper */
74- Local<Object> reqObject = HttpRequestWrapper::getReqInstance ();
80+ Local<Object> reqObject = perContextData-> reqTemplate . Get (isolate)-> Clone ();
7581 reqObject->SetAlignedPointerInInternalField (0 , req);
7682
7783 /* Attach a new V8 object with pointer to us, to us */
@@ -88,7 +94,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
8894
8995 /* Message handler is always optional */
9096 if (messagePf != Undefined (isolate)) {
91- behavior.message = [messagePf = std::move (messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
97+ behavior.message = [messagePf = std::move (messagePf), isolate ](auto *ws, std::string_view message, uWS::OpCode opCode) {
9298 HandleScope hs (isolate);
9399
94100 Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New (isolate, (void *) message.data (), message.length ());
@@ -106,7 +112,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
106112
107113 /* Drain handler is always optional */
108114 if (drainPf != Undefined (isolate)) {
109- behavior.drain = [drainPf = std::move (drainPf)](auto *ws) {
115+ behavior.drain = [drainPf = std::move (drainPf), isolate ](auto *ws) {
110116 HandleScope hs (isolate);
111117
112118 PerSocketData *perSocketData = (PerSocketData *) ws->getUserData ();
@@ -126,7 +132,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
126132 };
127133
128134 /* Close handler is NOT optional for the wrapper */
129- behavior.close = [closePf = std::move (closePf)](auto *ws, int code, std::string_view message) {
135+ behavior.close = [closePf = std::move (closePf), isolate ](auto *ws, int code, std::string_view message) {
130136 HandleScope hs (isolate);
131137
132138 Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New (isolate, (void *) message.data (), message.length ());
@@ -165,21 +171,22 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
165171 return ;
166172 }
167173
168- /* todo: make it UniquePersistent */
169- std::shared_ptr<Persistent<Function>> pf ( new Persistent<Function> );
170- pf-> Reset (args.GetIsolate (), Local<Function>::Cast (args[1 ]));
174+ /* This function requires perContextData */
175+ PerContextData *perContextData = (PerContextData *) Local<External>:: Cast (args. Data ())-> Value ( );
176+ UniquePersistent<Function> cb (args.GetIsolate (), Local<Function>::Cast (args[1 ]));
171177
172- (app->*f)(std::string (pattern.getString ()), [pf](auto *res, auto *req) {
178+ (app->*f)(std::string (pattern.getString ()), [cb = std::move (cb), perContextData](auto *res, auto *req) {
179+ Isolate *isolate = perContextData->isolate ;
173180 HandleScope hs (isolate);
174181
175- Local<Object> resObject = HttpResponseWrapper::getResInstance <APP >();
182+ Local<Object> resObject = perContextData-> resTemplate [getAppTypeIndex <APP >()]. Get (isolate)-> Clone ();
176183 resObject->SetAlignedPointerInInternalField (0 , res);
177184
178- Local<Object> reqObject = HttpRequestWrapper::getReqInstance ();
185+ Local<Object> reqObject = perContextData-> reqTemplate . Get (isolate)-> Clone ();
179186 reqObject->SetAlignedPointerInInternalField (0 , req);
180187
181188 Local<Value> argv[] = {resObject, reqObject};
182- Local<Function>:: New (isolate, *pf )->Call (isolate->GetCurrentContext (), isolate->GetCurrentContext ()->Global (), 2 , argv).IsEmpty ();
189+ cb. Get (isolate)->Call (isolate->GetCurrentContext (), isolate->GetCurrentContext ()->Global (), 2 , argv).IsEmpty ();
183190
184191 /* Properly invalidate req */
185192 reqObject->SetAlignedPointerInInternalField (0 , nullptr );
@@ -195,6 +202,8 @@ template <typename APP>
195202void uWS_App_listen (const FunctionCallbackInfo<Value> &args) {
196203 APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
197204
205+ Isolate *isolate = args.GetIsolate ();
206+
198207 /* Require at least two arguments */
199208 if (args.Length () < 2 ) {
200209 /* Throw here */
@@ -203,7 +212,7 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
203212 }
204213
205214 /* Callback is last */
206- auto cb = [&args](auto *token) {
215+ auto cb = [&args, isolate ](auto *token) {
207216 /* Return a false boolean if listen failed */
208217 Local<Value> argv[] = {token ? Local<Value>::Cast (External::New (isolate, token)) : Local<Value>::Cast (Boolean::New (isolate, false ))};
209218 Local<Function>::Cast (args[args.Length () - 1 ])->Call (isolate->GetCurrentContext (), isolate->GetCurrentContext ()->Global (), 1 , argv).IsEmpty ();
@@ -236,29 +245,35 @@ template <typename APP>
236245void uWS_App_publish (const FunctionCallbackInfo<Value> &args) {
237246 APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
238247
248+ Isolate *isolate = args.GetIsolate ();
249+
239250 NativeString topic (isolate, args[0 ]);
240251 NativeString message (isolate, args[1 ]);
241252 app->publish (topic.getString (), message.getString (), BooleanValue (isolate, args[2 ]) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT , BooleanValue (isolate, args[3 ]));
242253}
243254
244255template <typename APP >
245256void uWS_App (const FunctionCallbackInfo<Value> &args) {
257+
258+ Isolate *isolate = args.GetIsolate ();
259+
246260 Local<FunctionTemplate> appTemplate = FunctionTemplate::New (isolate);
247261
248262 APP *app;
249263
264+ /* These won't outlive the function, uSockets will have to copy strings it wants to keep! */
265+ std::string keyFileName;
266+ std::string certFileName;
267+ std::string passphrase;
268+ std::string dhParamsFileName;
269+
250270 /* Name differs based on type */
251271 if (std::is_same<APP , uWS::SSLApp>::value) {
252272 appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.SSLApp" , NewStringType::kNormal ).ToLocalChecked ());
253273
254274 /* We fill these below */
255275 us_socket_context_options_t ssl_options = {};
256276
257- static std::string keyFileName;
258- static std::string certFileName;
259- static std::string passphrase;
260- static std::string dhParamsFileName;
261-
262277 /* Read the options object (SSL options) */
263278 if (args.Length () == 1 ) {
264279
@@ -308,6 +323,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
308323 ssl_options.ssl_prefer_low_memory_usage = BooleanValue (isolate, optionsObject->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate, " ssl_prefer_low_memory_usage" , NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ());
309324 }
310325
326+ /* uSockets should really copy strings it wants to keep */
311327 app = new APP (ssl_options);
312328 } else {
313329 appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.App" , NewStringType::kNormal ).ToLocalChecked ());
@@ -326,58 +342,60 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
326342 /* All the http methods */
327343 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " get" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
328344 uWS_App_get<APP >(&APP ::get, args);
329- }));
345+ }, args. Data () ));
330346
331347 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " post" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
332348 uWS_App_get<APP >(&APP ::post , args);
333- }));
349+ }, args. Data () ));
334350
335351 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " options" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
336352 uWS_App_get<APP >(&APP ::options, args);
337- }));
353+ }, args. Data () ));
338354
339355 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " del" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
340356 uWS_App_get<APP >(&APP ::del, args);
341- }));
357+ }, args. Data () ));
342358
343359 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " patch" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
344360 uWS_App_get<APP >(&APP ::patch, args);
345- }));
361+ }, args. Data () ));
346362
347363 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " put" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
348364 uWS_App_get<APP >(&APP ::put, args);
349- }));
365+ }, args. Data () ));
350366
351367 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " head" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
352368 uWS_App_get<APP >(&APP ::head, args);
353- }));
369+ }, args. Data () ));
354370
355371 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " connect" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
356372 uWS_App_get<APP >(&APP ::connect, args);
357- }));
373+ }, args. Data () ));
358374
359375 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " trace" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
360376 uWS_App_get<APP >(&APP ::trace, args);
361- }));
377+ }, args. Data () ));
362378
363379 /* Any http method */
364380 appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " any" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, [](auto &args) {
365381 uWS_App_get<APP >(&APP ::any, args);
366- }));
382+ }, args. Data () ));
367383
368384 /* ws, listen */
369- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " ws" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_ws<APP >));
370- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_listen<APP >));
371- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " publish" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_publish<APP >));
385+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " ws" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_ws<APP >, args. Data () ));
386+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_listen<APP >, args. Data () ));
387+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " publish" , NewStringType::kNormal ).ToLocalChecked (), FunctionTemplate::New (isolate, uWS_App_publish<APP >, args. Data () ));
372388
373389 Local<Object> localApp = appTemplate->GetFunction (isolate->GetCurrentContext ()).ToLocalChecked ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
374390 localApp->SetAlignedPointerInInternalField (0 , app);
375391
392+ PerContextData *perContextData = (PerContextData *) Local<External>::Cast (args.Data ())->Value ();
393+
376394 /* Add this to our delete list */
377395 if constexpr (std::is_same<APP , uWS::SSLApp>::value) {
378- sslApps.emplace_back (app);
396+ perContextData-> sslApps .emplace_back (app);
379397 } else {
380- apps.emplace_back (app);
398+ perContextData-> apps .emplace_back (app);
381399 }
382400
383401 args.GetReturnValue ().Set (localApp);
0 commit comments