UniquePersistent all over, app.forcefully_free
This commit is contained in:
parent
f2a5d95726
commit
ba73b8882d
@ -37,3 +37,9 @@ const app = uWS./*SSL*/App({
|
|||||||
console.log('Failed to listen to port ' + port);
|
console.log('Failed to listen to port ' + port);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* This is questionable */
|
||||||
|
process.on('beforeExit', () => {
|
||||||
|
console.log('Forcefully freeing app now');
|
||||||
|
app.forcefully_free();
|
||||||
|
});
|
||||||
|
@ -14,12 +14,10 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We don't need to care for these just yet, since we do
|
UniquePersistent<Function> openPf;
|
||||||
* not have a way to free the app itself */
|
UniquePersistent<Function> messagePf;
|
||||||
Persistent<Function> *openPf = new Persistent<Function>();
|
UniquePersistent<Function> drainPf;
|
||||||
Persistent<Function> *messagePf = new Persistent<Function>();
|
UniquePersistent<Function> closePf;
|
||||||
Persistent<Function> *drainPf = new Persistent<Function>();
|
|
||||||
Persistent<Function> *closePf = new Persistent<Function>();
|
|
||||||
|
|
||||||
struct PerSocketData {
|
struct PerSocketData {
|
||||||
Persistent<Object> *socketPf;
|
Persistent<Object> *socketPf;
|
||||||
@ -46,16 +44,16 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Open */
|
/* Open */
|
||||||
openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
|
openPf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
|
||||||
/* Message */
|
/* Message */
|
||||||
messagePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "message"))));
|
messagePf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "message"))));
|
||||||
/* Drain */
|
/* Drain */
|
||||||
drainPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "drain"))));
|
drainPf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "drain"))));
|
||||||
/* Close */
|
/* Close */
|
||||||
closePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
|
closePf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
behavior.open = [openPf](auto *ws, auto *req) {
|
behavior.open = [openPf = std::move(openPf)](auto *ws, auto *req) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
/* Create a new websocket object */
|
/* Create a new websocket object */
|
||||||
@ -72,10 +70,10 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
perSocketData->socketPf->Reset(isolate, wsObject);
|
perSocketData->socketPf->Reset(isolate, wsObject);
|
||||||
|
|
||||||
Local<Value> argv[] = {wsObject, reqObject};
|
Local<Value> argv[] = {wsObject, reqObject};
|
||||||
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
Local<Function>::New(isolate, openPf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
||||||
};
|
};
|
||||||
|
|
||||||
behavior.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
||||||
@ -84,19 +82,19 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
|
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
|
||||||
messageArrayBuffer,
|
messageArrayBuffer,
|
||||||
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
|
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
|
||||||
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
Local<Function>::New(isolate, messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
||||||
|
|
||||||
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
|
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
|
||||||
messageArrayBuffer->Neuter();
|
messageArrayBuffer->Neuter();
|
||||||
};
|
};
|
||||||
|
|
||||||
behavior.drain = [drainPf](auto *ws) {
|
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
|
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
|
||||||
};
|
};
|
||||||
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
Local<Function>::New(isolate, drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
||||||
};
|
};
|
||||||
|
|
||||||
behavior.ping = [](auto *ws) {
|
behavior.ping = [](auto *ws) {
|
||||||
@ -107,7 +105,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
behavior.close = [closePf](auto *ws, int code, std::string_view message) {
|
behavior.close = [closePf = std::move(closePf)](auto *ws, int code, std::string_view message) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
||||||
@ -119,7 +117,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
|
|
||||||
/* Invalidate this wsObject */
|
/* Invalidate this wsObject */
|
||||||
wsObject->SetAlignedPointerInInternalField(0, nullptr);
|
wsObject->SetAlignedPointerInInternalField(0, nullptr);
|
||||||
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
Local<Function>::New(isolate, closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
||||||
|
|
||||||
delete perSocketData->socketPf;
|
delete perSocketData->socketPf;
|
||||||
|
|
||||||
@ -184,6 +182,14 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
|
|||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Mostly indended for debugging memory leaks */
|
||||||
|
template <typename APP>
|
||||||
|
void uWS_App_forcefully_free(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
||||||
|
|
||||||
|
delete app;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename APP>
|
template <typename APP>
|
||||||
void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
||||||
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
|
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
|
||||||
@ -299,6 +305,9 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
|||||||
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
|
||||||
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
|
||||||
|
|
||||||
|
/* forcefully_free is unsafe for end-users to use, but nice to track memory leaks with ASAN */
|
||||||
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "forcefully_free"), FunctionTemplate::New(isolate, uWS_App_forcefully_free<APP>));
|
||||||
|
|
||||||
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
localApp->SetAlignedPointerInInternalField(0, app);
|
localApp->SetAlignedPointerInInternalField(0, app);
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 9829f7fb37c129ac4945cfd58355959270d73d8f
|
Subproject commit e653e9d3142f8a9a9d76548e288652ab46729766
|
Loading…
Reference in New Issue
Block a user