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
fix: propagate wasm instantiation failure, null-check spine readers
esenginea03a248228ae
diff --git a/sdk/src/spine/SpineModuleLoader.ts b/sdk/src/spine/SpineModuleLoader.tsindex 3c96e237f..0dc22b1cb 100644--- a/sdk/src/spine/SpineModuleLoader.ts+++ b/sdk/src/spine/SpineModuleLoader.ts@@ -186,16 +186,23 @@ export function createSpineFactories(provider: SpineWasmProvider): Map<SpineVersconst moduleFactory = (globalThis as any).__ESSpineModule__ as(opts: Record<string, unknown>) => Promise<SpineWasmModule>;delete (globalThis as any).__ESSpineModule__;- return moduleFactory({+ // emscripten's instantiateWasm has no failure channel: surface an async+ // instantiation failure through a reject gate so the factory promise+ // rejects instead of hanging the spine module load forever.+ let rejectOnError: (e: unknown) => void = () => {};+ const errorGate = new Promise<never>((_, reject) => { rejectOnError = reject; });+ const modulePromise = moduleFactory({instantiateWasm(imports: WebAssembly.Imports, cb: Function) {WebAssembly.instantiate(wasmBytes, imports).then(r => cb(r.instance, r.module),).catch(e => {log.error('spine', 'WASM instantiation failed', e);+ rejectOnError(e);});return {};},});+ return Promise.race([modulePromise, errorGate]);});}diff --git a/sdk/src/wechatRuntime.ts b/sdk/src/wechatRuntime.tsindex 471857f3f..2dcd55df2 100644--- a/sdk/src/wechatRuntime.ts+++ b/sdk/src/wechatRuntime.ts@@ -86,25 +86,43 @@ function createPathResolver(index: ManifestIndex): (ref: string) => string {// Emscripten WASM Instantiation// =============================================================================-function createWasmInstantiator(wasmPath: string) {+function createWasmInstantiator(wasmPath: string, onError?: (e: unknown) => void) {return (imports: WebAssembly.Imports, successCallback: Function) => {platformInstantiateWasm(wasmPath, imports).then((result) => {successCallback(result.instance, result.module);}).catch((e) => {log.error('wechat', 'WASM instantiation failed', e);+ // emscripten's instantiateWasm has no failure channel: on a failed+ // async instantiation successCallback is never called and the factory+ // promise hangs forever. Surface the error so the caller can reject.+ onError?.(e);});return {};};}+// Wraps an emscripten module factory so an async instantiateWasm failure rejects+// the returned promise instead of hanging the module load indefinitely.+function instantiateModule<T>(+ factory: (opts: unknown) => Promise<T>,+ wasmPath: string,+ extraOpts: Record<string, unknown> = {},+): Promise<T> {+ let rejectOnError: (e: unknown) => void = () => {};+ const errorGate = new Promise<never>((_, reject) => { rejectOnError = reject; });+ const modulePromise = factory({+ ...extraOpts,+ instantiateWasm: createWasmInstantiator(wasmPath, rejectOnError),+ });+ return Promise.race([modulePromise, errorGate]);+}+async function initWasmModule<T>(factory: (opts: unknown) => Promise<T>,wasmPath: string,): Promise<T | null> {try {- return await factory({- instantiateWasm: createWasmInstantiator(wasmPath),- });+ return await instantiateModule(factory, wasmPath);} catch (e) {log.warn('wechat', `Failed to load WASM module: ${wasmPath}`, e);return null;@@ -136,10 +154,7 @@ export async function initWeChatRuntime(config: WeChatRuntimeConfig): Promise<vocanvas.width = info.windowWidth * info.pixelRatio;canvas.height = info.windowHeight * info.pixelRatio;- const module = await config.engineFactory({- canvas,- instantiateWasm: createWasmInstantiator('esengine.wasm'),- });+ const module = await instantiateModule(config.engineFactory, 'esengine.wasm', { canvas });const gl = (canvas.getContext('webgl2') || canvas.getContext('webgl')) as WebGLRenderingContext | null;if (!gl) {diff --git a/src/esengine/bindings/SpineModuleEntry.cpp b/src/esengine/bindings/SpineModuleEntry.cppindex 3d5bbf8af..f07b9ee7b 100644--- a/src/esengine/bindings/SpineModuleEntry.cpp+++ b/src/esengine/bindings/SpineModuleEntry.cpp@@ -182,6 +182,12 @@ int spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,if (isBinary) {spSkeletonBinary* binary = spSkeletonBinary_create(handle.atlas);+ if (!binary) {+ g_ctx.lastError = "Failed to create skeleton binary reader";+ destroySkeleton(handle);+ g_ctx.skeletons.erase(id);+ return -1;+ }binary->scale = 1.0f;handle.skeletonData = spSkeletonBinary_readSkeletonData(binary, reinterpret_cast<const unsigned char*>(skelDataPtr), skelDataLen);@@ -191,6 +197,12 @@ int spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,spSkeletonBinary_dispose(binary);} else {spSkeletonJson* json = spSkeletonJson_create(handle.atlas);+ if (!json) {+ g_ctx.lastError = "Failed to create skeleton json reader";+ destroySkeleton(handle);+ g_ctx.skeletons.erase(id);+ return -1;+ }json->scale = 1.0f;handle.skeletonData = spSkeletonJson_readSkeletonData(json, reinterpret_cast<const char*>(skelDataPtr));