Get a promise when starting a GUIpage

When calling `Engine.PushGuiPage` a promise is returned. The promise is
settled when the "child" page is closed. That allows to `await` it
inside `async` functions.
Previously the callback was run right inside the call to
`Engine.PopGuiPage`. Now the continuation of the promise is called at
the end of the "tick".

This won't help performance. It will more likely make things worse.
Since gui pages aren't opened or closed that frequently, it doesn't
matter that much.

Refs: 86c151ebaa

For the engine side:
The promise is stored in the `CGUIManager::SGUIPage` (like previously
the callback). When the promise is fulfilled it enqueues a callback in
the `JobQueue` of the `JSContext`.

Original patch by: @wraitii
Comments by: @wraitii, @Stan, @Polakrity, @lyv, @elexis, @vladislavbelov
Differential Revision: https://code.wildfiregames.com/D3807
This was SVN commit r28145.
This commit is contained in:
phosit
2024-07-08 19:07:04 +00:00
parent ae67a77bd9
commit f9114a87f2
22 changed files with 212 additions and 210 deletions
+17 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2023 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -27,6 +27,11 @@
constexpr int DEFAULT_CONTEXT_SIZE = 16 * 1024 * 1024;
constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER = 2 * 1024 * 1024;
namespace Script
{
class JobQueue;
}
/**
* Abstraction around a SpiderMonkey JSContext.
*
@@ -74,6 +79,14 @@ public:
void RegisterRealm(JS::Realm* realm);
void UnRegisterRealm(JS::Realm* realm);
/**
* Runs the promise continuation.
* On contexts where promises can be used this function has to be
* called.
* This function has to be called frequently.
*/
void RunJobs();
/**
* GetGeneralJSContext returns the context without starting a GC request and without
* entering any compartment. It should only be used in specific situations, such as
@@ -86,14 +99,15 @@ public:
private:
JSContext* m_cx;
const std::unique_ptr<Script::JobQueue> m_JobQueue;
void PrepareZonesForIncrementalGC() const;
std::list<JS::Realm*> m_Realms;
int m_ContextSize;
int m_HeapGrowthBytesGCTrigger;
int m_LastGCBytes;
double m_LastGCCheck;
int m_LastGCBytes{0};
double m_LastGCCheck{0.0};
};
// Using a global object for the context is a workaround until Simulation, AI, etc,