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.

refactor(wasm): remove vestigial 'as any' casts at WASM boundary

esengined6c7a6236ef1
diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.ts
index d91996ec6..08848b9f5 100644
--- a/sdk/src/asset/loaders/TextureLoader.ts
+++ b/sdk/src/asset/loaders/TextureLoader.ts
@@ -92,7 +92,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {
private getWebGL2Context(): WebGL2RenderingContext | null {
try {
- const glObj = (this.module_ as any).GL;
+ const glObj = this.module_.GL;
if (glObj?.currentContext?.GLctx instanceof WebGL2RenderingContext) {
return glObj.currentContext.GLctx;
}
@@ -119,7 +119,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {
gl.generateMipmap(gl.TEXTURE_2D);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
- const glObj = (this.module_ as any).GL;
+ const glObj = this.module_.GL;
const glTextureId = glObj.getNewId(glObj.textures);
glObj.textures[glTextureId] = texture;
diff --git a/sdk/src/spine/SpineCppAPI.ts b/sdk/src/spine/SpineCppAPI.ts
index 978c507fd..9bd19b6b6 100644
--- a/sdk/src/spine/SpineCppAPI.ts
+++ b/sdk/src/spine/SpineCppAPI.ts
@@ -49,38 +49,38 @@ export const SpineCpp = {
},
listConstraints(entity: Entity): { ik: string[]; transform: string[]; path: string[] } | null {
- return (module as any)?.spine_native_listConstraints?.(entity) ?? null;
+ return module?.spine_native_listConstraints?.(entity) ?? null;
},
getTransformConstraintMix(entity: Entity, name: string): { mixRotate: number; mixX: number; mixY: number; mixScaleX: number; mixScaleY: number; mixShearY: number } | null {
- return (module as any)?.spine_native_getTransformConstraintMix?.(entity, name) ?? null;
+ return module?.spine_native_getTransformConstraintMix?.(entity, name) ?? null;
},
setTransformConstraintMix(entity: Entity, name: string, rotate: number, x: number, y: number, scaleX: number, scaleY: number, shearY: number): boolean {
- return (module as any)?.spine_native_setTransformConstraintMix?.(entity, name, rotate, x, y, scaleX, scaleY, shearY) ?? false;
+ return module?.spine_native_setTransformConstraintMix?.(entity, name, rotate, x, y, scaleX, scaleY, shearY) ?? false;
},
getPathConstraintMix(entity: Entity, name: string): { position: number; spacing: number; mixRotate: number; mixX: number; mixY: number } | null {
- return (module as any)?.spine_native_getPathConstraintMix?.(entity, name) ?? null;
+ return module?.spine_native_getPathConstraintMix?.(entity, name) ?? null;
},
setPathConstraintMix(entity: Entity, name: string, position: number, spacing: number, rotate: number, x: number, y: number): boolean {
- return (module as any)?.spine_native_setPathConstraintMix?.(entity, name, position, spacing, rotate, x, y) ?? false;
+ return module?.spine_native_setPathConstraintMix?.(entity, name, position, spacing, rotate, x, y) ?? false;
},
getEventCount(): number {
- return (module as any)?.spine_native_getEventCount?.() ?? 0;
+ return module?.spine_native_getEventCount?.() ?? 0;
},
getEventRecord(index: number): { entity: number; animationName: string; eventName: string; stringValue: string } | null {
- return (module as any)?.spine_native_getEventRecord?.(index) ?? null;
+ return module?.spine_native_getEventRecord?.(index) ?? null;
},
getEventBuffer(): number {
- return (module as any)?.spine_native_getEventBuffer?.() ?? 0;
+ return module?.spine_native_getEventBuffer?.() ?? 0;
},
clearEvents(): void {
- (module as any)?.spine_native_clearEvents?.();
+ module?.spine_native_clearEvents?.();
},
};
diff --git a/sdk/src/timeline/TimelineRuntime.ts b/sdk/src/timeline/TimelineRuntime.ts
index d05151595..d744a1f54 100644
--- a/sdk/src/timeline/TimelineRuntime.ts
+++ b/sdk/src/timeline/TimelineRuntime.ts
@@ -97,7 +97,7 @@ function decodeEventString(module: ESEngineModule, index: number): string {
const ptr = module._tl_getEventString(index);
const len = module._tl_getEventStringLen(index);
if (!ptr || len <= 0) return '';
- const bytes = new Uint8Array((module as any).HEAPU8.buffer, ptr, len);
+ const bytes = new Uint8Array(module.HEAPU8.buffer, ptr, len);
return new TextDecoder().decode(bytes);
}
diff --git a/sdk/src/timeline/TimelineUploader.ts b/sdk/src/timeline/TimelineUploader.ts
index f2ef71e3b..b4d8b2db5 100644
--- a/sdk/src/timeline/TimelineUploader.ts
+++ b/sdk/src/timeline/TimelineUploader.ts
@@ -25,13 +25,13 @@ function resolveField(component: string, property: string): AnimTargetField {
function allocString(module: ESEngineModule, str: string): [number, number] {
const encoder = new TextEncoder();
const bytes = encoder.encode(str);
- const ptr = (module as any)._malloc(bytes.length);
- new Uint8Array((module as any).HEAPU8.buffer, ptr, bytes.length).set(bytes);
+ const ptr = module._malloc(bytes.length);
+ new Uint8Array(module.HEAPU8.buffer, ptr, bytes.length).set(bytes);
return [ptr, bytes.length];
}
function freePtr(module: ESEngineModule, ptr: number): void {
- (module as any)._free(ptr);
+ module._free(ptr);
}
export interface UploadedTrackInfo {
@@ -89,7 +89,6 @@ export function uploadTimelineToWasm(module: ESEngineModule, asset: TimelineAsse
}
function uploadPropertyTrack(module: ESEngineModule, handle: number, track: PropertyTrack): void {
- const mod = module as any;
const channelCount = track.channels.length;
const component = COMPONENT_MAP[track.component] ?? AnimTargetComponent.Custom;
const isCustom = component === AnimTargetComponent.Custom;
@@ -101,8 +100,8 @@ function uploadPropertyTrack(module: ESEngineModule, handle: number, track: Prop
const [childPtr, childLen] = allocString(module, track.childPath);
- const fieldsPtr = mod._malloc(channelCount);
- const fieldsArr = new Uint8Array(mod.HEAPU8.buffer, fieldsPtr, channelCount);
+ const fieldsPtr = module._malloc(channelCount);
+ const fieldsArr = new Uint8Array(module.HEAPU8.buffer, fieldsPtr, channelCount);
for (let i = 0; i < channelCount; i++) {
fieldsArr[i] = resolveField(track.component, track.channels[i].property);
}
@@ -114,11 +113,11 @@ function uploadPropertyTrack(module: ESEngineModule, handle: number, track: Prop
totalKeyframes += ch.keyframes.length;
}
- const countsPtr = mod._malloc(channelCount * 4);
- new Int32Array(mod.HEAPU8.buffer, countsPtr, channelCount).set(counts);
+ const countsPtr = module._malloc(channelCount * 4);
+ new Int32Array(module.HEAPU8.buffer, countsPtr, channelCount).set(counts);
- const dataPtr = mod._malloc(totalKeyframes * 5 * 4);
- const dataArr = new Float32Array(mod.HEAPU8.buffer, dataPtr, totalKeyframes * 5);
+ const dataPtr = module._malloc(totalKeyframes * 5 * 4);
+ const dataArr = new Float32Array(module.HEAPU8.buffer, dataPtr, totalKeyframes * 5);
let offset = 0;
for (const ch of track.channels) {
for (const kf of ch.keyframes) {
@@ -140,7 +139,6 @@ function uploadPropertyTrack(module: ESEngineModule, handle: number, track: Prop
}
function uploadCustomPropertyTrack(module: ESEngineModule, handle: number, track: PropertyTrack): void {
- const mod = module as any;
const channelCount = track.channels.length;
const [childPtr, childLen] = allocString(module, track.childPath);
@@ -150,15 +148,15 @@ function uploadCustomPropertyTrack(module: ESEngineModule, handle: number, track
let totalPathBytes = 0;
for (const p of pathStrings) totalPathBytes += p.length;
- const fieldPathsPtr = mod._malloc(totalPathBytes);
+ const fieldPathsPtr = module._malloc(totalPathBytes);
let pathOffset = 0;
for (const p of pathStrings) {
- new Uint8Array(mod.HEAPU8.buffer, fieldPathsPtr + pathOffset, p.length).set(p);
+ new Uint8Array(module.HEAPU8.buffer, fieldPathsPtr + pathOffset, p.length).set(p);
pathOffset += p.length;
}
- const fieldPathLensPtr = mod._malloc(channelCount * 4);
- new Int32Array(mod.HEAPU8.buffer, fieldPathLensPtr, channelCount).set(pathStrings.map(p => p.length));
+ const fieldPathLensPtr = module._malloc(channelCount * 4);
+ new Int32Array(module.HEAPU8.buffer, fieldPathLensPtr, channelCount).set(pathStrings.map(p => p.length));
let totalKeyframes = 0;
const counts: number[] = [];
@@ -167,11 +165,11 @@ function uploadCustomPropertyTrack(module: ESEngineModule, handle: number, track
totalKeyframes += ch.keyframes.length;
}
- const countsPtr = mod._malloc(channelCount * 4);
- new Int32Array(mod.HEAPU8.buffer, countsPtr, channelCount).set(counts);
+ const countsPtr = module._malloc(channelCount * 4);
+ new Int32Array(module.HEAPU8.buffer, countsPtr, channelCount).set(counts);
- const dataPtr = mod._malloc(totalKeyframes * 5 * 4);
- const dataArr = new Float32Array(mod.HEAPU8.buffer, dataPtr, totalKeyframes * 5);
+ const dataPtr = module._malloc(totalKeyframes * 5 * 4);
+ const dataArr = new Float32Array(module.HEAPU8.buffer, dataPtr, totalKeyframes * 5);
let dOffset = 0;
for (const ch of track.channels) {
for (const kf of ch.keyframes) {
@@ -197,16 +195,15 @@ function uploadCustomPropertyTrack(module: ESEngineModule, handle: number, track
function uploadSpineTrack(module: ESEngineModule, handle: number, track: Track): void {
if (track.type !== TrackType.Spine) return;
- const mod = module as any;
const [childPtr, childLen] = allocString(module, track.childPath);
const clipCount = track.clips.length;
- const floatsPtr = mod._malloc(clipCount * 4 * 4);
- const floats = new Float32Array(mod.HEAPU8.buffer, floatsPtr, clipCount * 4);
- const animPtrsPtr = mod._malloc(clipCount * 4);
- const animPtrs = new Uint32Array(mod.HEAPU8.buffer, animPtrsPtr, clipCount);
- const animLensPtr = mod._malloc(clipCount * 4);
- const animLens = new Int32Array(mod.HEAPU8.buffer, animLensPtr, clipCount);
+ const floatsPtr = module._malloc(clipCount * 4 * 4);
+ const floats = new Float32Array(module.HEAPU8.buffer, floatsPtr, clipCount * 4);
+ const animPtrsPtr = module._malloc(clipCount * 4);
+ const animPtrs = new Uint32Array(module.HEAPU8.buffer, animPtrsPtr, clipCount);
+ const animLensPtr = module._malloc(clipCount * 4);
+ const animLens = new Int32Array(module.HEAPU8.buffer, animLensPtr, clipCount);
const animStrPtrs: number[] = [];
for (let i = 0; i < clipCount; i++) {
@@ -233,16 +230,15 @@ function uploadSpineTrack(module: ESEngineModule, handle: number, track: Track):
function uploadAudioTrack(module: ESEngineModule, handle: number, track: Track): void {
if (track.type !== TrackType.Audio) return;
- const mod = module as any;
const [childPtr, childLen] = allocString(module, track.childPath);
const eventCount = track.events.length;
- const floatsPtr = mod._malloc(eventCount * 2 * 4);
- const floats = new Float32Array(mod.HEAPU8.buffer, floatsPtr, eventCount * 2);
- const clipPtrsPtr = mod._malloc(eventCount * 4);
- const clipPtrs = new Uint32Array(mod.HEAPU8.buffer, clipPtrsPtr, eventCount);
- const clipLensPtr = mod._malloc(eventCount * 4);
- const clipLens = new Int32Array(mod.HEAPU8.buffer, clipLensPtr, eventCount);
+ const floatsPtr = module._malloc(eventCount * 2 * 4);
+ const floats = new Float32Array(module.HEAPU8.buffer, floatsPtr, eventCount * 2);
+ const clipPtrsPtr = module._malloc(eventCount * 4);
+ const clipPtrs = new Uint32Array(module.HEAPU8.buffer, clipPtrsPtr, eventCount);
+ const clipLensPtr = module._malloc(eventCount * 4);
+ const clipLens = new Int32Array(module.HEAPU8.buffer, clipLensPtr, eventCount);
const strPtrs: number[] = [];
for (let i = 0; i < eventCount; i++) {
@@ -267,11 +263,10 @@ function uploadAudioTrack(module: ESEngineModule, handle: number, track: Track):
function uploadActivationTrack(module: ESEngineModule, handle: number, track: Track): void {
if (track.type !== TrackType.Activation) return;
- const mod = module as any;
const [childPtr, childLen] = allocString(module, track.childPath);
- const rangesPtr = mod._malloc(track.ranges.length * 2 * 4);
- const rangesArr = new Float32Array(mod.HEAPU8.buffer, rangesPtr, track.ranges.length * 2);
+ const rangesPtr = module._malloc(track.ranges.length * 2 * 4);
+ const rangesArr = new Float32Array(module.HEAPU8.buffer, rangesPtr, track.ranges.length * 2);
for (let i = 0; i < track.ranges.length; i++) {
rangesArr[i * 2] = track.ranges[i].start;
rangesArr[i * 2 + 1] = track.ranges[i].end;
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index 732f1cab8..0124c6da4 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -83,6 +83,12 @@ export interface ESEngineModule {
minorVersion: number;
enableExtensionsByDefault?: boolean;
}): number;
+ /** Emscripten-internal: active GL context record (populated after registerContext). */
+ currentContext?: { GLctx: WebGLRenderingContext | WebGL2RenderingContext };
+ /** Emscripten-internal: allocate a new handle id into the given object pool. */
+ getNewId(pool: Record<number, unknown>): number;
+ /** Emscripten-internal: texture object pool keyed by handle id. */
+ textures: Record<number, WebGLTexture>;
};
renderFrame(registry: CppRegistry, width: number, height: number): void;
renderFrameWithMatrix(registry: CppRegistry, width: number, height: number, matrixPtr: number): void;
@@ -99,6 +105,33 @@ export interface ESEngineModule {
spine_getAnimations?(entity: number): string[];
spine_getSkins?(entity: number): string[];
spine_setNeedsReload?(registry: CppRegistry, entity: number, value: boolean): void;
+
+ // Spine native (4.2) constraint + event accessors — only present when
+ // the 4.2 runtime is compiled in; optional on module interface.
+ spine_native_listConstraints?(entity: number): { ik: string[]; transform: string[]; path: string[] } | null;
+ spine_native_getTransformConstraintMix?(entity: number, name: string): {
+ mixRotate: number; mixX: number; mixY: number;
+ mixScaleX: number; mixScaleY: number; mixShearY: number;
+ } | null;
+ spine_native_setTransformConstraintMix?(
+ entity: number, name: string,
+ rotate: number, x: number, y: number,
+ scaleX: number, scaleY: number, shearY: number,
+ ): boolean;
+ spine_native_getPathConstraintMix?(entity: number, name: string): {
+ position: number; spacing: number; mixRotate: number; mixX: number; mixY: number;
+ } | null;
+ spine_native_setPathConstraintMix?(
+ entity: number, name: string,
+ position: number, spacing: number,
+ rotate: number, x: number, y: number,
+ ): boolean;
+ spine_native_getEventCount?(): number;
+ spine_native_getEventRecord?(index: number): {
+ entity: number; animationName: string; eventName: string; stringValue: string;
+ } | null;
+ spine_native_getEventBuffer?(): number;
+ spine_native_clearEvents?(): void;
renderer_submitSpineBatch?(
verticesPtr: number, vertexCount: number,
indicesPtr: number, indexCount: number,
diff --git a/sdk/src/wechatRuntime.ts b/sdk/src/wechatRuntime.ts
index 504587627..471857f3f 100644
--- a/sdk/src/wechatRuntime.ts
+++ b/sdk/src/wechatRuntime.ts
@@ -147,8 +147,8 @@ export async function initWeChatRuntime(config: WeChatRuntimeConfig): Promise<vo
return;
}
- const glHandle = (module as any).GL.registerContext(gl, {
- majorVersion: (gl as any).getParameter((gl as any).VERSION).indexOf('WebGL 2') === 0 ? 2 : 1,
+ const glHandle = module.GL.registerContext(gl, {
+ majorVersion: String(gl.getParameter(gl.VERSION)).indexOf('WebGL 2') === 0 ? 2 : 1,
minorVersion: 0,
enableExtensionsByDefault: true,
});