From b385b7e077d7a0652a5cc33edfb69fec4211b414 Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Tue, 27 Dec 2022 16:16:17 +0300 Subject: [PATCH] shared: Add alt.off function reference validation (#234) * shared: Add alt.off function reference validation * Revert formatOnSave --- shared/V8ResourceImpl.h | 19 +++++++++++++++++-- shared/bindings/Main.cpp | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/shared/V8ResourceImpl.h b/shared/V8ResourceImpl.h index 44221470..326dcd88 100644 --- a/shared/V8ResourceImpl.h +++ b/shared/V8ResourceImpl.h @@ -11,6 +11,7 @@ #include "V8Timer.h" #include "IRuntimeEventHandler.h" +#include "V8Helpers.h" class V8ResourceImpl : public alt::IResource::Impl { @@ -69,14 +70,28 @@ class V8ResourceImpl : public alt::IResource::Impl remoteGenericHandlers.push_back(V8Helpers::EventCallback{ isolate, cb, std::move(location), once }); } - void UnsubscribeLocal(const std::string& ev, v8::Local cb) + void UnsubscribeLocal(const std::string& ev, v8::Local cb, V8Helpers::SourceLocation&& location) { auto range = localHandlers.equal_range(ev); + bool anyHandlerRemoved = false; for(auto it = range.first; it != range.second; ++it) { - if(it->second.fn.Get(isolate)->StrictEquals(cb)) it->second.removed = true; + if(it->second.fn.Get(isolate)->StrictEquals(cb)) + { + it->second.removed = true; + anyHandlerRemoved = true; + } } + + if(!anyHandlerRemoved) + { + Log::Warning << + location.ToString() << " alt.off was called for event \"" << ev << + "\" with function reference that was not subscribed" << Log::Endl; + return; + } + alt::CEvent::Type type = V8Helpers::EventHandler::GetTypeForEventName(ev); if(type != alt::CEvent::Type::NONE) IRuntimeEventHandler::Instance().EventHandlerRemoved(type); } diff --git a/shared/bindings/Main.cpp b/shared/bindings/Main.cpp index acda0bdf..1286e28e 100644 --- a/shared/bindings/Main.cpp +++ b/shared/bindings/Main.cpp @@ -70,7 +70,7 @@ static void Off(const v8::FunctionCallbackInfo& info) V8_ARG_TO_STRING(1, evName); V8_ARG_TO_FUNCTION(2, callback); - resource->UnsubscribeLocal(evName, callback); + resource->UnsubscribeLocal(evName, callback, V8Helpers::SourceLocation::GetCurrent(isolate, resource)); } }