Skip to content

Commit

Permalink
shared: Add alt.off function reference validation (#234)
Browse files Browse the repository at this point in the history
* shared: Add alt.off function reference validation

* Revert formatOnSave
  • Loading branch information
xxshady authored Dec 27, 2022
1 parent fc30394 commit b385b7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "V8Timer.h"

#include "IRuntimeEventHandler.h"
#include "V8Helpers.h"

class V8ResourceImpl : public alt::IResource::Impl
{
Expand Down Expand Up @@ -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<v8::Function> cb)
void UnsubscribeLocal(const std::string& ev, v8::Local<v8::Function> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion shared/bindings/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void Off(const v8::FunctionCallbackInfo<v8::Value>& 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));
}
}

Expand Down

0 comments on commit b385b7e

Please sign in to comment.