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.

feat(aot): a host knows what engine it is, from the one author of that answer

yhh4885137e7d0e
diff --git a/compiler/tests/generated.test.ts b/compiler/tests/generated.test.ts
index 981c398f2..0d044e591 100644
--- a/compiler/tests/generated.test.ts
+++ b/compiler/tests/generated.test.ts
@@ -24,6 +24,7 @@ import { inlineSystem } from '../src/inline';
import { builtinShapes } from '../src/builtins';
import { packLayout } from '../src/abi';
import { emitC } from '../src/codegen';
+import { engineAbiDigest } from '../../sdk/src/ecs/aot/abiDigest';
const ROOT = resolve(fileURLToPath(new URL('../..', import.meta.url)));
const WRITE = process.env['ESTELLA_AOT_WRITE'] === '1';
@@ -59,7 +60,9 @@ describe('the C artifacts the engine builds against', () => {
});
const layout = packLayout(result.module.comps);
- const c = emitC(result.module, layout, [inlineSystem(move!, result.module.fns)]);
+ // Width 8: the only consumer is a native harness, and a fixture that bakes
+ // the other width is not the artifact a native build makes.
+ const c = emitC(result.module, layout, [inlineSystem(move!, result.module.fns)], 8);
it('the contract header, where the engine can include it', () => {
artifact(join(ROOT, 'src/esengine/aot/estella_abi.h'), c.header);
@@ -76,6 +79,29 @@ describe('the C artifacts the engine builds against', () => {
artifact(join(ROOT, 'tests/aot/generated/move_system_decl.c'), c.decls);
});
+ /**
+ * The digest a HOST compares a module against, from the same function the
+ * module's own comes from — so a native host carries no second opinion
+ * about what this engine is.
+ */
+ it('the engine digest, where a native host can compare against it', () => {
+ artifact(join(ROOT, 'src/esengine/aot/EngineDigest.generated.h'), [
+ '/* GENERATED by @estella/compiler — do not edit. */',
+ '/* What this engine is, for a host opening a compiled module. */',
+ '#pragma once',
+ '',
+ '#include "esengine/aot/estella_abi.h"',
+ '',
+ '/* One per address width, because the width is IN the digest: a host that',
+ ' took the other one would refuse every module built for it. */',
+ `#define ES_ENGINE_ABI_DIGEST_32 0x${engineAbiDigest(4)}ULL`,
+ `#define ES_ENGINE_ABI_DIGEST_64 0x${engineAbiDigest(8)}ULL`,
+ '#define ES_ENGINE_ABI_DIGEST \\',
+ ' (sizeof(es_addr_t) == 8 ? ES_ENGINE_ABI_DIGEST_64 : ES_ENGINE_ABI_DIGEST_32)',
+ '',
+ ].join('\n'));
+ });
+
it('and the hash the harness compares, so it is not a second copy', () => {
artifact(join(ROOT, 'tests/aot/generated/move_system_hash.h'), [
'/* GENERATED by @estella/compiler — do not edit. */',
diff --git a/src/esengine/aot/EngineDigest.generated.h b/src/esengine/aot/EngineDigest.generated.h
new file mode 100644
index 000000000..7cd1ec5fa
--- /dev/null
+++ b/src/esengine/aot/EngineDigest.generated.h
@@ -0,0 +1,12 @@
+/* GENERATED by @estella/compiler — do not edit. */
+/* What this engine is, for a host opening a compiled module. */
+#pragma once
+
+#include "esengine/aot/estella_abi.h"
+
+/* One per address width, because the width is IN the digest: a host that
+ took the other one would refuse every module built for it. */
+#define ES_ENGINE_ABI_DIGEST_32 0xe83b13ad2f96720cULL
+#define ES_ENGINE_ABI_DIGEST_64 0xf1506f426a7da360ULL
+#define ES_ENGINE_ABI_DIGEST \
+ (sizeof(es_addr_t) == 8 ? ES_ENGINE_ABI_DIGEST_64 : ES_ENGINE_ABI_DIGEST_32)
diff --git a/tests/aot/generated/move_system_decl.c b/tests/aot/generated/move_system_decl.c
index 66e27bbc5..0cebcff34 100644
--- a/tests/aot/generated/move_system_decl.c
+++ b/tests/aot/generated/move_system_decl.c
@@ -10,7 +10,7 @@
answer, it is a read of a different field. The address width belongs
to the MACHINE, so it mixes in here rather than in the compiler and a
32-bit artifact cannot load into a 64-bit host. */
-#define ES_ABI_ENGINE_DIGEST 0xe83b13ad2f96720cULL
+#define ES_ABI_ENGINE_DIGEST 0xf1506f426a7da360ULL
ES_EXPORT const uint64_t es_abi_hash =
ES_ABI_ENGINE_DIGEST ^ (0x9e3779b97f4a7c15ULL * (uint64_t)sizeof(es_addr_t));
diff --git a/tests/aot/generated/move_system_hash.h b/tests/aot/generated/move_system_hash.h
index 51cead3ff..61d486d4c 100644
--- a/tests/aot/generated/move_system_hash.h
+++ b/tests/aot/generated/move_system_hash.h
@@ -1,3 +1,3 @@
/* GENERATED by @estella/compiler — do not edit. */
/* The contract half of the handshake. The width half is the harness. */
-#define ES_EXPECTED_CONTRACT_HASH 0xe83b13ad2f96720cULL
+#define ES_EXPECTED_CONTRACT_HASH 0xf1506f426a7da360ULL
diff --git a/tests/aot/test_aot_host.cpp b/tests/aot/test_aot_host.cpp
index e9e773e43..1a5945f1b 100644
--- a/tests/aot/test_aot_host.cpp
+++ b/tests/aot/test_aot_host.cpp
@@ -35,6 +35,7 @@
#include "esengine/aot/AotComponents.generated.hpp"
#include "esengine/aot/AotModule.hpp"
#include "esengine/aot/AotDispatcher.hpp"
+#include "esengine/aot/EngineDigest.generated.h"
#include "esengine/ecs/Registry.hpp"
#include "esengine/ecs/components/Transform.hpp"
@@ -466,3 +467,14 @@ TEST_CASE("a system this host cannot name is left unbound, not refused") {
}
}
#endif
+
+TEST_CASE("the digest a host carries is the one a native module bakes") {
+ // One number, or a host refuses every module built for it — which reads as
+ // "AOT does not work here" rather than as a constant that drifted.
+ CHECK(ES_ENGINE_ABI_DIGEST_64 == ES_EXPECTED_CONTRACT_HASH);
+ CHECK(ES_ENGINE_ABI_DIGEST_32 != ES_ENGINE_ABI_DIGEST_64);
+ // And the one the preprocessor picks is this machine's.
+ CHECK(ES_ENGINE_ABI_DIGEST == (sizeof(es_addr_t) == 8
+ ? ES_ENGINE_ABI_DIGEST_64
+ : ES_ENGINE_ABI_DIGEST_32));
+}