Skip to content

Commit

Permalink
shared: Mark resource objects as deleted on resource stop (Closes alt…
Browse files Browse the repository at this point in the history
…mp#148)

Former-commit-id: 7fb25b2
  • Loading branch information
LeonMrBonnie committed May 16, 2022
1 parent 80d8fc7 commit 57287b1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
8 changes: 8 additions & 0 deletions shared/V8ResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ v8::Local<v8::Object> V8ResourceImpl::GetOrCreateResourceObject(alt::IResource*
return obj;
}

void V8ResourceImpl::DeleteResourceObject(alt::IResource* resource)
{
if(resourceObjects.count(resource) == 0) return;
v8::Local<v8::Object> 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<V8Helpers::EventCallback*>& handlers, std::vector<v8::Local<v8::Value>>& args, bool waitForPromiseResolve)
{
for(auto handler : handlers)
Expand Down
1 change: 1 addition & 0 deletions shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class V8ResourceImpl : public alt::IResource::Impl
}

v8::Local<v8::Object> GetOrCreateResourceObject(alt::IResource* resource);
void DeleteResourceObject(alt::IResource* resource);

bool HasBenchmarkTimer(const std::string& name)
{
Expand Down
11 changes: 11 additions & 0 deletions shared/bindings/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,47 @@ static void IsStartedGetter(v8::Local<v8::String>, 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());
}

static void TypeGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");
V8_RETURN_STRING(resource->GetType());
}

static void NameGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");
V8_RETURN_STRING(resource->GetName());
}

static void MainGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");
V8_RETURN_STRING(resource->GetMain());
}

static void ExportsGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
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()));
}

static void DependenciesGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");

const alt::Array<std::string> deps = resource->GetDependencies();
v8::Local<v8::Array> dependencies = v8::Array::New(isolate, deps.GetSize());
Expand All @@ -57,6 +63,7 @@ static void DependantsGetter(v8::Local<v8::String>, 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<std::string> deps = resource->GetDependants();
v8::Local<v8::Array> dependants = v8::Array::New(isolate, deps.GetSize());
Expand All @@ -71,6 +78,7 @@ static void RequiredPermissionsGetter(v8::Local<v8::String>, 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<alt::Permission> perms = resource->GetRequiredPermissions();
v8::Local<v8::Array> permissions = v8::Array::New(isolate, perms.GetSize());
Expand All @@ -85,6 +93,7 @@ static void OptionalPermissionsGetter(v8::Local<v8::String>, 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<alt::Permission> perms = resource->GetOptionalPermissions();
v8::Local<v8::Array> permissions = v8::Array::New(isolate, perms.GetSize());
Expand All @@ -100,13 +109,15 @@ static void PathGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8:
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");
V8_RETURN_STRING(resource->GetPath());
}

static void ConfigGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, resource, alt::IResource);
V8_CHECK(resource, "Invalid resource");

auto config = resource->GetConfig();
v8::Local<v8::Value> val = V8Helpers::ConfigNodeToV8(config);
Expand Down
21 changes: 9 additions & 12 deletions shared/events/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const alt::CResourceStartEvent*>(e);
V8_LOCAL_EVENT_HANDLER anyResourceStart(EventType::RESOURCE_START, "anyResourceStart", [](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args) {
auto ev = static_cast<const alt::CResourceStartEvent*>(e);

return resource->GetLocalHandlers("anyResourceStart");
},
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args) {
auto ev = static_cast<const alt::CResourceStartEvent*>(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<const alt::CResourceStopEvent*>(e);

for(alt::IResource* res : alt::ICore::Instance().GetAllResources())
{
if(res->GetType() != "js") continue;
static_cast<V8ResourceImpl*>(res->GetImpl())->DeleteResourceObject(resource->GetResource());
}
return resource->GetLocalHandlers("anyResourceStop");
},
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args) {
Expand Down

0 comments on commit 57287b1

Please sign in to comment.