From 57287b1ce1e035907532521cd30a227550d9b83c Mon Sep 17 00:00:00 2001 From: LeonMrBonnie Date: Mon, 16 May 2022 14:47:28 +0200 Subject: [PATCH] shared: Mark resource objects as deleted on resource stop (Closes #148) Former-commit-id: 7fb25b22df39a7cb427cd4873bcc1f527c8a34ed --- shared/V8ResourceImpl.cpp | 8 ++++++++ shared/V8ResourceImpl.h | 1 + shared/bindings/Resource.cpp | 11 +++++++++++ shared/events/Resource.cpp | 21 +++++++++------------ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/shared/V8ResourceImpl.cpp b/shared/V8ResourceImpl.cpp index cb0451f5..d8ec373c 100644 --- a/shared/V8ResourceImpl.cpp +++ b/shared/V8ResourceImpl.cpp @@ -319,6 +319,14 @@ v8::Local V8ResourceImpl::GetOrCreateResourceObject(alt::IResource* return obj; } +void V8ResourceImpl::DeleteResourceObject(alt::IResource* resource) +{ + if(resourceObjects.count(resource) == 0) return; + v8::Local obj = resourceObjects.at(resource).Get(isolate); + obj->SetInternalField(0, v8::External::New(isolate, nullptr)); + resourceObjects.erase(resource); +} + void V8ResourceImpl::InvokeEventHandlers(const alt::CEvent* ev, const std::vector& handlers, std::vector>& args, bool waitForPromiseResolve) { for(auto handler : handlers) diff --git a/shared/V8ResourceImpl.h b/shared/V8ResourceImpl.h index 02c2fe23..52692603 100644 --- a/shared/V8ResourceImpl.h +++ b/shared/V8ResourceImpl.h @@ -230,6 +230,7 @@ class V8ResourceImpl : public alt::IResource::Impl } v8::Local GetOrCreateResourceObject(alt::IResource* resource); + void DeleteResourceObject(alt::IResource* resource); bool HasBenchmarkTimer(const std::string& name) { diff --git a/shared/bindings/Resource.cpp b/shared/bindings/Resource.cpp index 470b3f02..5aaa4941 100644 --- a/shared/bindings/Resource.cpp +++ b/shared/bindings/Resource.cpp @@ -8,6 +8,7 @@ static void IsStartedGetter(v8::Local, const v8::PropertyCallbackInf { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); V8_RETURN(resource->IsStarted()); } @@ -15,6 +16,7 @@ static void TypeGetter(v8::Local, const v8::PropertyCallbackInfoGetType()); } @@ -22,6 +24,7 @@ static void NameGetter(v8::Local, const v8::PropertyCallbackInfoGetName()); } @@ -29,6 +32,7 @@ static void MainGetter(v8::Local, const v8::PropertyCallbackInfoGetMain()); } @@ -36,6 +40,7 @@ static void ExportsGetter(v8::Local, const v8::PropertyCallbackInfo< { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); V8_RETURN(V8Helpers::MValueToV8(resource->GetExports())); } @@ -43,6 +48,7 @@ static void DependenciesGetter(v8::Local, const v8::PropertyCallback { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); const alt::Array deps = resource->GetDependencies(); v8::Local dependencies = v8::Array::New(isolate, deps.GetSize()); @@ -57,6 +63,7 @@ static void DependantsGetter(v8::Local, const v8::PropertyCallbackIn { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); const alt::Array deps = resource->GetDependants(); v8::Local dependants = v8::Array::New(isolate, deps.GetSize()); @@ -71,6 +78,7 @@ static void RequiredPermissionsGetter(v8::Local, const v8::PropertyC { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); const alt::Array perms = resource->GetRequiredPermissions(); v8::Local permissions = v8::Array::New(isolate, perms.GetSize()); @@ -85,6 +93,7 @@ static void OptionalPermissionsGetter(v8::Local, const v8::PropertyC { V8_GET_ISOLATE_CONTEXT(); V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource); + V8_CHECK(resource, "Invalid resource"); const alt::Array perms = resource->GetOptionalPermissions(); v8::Local permissions = v8::Array::New(isolate, perms.GetSize()); @@ -100,6 +109,7 @@ static void PathGetter(v8::Local, const v8::PropertyCallbackInfoGetPath()); } @@ -107,6 +117,7 @@ static void ConfigGetter(v8::Local, const v8::PropertyCallbackInfoGetConfig(); v8::Local val = V8Helpers::ConfigNodeToV8(config); diff --git a/shared/events/Resource.cpp b/shared/events/Resource.cpp index fc901e79..a10a32df 100644 --- a/shared/events/Resource.cpp +++ b/shared/events/Resource.cpp @@ -8,24 +8,21 @@ using EventType = alt::CEvent::Type; -V8_EVENT_HANDLER anyResourceStart( - EventType::RESOURCE_START, - [](V8ResourceImpl* resource, const alt::CEvent* e) { - auto ev = static_cast(e); +V8_LOCAL_EVENT_HANDLER anyResourceStart(EventType::RESOURCE_START, "anyResourceStart", [](V8ResourceImpl* resource, const alt::CEvent* e, std::vector>& args) { + auto ev = static_cast(e); - return resource->GetLocalHandlers("anyResourceStart"); - }, - [](V8ResourceImpl* resource, const alt::CEvent* e, std::vector>& args) { - auto ev = static_cast(e); - - args.push_back(V8Helpers::JSValue(ev->GetResource()->GetName())); - }); + args.push_back(V8Helpers::JSValue(ev->GetResource()->GetName())); +}); V8_EVENT_HANDLER anyResourceStop( EventType::RESOURCE_STOP, [](V8ResourceImpl* resource, const alt::CEvent* e) { auto ev = static_cast(e); - + for(alt::IResource* res : alt::ICore::Instance().GetAllResources()) + { + if(res->GetType() != "js") continue; + static_cast(res->GetImpl())->DeleteResourceObject(resource->GetResource()); + } return resource->GetLocalHandlers("anyResourceStop"); }, [](V8ResourceImpl* resource, const alt::CEvent* e, std::vector>& args) {