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(sdk): a device answers for DragonBones, which it already carried

yhhdf85d03f3966
diff --git a/sdk/src/ecs/nativeSideModules.ts b/sdk/src/ecs/nativeSideModules.ts
index 6086eda2e..db1f71804 100644
--- a/sdk/src/ecs/nativeSideModules.ts
+++ b/sdk/src/ecs/nativeSideModules.ts
@@ -25,6 +25,9 @@ import { createSideModuleHost, type SideModuleHost, type SideModule } from '../s
import { SPINE_VERSIONS, spineModuleId, type SideModuleId } from '../sideModules/registry';
import { log } from '../logger';
import { createNativeHeap, type NativeHeap } from './nativeHeap';
+// Not TextEncoder/TextDecoder: this file runs inside QuickJS on a device, which
+// has neither. See utf8.ts.
+import { encodeUtf8, decodeUtf8 } from '../utf8';
/**
* One global per subsystem that proves the host compiled it in. Picked from the
@@ -46,6 +49,9 @@ const SPINE_PROBES = SPINE_VERSIONS.reduce<Partial<Record<SideModuleId, string>>
export const NATIVE_SIDE_MODULE_PROBES: Partial<Record<SideModuleId, string>> = {
physics: 'es_physics_init',
videodec: 'es_video_open',
+ // One entry, not one per version: the DragonBones format is frozen, so unlike
+ // Spine there is no "which runtime did this binary link" question to ask.
+ dragonbones: 'es_db_loadSkeleton',
...SPINE_PROBES,
};
@@ -67,12 +73,12 @@ function readUtf8(heap: NativeHeap, ptr: number): string {
if (!ptr) return '';
let end = ptr;
while (end < heap.HEAPU8.length && heap.HEAPU8[end] !== 0) end++;
- return new TextDecoder().decode(heap.HEAPU8.subarray(ptr, end));
+ return decodeUtf8(heap.HEAPU8.subarray(ptr, end));
}
/** Copy `str` into the heap as NUL-terminated UTF-8; the caller frees it. */
function writeUtf8(heap: NativeHeap, str: string): number {
- const bytes = new TextEncoder().encode(str);
+ const bytes = encodeUtf8(str);
const ptr = heap._malloc(bytes.length + 1);
if (!ptr) return 0;
heap.HEAPU8.set(bytes, ptr);
diff --git a/sdk/tests/binding-surface.test.ts b/sdk/tests/binding-surface.test.ts
index 7ba0c4fe9..a1cbd3b59 100644
--- a/sdk/tests/binding-surface.test.ts
+++ b/sdk/tests/binding-surface.test.ts
@@ -394,6 +394,39 @@ describe('WASM binding surface: side modules (C exports)', () => {
.toEqual([]);
});
+ // Read off the WRAPPED interface rather than the cwrap call sites, because this
+ // loader binds through `w('loadSkeleton', …)` and lets the helper add the `db_`
+ // — there is no `cwrap('db_loadSkeleton')` literal to find. The interface is a
+ // faithful stand-in for the table: tsc rejects a member with no `w(…)` behind it
+ // and a `w(…)` with no member, so the two cannot drift.
+ it('DragonBonesWrappedAPI mirrors the module exports exactly', () => {
+ const cpp = parseKeepaliveExports(read(resolve(CPP, 'bindings/DragonBonesModuleEntry.cpp')));
+ const loaderTs = read(resolve(SDK, 'dragonbones/DragonBonesModuleLoader.ts'));
+ const declared = new Set(
+ [...parseInterfaceMethods(loaderTs, 'DragonBonesWrappedAPI')].map((n) => `_db_${n}`),
+ );
+ expectMirrored('dragonbones', declared, cpp);
+ });
+
+ // The native half, as for physics and spine. This one has already been the bug
+ // it guards: the module was linked into the Android host and unreachable from
+ // it, because the ABI lived only in the .cpp and the generator had nothing to
+ // read. A declaration missing here is an entry point a device does not answer.
+ it('DragonBonesBindings.hpp declares exactly what the module exports', () => {
+ const header = stripComments(read(resolve(CPP, 'bindings/DragonBonesBindings.hpp')));
+ const declared = new Set([...header.matchAll(/\b(db_\w+)\s*\(/g)].map((m) => m[1]!));
+ const exported = new Set(
+ [...parseKeepaliveExports(read(resolve(CPP, 'bindings/DragonBonesModuleEntry.cpp')))]
+ .filter((n) => n.startsWith('db_')),
+ );
+ const undeclared = [...exported].filter((n) => !declared.has(n)).sort();
+ const phantom = [...declared].filter((n) => !exported.has(n)).sort();
+ expect(undeclared, `exported but not declared in DragonBonesBindings.hpp: ${undeclared.join(', ')}`)
+ .toEqual([]);
+ expect(phantom, `declared in DragonBonesBindings.hpp but defined nowhere: ${phantom.join(', ')}`)
+ .toEqual([]);
+ });
+
it('BasisWasmModule mirrors the basis EXPORTED_FUNCTIONS list exactly', () => {
const cmake = read(resolve(CPP, '../../cmake/Emscripten.cmake'));
const exported = parseExportedFunctions(cmake, 'es_basis');
diff --git a/sdk/tests/native-side-modules.test.ts b/sdk/tests/native-side-modules.test.ts
index 8b849cb8e..cebb4b9d2 100644
--- a/sdk/tests/native-side-modules.test.ts
+++ b/sdk/tests/native-side-modules.test.ts
@@ -14,10 +14,14 @@ import {
} from '../src/ecs/nativeSideModules';
import { HEAP_BINDINGS } from '../src/ecs/nativeHeap';
import type { PhysicsWasmModule } from '../src/physics/PhysicsModuleLoader';
+import { wrapDragonBonesModule } from '../src/dragonbones/DragonBonesModuleLoader';
+import type { DragonBonesWasmModule } from '../src/dragonbones/DragonBonesModuleLoader';
/** A host that compiled physics in: the heap plus a few es_physics_* globals. */
-function mockNativeHost(options: { physics?: boolean; heap?: boolean; spine?: number } = {}) {
- const { physics = true, heap = true, spine } = options;
+function mockNativeHost(options: {
+ physics?: boolean; heap?: boolean; spine?: number; dragonbones?: boolean;
+} = {}) {
+ const { physics = true, heap = true, spine, dragonbones = false } = options;
const buffer = new ArrayBuffer(4096);
let next = 8;
const calls: { name: string; args: unknown[] }[] = [];
@@ -48,6 +52,13 @@ function mockNativeHost(options: { physics?: boolean; heap?: boolean; spine?: nu
// and writes them, so nothing here needs the heap.
scope['es_spine_getAnimations'] = () => 'idle,walk,run';
}
+ if (dragonbones) {
+ // One runtime, no version to declare: the format is frozen, so unlike Spine
+ // there is nothing for the host to disagree with the content about.
+ scope['es_db_loadSkeleton'] = (...args: unknown[]) => { calls.push({ name: 'loadSkeleton', args }); return 1; };
+ scope['es_db_createInstance'] = (...args: unknown[]) => { calls.push({ name: 'createInstance', args }); return 0; };
+ scope['es_db_getArmatures'] = () => '["Dragon"]';
+ }
return { scope, buffer, calls };
}
@@ -76,6 +87,24 @@ describe('createNativeSideModules', () => {
expect(NATIVE_SIDE_MODULE_PROBES.basis).toBeUndefined();
});
+ // The DragonBones module was linked into the Android host and unreachable from
+ // it for exactly one missing probe entry, which is the shape of failure this
+ // pins: the acquirer answering null while the binary carries the runtime.
+ it('answers DragonBones when the host compiled it in', async () => {
+ const { scope } = mockNativeHost({ dragonbones: true });
+ const mod = await createNativeSideModules(scope).acquire('dragonbones');
+ expect(mod).not.toBeNull();
+ // Through the SAME cwrap the web loader uses, so the controller cannot tell
+ // a compiled-in runtime from a fetched one.
+ const wrapped = wrapDragonBonesModule(mod as unknown as DragonBonesWasmModule);
+ expect(wrapped.getArmatures(1)).toBe('["Dragon"]');
+ });
+
+ it('answers null for DragonBones when the host did not compile it in', async () => {
+ const { scope } = mockNativeHost();
+ expect(await createNativeSideModules(scope).acquire('dragonbones')).toBeNull();
+ });
+
it('caches, so two consumers share one view', async () => {
const host = createNativeSideModules(mockNativeHost().scope);
expect(await host.acquire('physics')).toBe(await host.acquire('physics'));