Files
esengine/packages/platform-common/src/wasm/IWasmLibraryLoader.ts

241 lines
5.1 KiB
TypeScript
Raw Normal View History

/**
* WASM
*
* WASM
*
*
* - Web WebAssembly API
* - WXWebAssembly
* -
* -
* -
*/
/**
*
*/
export enum PlatformType {
/** Web 浏览器 */
Web = 'web',
/** 微信小游戏 */
WeChatMiniGame = 'wechat-minigame',
/** 字节跳动小游戏 */
ByteDanceMiniGame = 'bytedance-minigame',
/** 支付宝小游戏 */
AlipayMiniGame = 'alipay-minigame',
/** 百度小游戏 */
BaiduMiniGame = 'baidu-minigame',
/** Node.js */
NodeJS = 'nodejs',
/** 未知平台 */
Unknown = 'unknown'
}
/**
* WASM
*
* @example
* ```typescript
* const config: WasmLibraryConfig = {
* name: 'Rapier2D',
* web: {
* useCompat: true, // Web 使用 compat 版本
* },
* minigame: {
* wasmPath: 'wasm/rapier2d_bg.wasm',
* needsTextDecoderPolyfill: true,
* }
* };
* ```
*/
export interface WasmLibraryConfig {
/**
*
*/
name: string;
/**
* Web
*/
web?: {
/**
* 使 -compat WASM base64 JS
*
*
*
*/
useCompat?: boolean;
/**
* compat 使
*/
modulePath?: string;
/**
* WASM compat 使
*/
wasmPath?: string;
};
/**
*
*/
minigame?: {
/**
* WASM
*/
wasmPath: string;
/**
* JS glue
*/
gluePath?: string;
/**
* TextDecoder polyfill
*
* iOS polyfill
*/
needsTextDecoderPolyfill?: boolean;
/**
* TextEncoder polyfill
*
* iOS polyfill
*/
needsTextEncoderPolyfill?: boolean;
};
/**
*
*
*
*
* @param wasmInstance - WASM
* @returns
*/
customInit?: (wasmInstance: any) => Promise<any>;
}
/**
*
* Platform information
*/
export interface PlatformInfo {
/** 平台类型 | Platform type */
type: PlatformType;
/** 是否支持 WebAssembly | Supports WebAssembly */
supportsWasm: boolean;
/** 是否支持 SharedArrayBuffer | Supports SharedArrayBuffer */
supportsSharedArrayBuffer: boolean;
/** 需要安装的 polyfills 列表 | Required polyfills */
needsPolyfills: string[];
/**
* Tauri
* Whether running in editor environment (Tauri desktop app)
*/
isEditor: boolean;
}
/**
* WASM
*
* WASM
*
* @typeParam T - WASM
*
* @example
* ```typescript
* class Rapier2DLoader implements IWasmLibraryLoader<typeof RAPIER> {
* async load(): Promise<typeof RAPIER> {
* const RAPIER = await import('@dimforge/rapier2d-compat');
* await RAPIER.init();
* return RAPIER;
* }
*
* isSupported(): boolean {
* return typeof WebAssembly !== 'undefined';
* }
*
* getPlatformInfo(): PlatformInfo {
* return {
* type: PlatformType.Web,
* supportsWasm: true,
* supportsSharedArrayBuffer: true,
* needsPolyfills: []
* };
* }
* }
* ```
*/
export interface IWasmLibraryLoader<T> {
/**
* WASM
*
* @returns
* @throws
*/
load(): Promise<T>;
/**
*
*
* @returns
*/
isSupported(): boolean;
/**
*
*
* @returns
*/
getPlatformInfo(): PlatformInfo;
/**
*
*
* @returns
*/
getConfig(): WasmLibraryConfig;
}
/**
* WASM
*
* WASM
*/
export interface IPlatformWasmLoader {
/**
*
*/
readonly platformType: PlatformType;
/**
* WASM
*
* @param wasmPath - WASM
* @param imports - WASM
* @returns WASM
*/
loadWasmModule(
wasmPath: string,
imports?: WebAssembly.Imports
): Promise<WebAssembly.Instance>;
/**
* WASM
*
* @returns
*/
isSupported(): boolean;
/**
* polyfills
*/
installPolyfills(): void;
}