diff --git a/@types/pal/wasm.d.ts b/@types/pal/wasm.d.ts index e840593860..d6783b940a 100644 --- a/@types/pal/wasm.d.ts +++ b/@types/pal/wasm.d.ts @@ -7,7 +7,7 @@ declare module 'pal/wasm' { * @param wasmUrl the url of wasm, this should be a url relative from build output chunk. * @param importObject the standard `WebAssembly.Imports` instance */ - export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise; + export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise; /** * Fetch binary data from wasm url or js mem url. diff --git a/cc.config.json b/cc.config.json index 57f44cd329..b99df9c664 100644 --- a/cc.config.json +++ b/cc.config.json @@ -653,7 +653,7 @@ "WASM_SUPPORT_MODE": { "comment": "Whether support wasm, here we provide 3 options:\n0: The platform doesn't support WASM\n1: The platform supports WASM\n2: The platform may support WASM, especially on Web platform", "type": "number", - "value": "$HTML5 ? 2 : ($NATIVE ? (($OPEN_HARMONY||$IOS)?0:1): ($MINIGAME? ($WECHAT?1:0) :0))", + "value": "($HTML5 || $BYTEDANCE) ? 2 : ($NATIVE ? (($OPEN_HARMONY||$IOS)?0:1): ($MINIGAME? ($WECHAT?1:0) :0))", "internal": true }, "FORCE_BANNING_BULLET_WASM": { diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index 3c8db43d1c..de79e228cd 100644 --- a/cocos/physics-2d/box2d-wasm/instantiated.ts +++ b/cocos/physics-2d/box2d-wasm/instantiated.ts @@ -143,8 +143,8 @@ function initWasm (wasmFactory, wasmUrl: string): Promise { receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void, ) { // NOTE: the Promise return by instantiateWasm hook can't be caught. - instantiateWasm(wasmUrl, importObject).then((result: any) => { - receiveInstance(result.instance as WebAssembly.Instance, result.module as WebAssembly.Module); + instantiateWasm(wasmUrl, importObject).then((result) => { + receiveInstance(result.instance, result.module); }).catch((err) => reject(errorMessage(err))); }, }).then((Instance: any) => { diff --git a/cocos/physics/bullet/instantiated.ts b/cocos/physics/bullet/instantiated.ts index 967831146b..6057a33a05 100644 --- a/cocos/physics/bullet/instantiated.ts +++ b/cocos/physics/bullet/instantiated.ts @@ -100,8 +100,8 @@ function initWASM (wasmFactory, wasmUrl: string): Promise { receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void, ) { // NOTE: the Promise return by instantiateWasm hook can't be caught. - instantiateWasm(wasmUrl, importObject).then((result: any) => { - receiveInstance(result.instance as WebAssembly.Instance, result.module as WebAssembly.Module); + instantiateWasm(wasmUrl, importObject).then((result) => { + receiveInstance(result.instance, result.module); }).catch((err) => reject(errorMessage(err))); }, }).then((instance: any) => { diff --git a/cocos/physics/physx/physx-adapter.ts b/cocos/physics/physx/physx-adapter.ts index 56db9bc525..809b7a41c7 100644 --- a/cocos/physics/physx/physx-adapter.ts +++ b/cocos/physics/physx/physx-adapter.ts @@ -79,7 +79,7 @@ export function InitPhysXLibs (): Promise { } } -function InitPhysXLibsInternal (physxWasmFactory, physxWasmUrl, physxAsmFactory): any { +function InitPhysXLibsInternal (physxWasmFactory, physxWasmUrl: string, physxAsmFactory): any { if (shouldUseWasmModule()) { return initWASM(physxWasmFactory, physxWasmUrl); } else { @@ -104,7 +104,7 @@ function initASM (physxAsmFactory): any { } } -function initWASM (physxWasmFactory, physxWasmUrl): any { +function initWASM (physxWasmFactory, physxWasmUrl: string): any { globalThis.PhysX = globalThis.PHYSX ? globalThis.PHYSX : physxWasmFactory; if (globalThis.PhysX != null) { return globalThis.PhysX({ @@ -112,7 +112,7 @@ function initWASM (physxWasmFactory, physxWasmUrl): any { importObject: WebAssembly.Imports, receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void, ): any { - return instantiateWasm(physxWasmUrl, importObject).then((result: any): void => { + return instantiateWasm(physxWasmUrl, importObject).then((result): void => { receiveInstance(result.instance, result.module); }); }, diff --git a/cocos/spine/lib/instantiated.ts b/cocos/spine/lib/instantiated.ts index 1adf8f8aa8..7e7d61bc2b 100644 --- a/cocos/spine/lib/instantiated.ts +++ b/cocos/spine/lib/instantiated.ts @@ -42,14 +42,16 @@ let wasmInstance: SpineWasm.instance = null!; const registerList: any[] = []; /////////////////////////////////////////////////////////////////////////////////////////////////// -function initWasm (wasmFactory, wasmUrl): Promise { +function initWasm (wasmFactory, wasmUrl: string): Promise { return new Promise((resolve, reject) => { const errorMessage = (err: any): string => `[Spine]: Spine wasm load failed: ${err}`; wasmFactory({ - instantiateWasm (importObject: WebAssembly.Imports, - receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void) { + instantiateWasm ( + importObject: WebAssembly.Imports, + receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void, + ) { // NOTE: the Promise return by instantiateWasm hook can't be caught. - instantiateWasm(wasmUrl, importObject).then((result: any) => { + instantiateWasm(wasmUrl, importObject).then((result) => { receiveInstance(result.instance, result.module); }).catch((err) => reject(errorMessage(err))); }, @@ -62,7 +64,7 @@ function initWasm (wasmFactory, wasmUrl): Promise { }); } -function initAsmJS (asmFactory, asmJsMemUrl): Promise { +function initAsmJS (asmFactory, asmJsMemUrl: string): Promise { return new Promise((resolve, reject) => { if (CULL_ASM_JS_MODULE) { reject(getError(4601)); diff --git a/pal/system-info/minigame/system-info.ts b/pal/system-info/minigame/system-info.ts index b00600cfa5..4af29eefaa 100644 --- a/pal/system-info/minigame/system-info.ts +++ b/pal/system-info/minigame/system-info.ts @@ -61,6 +61,37 @@ if (WECHAT) { currentPlatform = Platform.QTT_MINI_GAME; } +let isVersionGreaterOrEqualTo; +if (BYTEDANCE) { + isVersionGreaterOrEqualTo = function isVersionGreaterOrEqualTo (versionA: string, versionB: string): boolean { + // Split the version number string into an array of integers + function parseVersion (version: string): number[] { + return version.split('.').map((part: string) => parseInt(part, 10)); + } + + // Parse the version number strings into arrays + const versionArrayA = parseVersion(versionA); + const versionArrayB = parseVersion(versionB); + + if (versionArrayA.length !== versionArrayB.length) { + return false; + } + + // Compare versions level by level + for (let i = 0; i < versionArrayA.length; i++) { + if (versionArrayA[i] > versionArrayB[i]) { + // versionA > versionB + return true; + } else if (versionArrayA[i] < versionArrayB[i]) { + // versionA < versionB + return false; + } + } + + // versionA === versionB + return true; + }; +} class SystemInfo extends EventTarget { public readonly networkType: NetworkType; public readonly isNative: boolean; @@ -129,6 +160,23 @@ class SystemInfo extends EventTarget { this.isXR = false; const isPCWechat = WECHAT && this.os === OS.WINDOWS && !minigame.isDevTool; + + const supportWasm = ((): boolean => { + if (WECHAT) { + return true; + } + + if (BYTEDANCE) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error TTWebAssembly is defined if bytedance client supports wasm. + if (isVersionGreaterOrEqualTo(minigameSysInfo.SDKVersion, '3.7.0') && typeof TTWebAssembly === 'object') { + return true; + } + } + + return false; + })(); + this._featureMap = { [Feature.WEBP]: false, // Initialize in Promise, [Feature.IMAGE_BITMAP]: false, @@ -146,7 +194,7 @@ class SystemInfo extends EventTarget { [Feature.EVENT_HANDLE]: this.isXR, [Feature.EVENT_HMD]: this.isXR, [Feature.EVENT_HANDHELD]: false, - [Feature.WASM]: WECHAT, + [Feature.WASM]: supportWasm, }; this._initPromise = []; diff --git a/pal/wasm/wasm-minigame.ts b/pal/wasm/wasm-minigame.ts index cd5e2c88f0..fa46f5ea82 100644 --- a/pal/wasm/wasm-minigame.ts +++ b/pal/wasm/wasm-minigame.ts @@ -28,21 +28,28 @@ import { basename } from '../../cocos/core/utils/path'; import { checkPalIntegrity, withImpl } from '../integrity-check'; import { log } from '../../cocos/core/platform/debug'; +// NOTE: The global variable `CCWebAssembly` is assigned in platforms/(bytedance|wechat)/wrapper/builtin/index.js +declare namespace CCWebAssembly { + // The first argument of `instantiate` function in mini-game platforms is always a wasm url. + function instantiate(url: string, importObject?: WebAssembly.Imports): Promise; +} + export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise { - return getPlatformBinaryUrl(wasmUrl).then((url) => WebAssembly.instantiate(url, importObject)); + return getPlatformBinaryUrl(wasmUrl).then((url: string) => CCWebAssembly.instantiate(url, importObject)); } export function fetchBuffer (binaryUrl: string): Promise { return new Promise((resolve, reject) => { getPlatformBinaryUrl(binaryUrl).then((url) => { // NOTE: fsUtils is defined in engine-adapter, we need to access globalThis explicitly for Taobao platform - globalThis.fsUtils.readArrayBuffer(url, (err, arrayBuffer) => { + globalThis.fsUtils.readArrayBuffer(url, (err, arrayBuffer: ArrayBuffer) => { if (err) { reject(err); return; } resolve(arrayBuffer); }); + // eslint-disable-next-line @typescript-eslint/no-empty-function }).catch((e) => {}); }); } diff --git a/pal/wasm/wasm-web.ts b/pal/wasm/wasm-web.ts index 6585310a34..b3912a369a 100644 --- a/pal/wasm/wasm-web.ts +++ b/pal/wasm/wasm-web.ts @@ -42,7 +42,7 @@ export function fetchBuffer (binaryUrl: string): Promise { // IDEA: it's better we implement another PAL for nodejs platform. // eslint-disable-next-line @typescript-eslint/no-var-requires const fs = require('fs'); - const arrayBuffer = fs.readFileSync(binaryUrl); + const arrayBuffer = fs.readFileSync(binaryUrl) as ArrayBuffer; resolve(arrayBuffer); }); return; diff --git a/platforms/minigame/platforms/bytedance/wrapper/builtin/index.js b/platforms/minigame/platforms/bytedance/wrapper/builtin/index.js index f015ccca21..1a5f237c4b 100644 --- a/platforms/minigame/platforms/bytedance/wrapper/builtin/index.js +++ b/platforms/minigame/platforms/bytedance/wrapper/builtin/index.js @@ -1,62 +1,65 @@ -import * as _window from './window' -import document from './document' -import HTMLElement from './HTMLElement' +import * as _window from './window'; +import document from './document'; +import HTMLElement from './HTMLElement'; -const global = GameGlobal +const global = GameGlobal; function inject () { _window.document = document; _window.addEventListener = (type, listener) => { - _window.document.addEventListener(type, listener) - } + _window.document.addEventListener(type, listener); + }; _window.removeEventListener = (type, listener) => { - _window.document.removeEventListener(type, listener) - } + _window.document.removeEventListener(type, listener); + }; _window.dispatchEvent = function (event = {}) { - console.log('window.dispatchEvent' , event.type, event); + console.log('window.dispatchEvent', event.type, event); // nothing to do - } + }; - const { platform } = tt.getSystemInfoSync() + const { platform } = tt.getSystemInfoSync(); // 开发者工具无法重定义 window if (typeof __devtoolssubcontext === 'undefined' && platform === 'devtools') { for (const key in _window) { - const descriptor = Object.getOwnPropertyDescriptor(global, key) + const descriptor = Object.getOwnPropertyDescriptor(global, key); if (!descriptor || descriptor.configurable === true) { Object.defineProperty(window, key, { - value: _window[key] - }) + value: _window[key], + }); } } for (const key in _window.document) { - const descriptor = Object.getOwnPropertyDescriptor(global.document, key) + const descriptor = Object.getOwnPropertyDescriptor(global.document, key); if (!descriptor || descriptor.configurable === true) { Object.defineProperty(global.document, key, { - value: _window.document[key] - }) + value: _window.document[key], + }); } } - window.parent = window + window.parent = window; } else { for (const key in _window) { - global[key] = _window[key] + global[key] = _window[key]; } - global.window = _window - window = global - window.top = window.parent = window + global.window = _window; + window = global; + window.top = window.parent = window; GameGlobal.global = GameGlobal.global || GameGlobal; } } if (!GameGlobal.__isAdapterInjected) { - GameGlobal.__isAdapterInjected = true - inject() + GameGlobal.__isAdapterInjected = true; + inject(); } +//NOTE: Don't override global `WebAssembly` variable since bytedance platform has a bug that will cause TTWebAssembly.instantiate fails. +// So we add our own `CCWebAssembly` variable here. +global.CCWebAssembly = global.TTWebAssembly; require('../../../../common/xmldom/dom-parser'); -require('../unify'); \ No newline at end of file +require('../unify'); diff --git a/platforms/minigame/platforms/wechat/wrapper/builtin/index.js b/platforms/minigame/platforms/wechat/wrapper/builtin/index.js index 98eef6a024..83c8c9f021 100644 --- a/platforms/minigame/platforms/wechat/wrapper/builtin/index.js +++ b/platforms/minigame/platforms/wechat/wrapper/builtin/index.js @@ -58,7 +58,7 @@ if (!GameGlobal.__isAdapterInjected) { } // overwrite global WebAssembly -global.WebAssembly = global.WXWebAssembly; +global.CCWebAssembly = global.WebAssembly = global.WXWebAssembly; require('../../../../common/xmldom/dom-parser'); require('../unify'); diff --git a/templates/wechatprogram/app.js b/templates/wechatprogram/app.js index b5bc1b4052..5b59f4571c 100755 --- a/templates/wechatprogram/app.js +++ b/templates/wechatprogram/app.js @@ -19,6 +19,7 @@ App({ DOMParser: undefined, WebAssembly: undefined, WXWebAssembly: undefined, + CCWebAssembly: undefined, _touchStart: undefined, _touchMove: undefined, _touchEnd: undefined, diff --git a/templates/wechatprogram/miniapp-adapter.js b/templates/wechatprogram/miniapp-adapter.js index 18085bfffa..d5721d773b 100644 --- a/templates/wechatprogram/miniapp-adapter.js +++ b/templates/wechatprogram/miniapp-adapter.js @@ -1,7 +1,7 @@ var GameGlobal = getApp().GameGlobal; exports.adapt = function (canvas, width, height) { GameGlobal.canvas = canvas; - GameGlobal.WXWebAssembly = WXWebAssembly; + GameGlobal.CCWebAssembly = GameGlobal.WXWebAssembly = WXWebAssembly; GameGlobal.requestAnimationFrame = GameGlobal.canvas.requestAnimationFrame || globalThis.setTimeout; GameGlobal.cancelAnimationFrame = GameGlobal.canvas.cancelAnimationFrame || globalThis.setTimeout; GameGlobal.setTimeout = globalThis.setTimeout;