Skip to content

Commit ddbe131

Browse files
committed
Support for V8 14
1 parent 2b6efef commit ddbe131

6 files changed

Lines changed: 91 additions & 50 deletions

File tree

‎src/AppWrapper.h‎

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
3333

3434
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
3535

36-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
36+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
3737
/* This one is default constructed with defaults */
3838
typename APP::template WebSocketBehavior<PerSocketData> behavior = {};
3939

@@ -128,16 +128,19 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
128128

129129
Local<Function> upgradeLf = Local<Function>::New(isolate, upgradePf);
130130
Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
131-
resObject->SetAlignedPointerInInternalField(0, res);
131+
//resObject->SetAlignedPointerInInternalField(0, res);
132+
setInternalPointer(resObject, res);
132133

133134
Local<Object> reqObject = perContextData->reqTemplate[std::is_same<APP, uWS::H3App>::value].Get(isolate)->Clone();
134-
reqObject->SetAlignedPointerInInternalField(0, req);
135+
//reqObject->SetAlignedPointerInInternalField(0, req);
136+
setInternalPointer(reqObject, req);
135137

136138
Local<Value> argv[3] = {resObject, reqObject, External::New(isolate, (void *) context)};
137139
CallJS(isolate, upgradeLf, 3, argv);
138140

139141
/* Properly invalidate req */
140-
reqObject->SetAlignedPointerInInternalField(0, nullptr);
142+
//reqObject->SetAlignedPointerInInternalField(0, nullptr);
143+
setInternalPointer(reqObject, nullptr);
141144

142145
/* µWS itself will terminate if not responded and not attached
143146
* onAborted handler, so we can assume it's done */
@@ -151,7 +154,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
151154

152155
/* Create a new websocket object */
153156
Local<Object> wsObject = perContextData->wsTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
154-
wsObject->SetAlignedPointerInInternalField(0, ws);
157+
//wsObject->SetAlignedPointerInInternalField(0, ws);
158+
setInternalPointer(wsObject, ws);
155159

156160
/* Retrieve temporary userData object */
157161
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
@@ -286,7 +290,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
286290
Local<Object> wsObject = Local<Object>::New(isolate, perSocketData->socketPf);
287291

288292
/* Invalidate this wsObject */
289-
wsObject->SetAlignedPointerInInternalField(0, nullptr);
293+
//wsObject->SetAlignedPointerInInternalField(0, nullptr);
294+
setInternalPointer(wsObject, nullptr);
290295

291296
/* Only call close handler if we have one set */
292297
Local<Function> closeLf = Local<Function>::New(isolate, closePf);
@@ -311,7 +316,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
311316
/* This method wraps get, post and all http methods */
312317
template <typename APP, typename F>
313318
void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
314-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
319+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
315320

316321
/* Pattern */
317322
NativeString pattern(args.GetIsolate(), args[0]);
@@ -464,16 +469,19 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
464469
HandleScope hs(isolate);
465470

466471
Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
467-
resObject->SetAlignedPointerInInternalField(0, res);
472+
//resObject->SetAlignedPointerInInternalField(0, res);
473+
setInternalPointer(resObject, res);
468474

469475
Local<Object> reqObject = perContextData->reqTemplate[std::is_same<APP, uWS::H3App>::value].Get(isolate)->Clone();
470-
reqObject->SetAlignedPointerInInternalField(0, req);
476+
//reqObject->SetAlignedPointerInInternalField(0, req);
477+
setInternalPointer(reqObject, req);
471478

472479
Local<Value> argv[] = {resObject, reqObject};
473480
CallJS(isolate, cb.Get(isolate), 2, argv);
474481

475482
/* Properly invalidate req */
476-
reqObject->SetAlignedPointerInInternalField(0, nullptr);
483+
//reqObject->SetAlignedPointerInInternalField(0, nullptr);
484+
setInternalPointer(reqObject, nullptr);
477485

478486
/* µWS itself will terminate if not responded and not attached
479487
* onAborted handler, so we can assume it's done */
@@ -484,15 +492,15 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
484492

485493
template <typename APP>
486494
void uWS_App_close(const FunctionCallbackInfo<Value> &args) {
487-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
495+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
488496

489497
app->close();
490498
args.GetReturnValue().Set(args.This());
491499
}
492500

493501
template <typename APP>
494502
void uWS_App_listen_unix(const FunctionCallbackInfo<Value> &args) {
495-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
503+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
496504

497505
Isolate *isolate = args.GetIsolate();
498506

@@ -526,7 +534,7 @@ void uWS_App_listen_unix(const FunctionCallbackInfo<Value> &args) {
526534

527535
template <typename APP>
528536
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
529-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
537+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
530538

531539
Isolate *isolate = args.GetIsolate();
532540

@@ -568,7 +576,7 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
568576

569577
template <typename APP>
570578
void uWS_App_filter(const FunctionCallbackInfo<Value> &args) {
571-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
579+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
572580

573581
/* Handler */
574582
Callback checkedCallback(args.GetIsolate(), args[0]);
@@ -585,7 +593,8 @@ void uWS_App_filter(const FunctionCallbackInfo<Value> &args) {
585593
HandleScope hs(isolate);
586594

587595
Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
588-
resObject->SetAlignedPointerInInternalField(0, res);
596+
//resObject->SetAlignedPointerInInternalField(0, res);
597+
setInternalPointer(resObject, res);
589598

590599
Local<Value> argv[] = {resObject, Local<Value>::Cast(Integer::New(isolate, count))};
591600
CallJS(isolate, cb.Get(isolate), 2, argv);
@@ -596,7 +605,7 @@ void uWS_App_filter(const FunctionCallbackInfo<Value> &args) {
596605

597606
template <typename APP>
598607
void uWS_App_domain(const FunctionCallbackInfo<Value> &args) {
599-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
608+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
600609

601610
Isolate *isolate = args.GetIsolate();
602611

@@ -617,7 +626,7 @@ void uWS_App_domain(const FunctionCallbackInfo<Value> &args) {
617626

618627
template <typename APP>
619628
void uWS_App_publish(const FunctionCallbackInfo<Value> &args) {
620-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
629+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
621630

622631
Isolate *isolate = args.GetIsolate();
623632

@@ -643,7 +652,7 @@ void uWS_App_publish(const FunctionCallbackInfo<Value> &args) {
643652

644653
template <typename APP>
645654
void uWS_App_numSubscribers(const FunctionCallbackInfo<Value> &args) {
646-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
655+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
647656

648657
Isolate *isolate = args.GetIsolate();
649658

@@ -744,7 +753,7 @@ std::pair<uWS::SocketContextOptions, bool> readOptionsObject(const FunctionCallb
744753

745754
template <typename APP>
746755
void uWS_App_adoptSocket(const FunctionCallbackInfo<Value> &args) {
747-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
756+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
748757

749758
Isolate *isolate = args.GetIsolate();
750759

@@ -762,7 +771,7 @@ void uWS_App_adoptSocket(const FunctionCallbackInfo<Value> &args) {
762771

763772
template <typename APP>
764773
void uWS_App_removeChildApp(const FunctionCallbackInfo<Value> &args) {
765-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
774+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
766775

767776
Isolate *isolate = args.GetIsolate();
768777

@@ -778,7 +787,7 @@ void uWS_App_removeChildApp(const FunctionCallbackInfo<Value> &args) {
778787

779788
template <typename APP>
780789
void uWS_App_addChildApp(const FunctionCallbackInfo<Value> &args) {
781-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
790+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
782791

783792
Isolate *isolate = args.GetIsolate();
784793

@@ -801,7 +810,7 @@ void uWS_App_addChildApp(const FunctionCallbackInfo<Value> &args) {
801810

802811
template <typename APP>
803812
void uWS_App_getDescriptor(const FunctionCallbackInfo<Value> &args) {
804-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
813+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
805814

806815
Isolate *isolate = args.GetIsolate();
807816

@@ -826,7 +835,7 @@ void uWS_App_getDescriptor(const FunctionCallbackInfo<Value> &args) {
826835

827836
template <typename APP>
828837
void uWS_App_addServerName(const FunctionCallbackInfo<Value> &args) {
829-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
838+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
830839

831840
Isolate *isolate = args.GetIsolate();
832841
NativeString hostnamePatternValue(isolate, args[0]);
@@ -850,7 +859,7 @@ void uWS_App_addServerName(const FunctionCallbackInfo<Value> &args) {
850859

851860
template <typename APP>
852861
void uWS_App_removeServerName(const FunctionCallbackInfo<Value> &args) {
853-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
862+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
854863

855864
Isolate *isolate = args.GetIsolate();
856865
NativeString hostnamePatternValue(isolate, args[0]);
@@ -869,7 +878,7 @@ void uWS_App_removeServerName(const FunctionCallbackInfo<Value> &args) {
869878

870879
template <typename APP>
871880
void uWS_App_missingServerName(const FunctionCallbackInfo<Value> &args) {
872-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
881+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
873882
Isolate *isolate = args.GetIsolate();
874883

875884
UniquePersistent<Function> missingPf;
@@ -933,7 +942,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
933942
std::cout << "Registering cached get handler" << std::endl;
934943

935944

936-
APP *app = (APP *) args.This()->GetAlignedPointerFromInternalField(0);
945+
APP *app = (APP *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
937946

938947
/* Pattern */
939948
NativeString pattern(args.GetIsolate(), args[0]);
@@ -958,16 +967,19 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
958967

959968
// this needs to be cachedresponse wrapper (for both cached tcp and cached SSL?)
960969
Local<Object> resObject = perContextData->resTemplate[/*getAppTypeIndex<APP>()*/3].Get(isolate)->Clone();
961-
resObject->SetAlignedPointerInInternalField(0, res);
970+
//resObject->SetAlignedPointerInInternalField(0, res);
971+
setInternalPointer(resObject, res);
962972

963973
Local<Object> reqObject = perContextData->reqTemplate[std::is_same<APP, uWS::H3App>::value].Get(isolate)->Clone();
964-
reqObject->SetAlignedPointerInInternalField(0, req);
974+
//reqObject->SetAlignedPointerInInternalField(0, req);
975+
setInternalPointer(reqObject, req);
965976

966977
Local<Value> argv[] = {resObject, reqObject};
967978
CallJS(isolate, cb.Get(isolate), 2, argv);
968979

969980
/* Properly invalidate req */
970-
reqObject->SetAlignedPointerInInternalField(0, nullptr);
981+
//reqObject->SetAlignedPointerInInternalField(0, nullptr);
982+
setInternalPointer(reqObject, nullptr);
971983

972984
/* µWS itself will terminate if not responded and not attached
973985
* onAborted handler, so we can assume it's done */
@@ -1052,7 +1064,8 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
10521064
}
10531065

10541066
Local<Object> localApp = appTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
1055-
localApp->SetAlignedPointerInInternalField(0, app);
1067+
//localApp->SetAlignedPointerInInternalField(0, app);
1068+
setInternalPointer(localApp, app);
10561069

10571070
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
10581071

‎src/HttpRequestWrapper.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct HttpRequestWrapper {
2929
static inline constexpr decltype(auto) getHttpRequest(const FunctionCallbackInfo<Value> &args) {
3030
Isolate *isolate = args.GetIsolate();
3131
/* Thow on deleted request */
32-
auto *req = (uWS::HttpRequest *) args.This()->GetAlignedPointerFromInternalField(0);
32+
auto *req = (uWS::HttpRequest *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
3333
if (!req) {
3434
args.GetReturnValue().Set(isolate->ThrowException(v8::Exception::Error(String::NewFromUtf8(isolate, "uWS.HttpRequest must not be accessed after await or route handler return. See documentation for uWS.HttpRequest and consult the user manual.", NewStringType::kNormal).ToLocalChecked())));
3535
}

‎src/HttpResponseWrapper.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct HttpResponseWrapper {
3636
template <int PROTOCOL>
3737
static inline constexpr decltype(auto) getHttpResponse(const FunctionCallbackInfo<Value> &args) {
3838
Isolate *isolate = args.GetIsolate();
39-
void *res = args.This()->GetAlignedPointerFromInternalField(0);
39+
void *res = getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
4040
if (!res) {
4141
args.GetReturnValue().Set(isolate->ThrowException(v8::Exception::Error(String::NewFromUtf8(isolate, "uWS.HttpResponse must not be accessed after uWS.HttpResponse.onAborted callback, or after a successful response. See documentation for uWS.HttpResponse and consult the user manual.", NewStringType::kNormal).ToLocalChecked())));
4242
}
@@ -53,7 +53,8 @@ struct HttpResponseWrapper {
5353

5454
/* Marks this JS object invalid */
5555
static inline void invalidateResObject(const FunctionCallbackInfo<Value> &args) {
56-
args.This()->SetAlignedPointerInInternalField(0, nullptr);
56+
//args.This()->SetAlignedPointerInInternalField(0, nullptr);
57+
setInternalPointer(args.This(), nullptr);
5758
}
5859

5960
/* Takes nothing, returns this */
@@ -231,7 +232,7 @@ struct HttpResponseWrapper {
231232
HandleScope hs(isolate);
232233

233234
/* Mark this resObject invalid */
234-
Local<Object>::New(isolate, resObject)->SetAlignedPointerInInternalField(0, nullptr);
235+
setInternalPointer(Local<Object>::New(isolate, resObject), nullptr);//->SetAlignedPointerInInternalField(0, nullptr);
235236

236237
CallJS(isolate, Local<Function>::New(isolate, p), 0, nullptr);
237238
});

‎src/Utilities.h‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323
#include <v8.h>
2424
using namespace v8;
2525

26+
/* Getting internal pointer is different in recent V8 versions */
27+
#if (V8_MAJOR_VERSION == 14)
28+
void *getInternalPointer(const Local<Object> &holder) {
29+
return holder->GetAlignedPointerFromInternalField(0, 0);
30+
}
31+
32+
void setInternalPointer(const Local<Object> &holder, void *value) {
33+
holder->SetAlignedPointerInInternalField(0, value, 0);
34+
}
35+
#else
36+
void *getInternalPointer(const Local<Object> &holder) {
37+
return holder->GetAlignedPointerFromInternalField(0);
38+
}
39+
40+
void setInternalPointer(const Local<Object> &holder, void *value) {
41+
holder->SetAlignedPointerInInternalField(0, value);
42+
}
43+
#endif
44+
2645
/* Unfortunately we _have_ to depend on Node.js crap */
2746
#include <node.h>
2847

@@ -168,11 +187,20 @@ class NativeString {
168187

169188
/* StringView path is Latin-1, not Utf-8 */
170189

171-
// Fallback
172-
length = string->Utf8Length(isolate);
173-
data = alloc(length);
174-
allocated = true;
175-
string->WriteUtf8(isolate, data, length, nullptr, String::WriteOptions::NO_NULL_TERMINATION);
190+
#if (V8_MAJOR_VERSION == 14)
191+
// Fallback
192+
length = string->Utf8LengthV2(isolate);
193+
data = alloc(length);
194+
allocated = true;
195+
string->WriteUtf8V2(isolate, data, length);
196+
#else
197+
// Fallback
198+
length = string->Utf8Length(isolate);
199+
data = alloc(length);
200+
allocated = true;
201+
string->WriteUtf8(isolate, data, length, nullptr, String::WriteOptions::NO_NULL_TERMINATION);
202+
#endif
203+
176204

177205
} else if (value->IsArrayBufferView()) { /* DataView or TypedArray */
178206
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);

‎src/WebSocketWrapper.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ struct WebSocketWrapper {
2929
template <bool SSL>
3030
static inline uWS::WebSocket<SSL, true, PerSocketData> *getWebSocket(const FunctionCallbackInfo<Value> &args) {
3131
Isolate *isolate = args.GetIsolate();
32-
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) args.This()->GetAlignedPointerFromInternalField(0);
32+
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
3333
if (!ws) {
3434
args.GetReturnValue().Set(isolate->ThrowException(v8::Exception::Error(String::NewFromUtf8(isolate, "Invalid access of closed uWS.WebSocket/SSLWebSocket.", NewStringType::kNormal).ToLocalChecked())));
3535
}
3636
return ws;
3737
}
3838

3939
static inline void invalidateWsObject(const FunctionCallbackInfo<Value> &args) {
40-
args.This()->SetAlignedPointerInInternalField(0, nullptr);
40+
//args.This()->SetAlignedPointerInInternalField(0, nullptr);
41+
setInternalPointer(args.This(), nullptr);
4142
}
4243

4344
/* Takes nothing returns holder (only used to fool TypeScript, as a conversion from WS to UserData) */
@@ -335,7 +336,7 @@ template <bool SSL>
335336
static uint32_t uWS_WebSocket_send_fast_string(v8::Local<v8::Object> receiver,
336337
const v8::FastOneByteString& message,
337338
bool isBinary, bool compress) {
338-
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
339+
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0);
339340
if (!ws) return 0;
340341
return ws->send(std::string_view(message.data, message.length),
341342
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
@@ -346,7 +347,7 @@ template <bool SSL>
346347
static uint32_t uWS_WebSocket_send_fast_buffer(v8::Local<v8::Object> receiver,
347348
v8::Local<v8::Value> message,
348349
bool isBinary, bool compress) {
349-
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
350+
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0);
350351
if (!ws) return 0;
351352

352353
char* data = nullptr;

0 commit comments

Comments
 (0)