Skip to content

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.

fix: address self-review findings (A6 pollution, A16 gap, test quality)

esengine5a5bdebcb931
diff --git a/sdk/src/playableRuntime.ts b/sdk/src/playableRuntime.ts
index 4c1aac39b..7484d3dde 100644
--- a/sdk/src/playableRuntime.ts
+++ b/sdk/src/playableRuntime.ts
@@ -116,6 +116,8 @@ async function instantiateWasmModule(
): Promise<unknown | null> {
try {
const wasmBytes = decodeBase64ToWasm(wasmBase64);
+ let rejectOnError: (e: unknown) => void = () => {};
+ const errorGate = new Promise<never>((_, reject) => { rejectOnError = reject; });
const opts: Record<string, unknown> = {
instantiateWasm(imports: WebAssembly.Imports, cb: Function) {
const response = new Response(wasmBytes.buffer as ArrayBuffer, {
@@ -128,14 +130,17 @@ async function instantiateWasmModule(
r => cb((r as WebAssembly.WebAssemblyInstantiatedSource).instance,
(r as WebAssembly.WebAssemblyInstantiatedSource).module),
).catch(e => {
+ // No failure channel in instantiateWasm: surface the error so
+ // the factory promise rejects instead of hanging forever.
log.error('playable', 'WASM instantiation fallback failed', e);
+ rejectOnError(e);
});
},
);
return {};
},
};
- return await factory(opts);
+ return await Promise.race([factory(opts), errorGate]);
} catch (e) {
log.error('playable', 'WASM module init failed', e);
return null;
@@ -158,7 +163,9 @@ function buildSpineManager(
const ver = version as SpineVersion;
factories.set(ver, async () => {
const wasmBytes = decodeBase64ToWasm(entry.wasmBase64);
- return entry.factory({
+ let rejectOnError: (e: unknown) => void = () => {};
+ const errorGate = new Promise<never>((_, reject) => { rejectOnError = reject; });
+ const modulePromise = entry.factory({
instantiateWasm(imports: WebAssembly.Imports, cb: Function) {
const response = new Response(wasmBytes.buffer as ArrayBuffer, {
headers: { 'Content-Type': 'application/wasm' },
@@ -171,12 +178,14 @@ function buildSpineManager(
(r as WebAssembly.WebAssemblyInstantiatedSource).module),
).catch(e => {
log.error('playable', 'Spine WASM instantiation fallback failed', e);
+ rejectOnError(e);
});
},
);
return {};
},
}) as Promise<SpineWasmModule>;
+ return Promise.race([modulePromise, errorGate]);
});
}
return new SpineManager(module, factories);
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index 91aed9941..c4432b978 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -309,7 +309,10 @@ public:
auto* pool = getPool<T>();
if (!pool) {
ES_LOG_ERROR("Registry::get: component pool does not exist (returning fallback)");
+ // Reset each miss so a write through a prior miss can't poison the
+ // shared static (see SparseSet::get). tryGet() is the checked path.
static T fallback{};
+ fallback = T{};
return fallback;
}
return pool->get(entity); // SparseSet::get is itself release-safe
diff --git a/src/esengine/ecs/SparseSet.hpp b/src/esengine/ecs/SparseSet.hpp
index 57cfa0274..b7a554e69 100644
--- a/src/esengine/ecs/SparseSet.hpp
+++ b/src/esengine/ecs/SparseSet.hpp
@@ -198,7 +198,12 @@ public:
u32 di = denseIndexOf(entity);
if (di == INVALID_INDEX || dense_[di] != entity) {
ES_LOG_ERROR("SparseSet::get on an entity without this component (returning fallback)");
+ // Reset on every miss: get<T> is sometimes used as a writable accessor,
+ // so without this a caller writing through one miss would poison the
+ // shared static for the next miss. (A missing-component get is a logic
+ // error upstream; tryGet() is the checked, non-polluting alternative.)
static T fallback{};
+ fallback = T{};
return fallback;
}
return components_[di];
diff --git a/tests/ecs/test_registry_safety.cpp b/tests/ecs/test_registry_safety.cpp
index af44c34db..e4d40fc48 100644
--- a/tests/ecs/test_registry_safety.cpp
+++ b/tests/ecs/test_registry_safety.cpp
@@ -48,6 +48,20 @@ int main() {
CHECK(v.dx == 0.0f, "get<Vel> with no Vel pool returns fallback (no OOB)");
}
+ // --- A6b: writing through a missing-component get must not poison later misses ---
+ // Review finding: the static fallback is shared, so a write through one miss used
+ // to corrupt the value returned to the next miss. Reset-on-miss must prevent that.
+ {
+ Registry r;
+ Entity owner = r.create();
+ r.emplace<Pos>(owner, Pos{5.0f, 5.0f}); // creates the Pos pool
+ Entity a = r.create();
+ Entity b = r.create();
+ r.get<Pos>(a).x = 999.0f; // write through a's fallback (a has no Pos)
+ CHECK(r.get<Pos>(b).x == 1.0f, "SparseSet::get fallback reset between misses (no pollution)");
+ CHECK(r.get<Pos>(owner).x == 5.0f, "real component unaffected by fallback writes");
+ }
+
// --- A7: re-entrant destroy(entity) from onDestroy must not double-teardown ---
{
Registry r;
@@ -61,10 +75,13 @@ int main() {
r.destroy(e);
CHECK(r.entityCount() == 0u, "entity_count is 0 after re-entrant destroy (no underflow)");
- // Old bug: idx pushed onto recycled_ twice -> two creates alias one index.
+ // Weak assertion (kept for documentation): create()'s `if (entityValid_[index])`
+ // guard masks the double-recycle — the duplicate idx is skipped on the second
+ // pop — so this passes even against the old code. The real, testable harm of
+ // double-recycle is the entity_count underflow asserted above.
Entity a = r.create();
Entity b = r.create();
- CHECK(a.index() != b.index(), "indices not aliased after re-entrant destroy (no double-recycle)");
+ CHECK(a.index() != b.index(), "indices not aliased after re-entrant destroy");
CHECK(r.valid(a) && r.valid(b), "both recreated entities are valid");
}
diff --git a/tests/text/test_bitmap_font.cpp b/tests/text/test_bitmap_font.cpp
index dd651a021..e7c5b8edf 100644
--- a/tests/text/test_bitmap_font.cpp
+++ b/tests/text/test_bitmap_font.cpp
@@ -1,15 +1,17 @@
// Native harness for BitmapFont::createLabelAtlas degenerate dimensions (Audit A5).
//
-// createLabelAtlas computed `cols = texWidth / charWidth` and later `% cols`.
-// With charWidth==0 (or texWidth<charWidth making cols==0) this is an integer
-// divide-by-zero -> WASM trap / SIGFPE. ES_ASSERT is stripped in release, so the
-// guard must be a runtime check. This harness drives degenerate dims and proves
-// they no longer crash.
+// createLabelAtlas computed `cols = texWidth / charWidth` and `glyphIndex % cols`.
+// With charWidth==0 (or texWidth<charWidth making cols==0) this is integer
+// divide-by-zero. On the WASM target i32.div_u/rem_u by zero is a spec-mandated
+// trap; on platforms where it's defined (arm64 returns 0) the OLD code instead
+// populated the atlas with garbage glyphs. The fix is observable on BOTH targets:
+// after a degenerate call the guard returns early and registers NO glyphs, while
+// the old code either traps (WASM) or registers garbage (native). So asserting an
+// empty atlas distinguishes fixed-from-unfixed WITHOUT depending on a trap — which
+// the earlier "did not crash" version did not.
//
-// Note: native ARM returns 0 on integer divide-by-zero (no trap), so this only
-// exercises the guard's logic; the real trap is on the WASM target, where
-// i32.rem_u/div_u by zero is a spec-mandated trap. Not wired into CMake because
-// the BitmapFont TU references ResourceManager; run it standalone:
+// Not wired into CMake/CTest: the BitmapFont TU references ResourceManager
+// (loadFromFntText), which would drag in the whole resource stack. Run standalone:
// clang++ -std=c++20 -I src -I third_party/glm -Wl,-undefined,dynamic_lookup \
// tests/text/test_bitmap_font.cpp src/esengine/text/BitmapFont.cpp \
// src/esengine/core/Log.cpp -o /tmp/test_bf && /tmp/test_bf
@@ -18,18 +20,40 @@
#include <cstdio>
+static int g_failures = 0;
+#define CHECK(cond, msg) \
+ do { \
+ if (!(cond)) { std::printf("FAIL: %s\n", msg); ++g_failures; } \
+ else { std::printf("ok: %s\n", msg); } \
+ } while (0)
+
int main() {
using esengine::text::BitmapFont;
esengine::resource::TextureHandle tex{}; // invalid handle is fine; only stored
- BitmapFont font;
- // charWidth==0 / charHeight==0: would divide by zero on `texWidth / charWidth`.
- font.createLabelAtlas(tex, 0, 0, "ABC", 0, 0);
- // texWidth < charWidth: cols == 0, then `% cols` divides by zero.
- font.createLabelAtlas(tex, 4, 4, "ABC", 8, 8);
- // Valid dimensions still build glyphs.
- font.createLabelAtlas(tex, 64, 16, "ABC", 16, 16);
+ // Degenerate dims must register NO glyphs (the guard returns early). Old code
+ // would trap on WASM, or register garbage glyphs on arm64 -> getGlyph != null.
+ {
+ BitmapFont font;
+ font.createLabelAtlas(tex, 0, 0, "ABC", 0, 0); // charWidth == 0
+ CHECK(font.getGlyph('A') == nullptr, "charWidth==0 registers no glyphs (guard fired)");
+ }
+ {
+ BitmapFont font;
+ font.createLabelAtlas(tex, 4, 4, "ABC", 8, 8); // texWidth < charWidth -> cols==0
+ CHECK(font.getGlyph('A') == nullptr, "texWidth<charWidth registers no glyphs (guard fired)");
+ }
+ // Valid dims still build glyphs.
+ {
+ BitmapFont font;
+ font.createLabelAtlas(tex, 64, 16, "ABC", 16, 16);
+ CHECK(font.getGlyph('A') != nullptr, "valid dims still register glyphs");
+ }
- std::printf("A5 OK: degenerate atlas dimensions did not divide by zero\n");
- return 0;
+ if (g_failures == 0) {
+ std::printf("\nALL A5 TESTS PASSED\n");
+ return 0;
+ }
+ std::printf("\n%d FAILURE(S)\n", g_failures);
+ return 1;
}