From 703311649c915423ecaa44e1c887bd432b5f25f5 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sat, 10 Apr 2021 23:08:50 +0200 Subject: [PATCH] Wrap getTopics as special case for JS --- src/WebSocketWrapper.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index f5a699f..3f8e2ec 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -233,6 +233,26 @@ struct WebSocketWrapper { } } + /* This one is wrapped instead of iterateTopics as JS-people will put their hands in wood chipper for sure. */ + template + static void uWS_WebSocket_getTopics(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *ws = getWebSocket(args); + if (ws) { + + Local topicsArray = Array::New(isolate, 0); + + ws->iterateTopics([&topicsArray, isolate](std::string_view topic) { + Local topicString = String::NewFromUtf8(isolate, topic.data(), NewStringType::kNormal, topic.length()).ToLocalChecked(); + + topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing(); + }); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(topicsArray); + } + } + template static Local init(Isolate *isolate) { Local wsTemplateLocal = FunctionTemplate::New(isolate); @@ -257,6 +277,9 @@ struct WebSocketWrapper { wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed)); + /* This one does not exist in C++ */ + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getTopics", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getTopics)); + /* Create the template */ Local wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();