A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(engine): unify ctx() onto a single safe activeCtx() (instancing N2)
esengine4522646ed836
diff --git a/src/esengine/bindings/ActiveContext.hpp b/src/esengine/bindings/ActiveContext.hppindex 8d2594001..2e24c36eb 100644--- a/src/esengine/bindings/ActiveContext.hpp+++ b/src/esengine/bindings/ActiveContext.hpp@@ -2,21 +2,35 @@#include "../core/EstellaContext.hpp"+#include "EngineContext.hpp" // unset-fallback default context (see activeCtx)namespace esengine {/**- * @brief Active engine context for the current thread/call- * @details All binding functions access the engine through this pointer.- * Set by initRenderer / setActiveContext before any rendering calls.+ * @brief The explicitly-installed active engine context (or null if none).+ * @details Set by setActiveContext (REARCH_ENGINE_INSTANCING) or, until N3, by+ * the legacy initRenderer path. Read through {@link activeCtx}.*/inline EstellaContext* g_activeContext = nullptr;/**- * @brief Get the active context (asserts non-null in debug)+ * @brief The engine context every binding routes through — single source of truth.+ *+ * @details Returns the explicitly-installed context when present; otherwise falls+ * back to the process default context (`EngineContext::instance`), whose+ * constructor registers the GPU-independent logic systems, so headless+ * tooling/tests that never call initRenderer can still run UI layout /+ * hit-test instead of dereferencing nullptr. The default is just the+ * unset fallback, **not** a privileged singleton — real Apps install+ * their own context via setActiveContext.+ *+ * Putting the fallback here (rather than in each binding file's local+ * `ctx()`) makes headless-vs-rendered behavior **uniform**: before, only+ * WebSDKEntry's `ctx()` had the fallback and the other binding files+ * dereferenced `g_activeContext` bare → null deref when headless.*/inline EstellaContext& activeCtx() {- return *g_activeContext;+ return g_activeContext ? *g_activeContext : EngineContext::instance().context();}} // namespace esenginediff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 8ed2d2553..26bb4118a 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -66,15 +66,10 @@ static_assert(sizeof(void*) == 4, "EM_JS pointer passing assumes wasm32 (4-bytenamespace esengine {static EngineContext& legacyCtx() { return EngineContext::instance(); }-static EstellaContext& ctx() {- // The active context is installed by initRenderer (g_activeContext). Headless- // callers — tooling/tests that drive UI layout/hit-test without a GL context- // — never set it; fall back to the process singleton (whose constructor- // registers the GPU-independent logic systems) rather than dereferencing- // nullptr through activeCtx(). GPU services stay absent there, so the- // renderer bindings' tryGet<>() returns null exactly as before.- return g_activeContext ? activeCtx() : legacyCtx().context();-}+// Single source of truth for the active context lives in activeCtx()+// (ActiveContext.hpp), which now carries the headless fallback uniformly — so+// this is just the file-local short alias, identical to every other binding's.+static EstellaContext& ctx() { return activeCtx(); }#define g_initialized (ctx().state().initialized)#define g_resourceManager (ctx().tryGet<resource::ResourceManager>())