k***@163.com
k***@163.com
  • 发布:2025-10-10 18:15
  • 更新:2025-10-10 18:16
  • 阅读:15

APP环境中如何使用wasm

分类:uni-app

使用wasm-pack build --target web生成的pkg目录,引入对应的js文件初始化wasm时,很多api是没有的,需要怎么修改对应的代码呢

let wasm;

/**

  • @param {number} a
  • @param {number} b
  • @returns {number}
    */
    export function greet(a, b) {
    const ret = wasm.greet(a, b);
    return ret;
    }

const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);

    if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {  
      console.warn(  
        '`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n',  
        e,  
      );  
    } else {  
      throw e;  
    }  
  }  
}  

const bytes = await module.arrayBuffer();  
return await WebAssembly.instantiate(bytes, imports);  

} else {
const instance = await WebAssembly.instantiate(module, imports);

if (instance instanceof WebAssembly.Instance) {  
  return { instance, module };  
} else {  
  return instance;  
}  

}
}

function wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.
wbindgen_init_externref_table = function () {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
};

return imports;
}

function __wbg_init_memory(imports, memory) {}

function wbg_finalize_init(instance, module) {
wasm = instance.exports;
wbg_init.__wbindgen_wasm_module = module;

wasm.__wbindgen_start();
return wasm;
}

function initSync(module) {
if (wasm !== undefined) return wasm;

if (typeof module !== 'undefined') {
if (Object.getPrototypeOf(module) === Object.prototype) {
({ module } = module);
} else {
console.warn('using deprecated parameters for initSync(); pass a single object instead');
}
}

const imports = __wbg_get_imports();

__wbg_init_memory(imports);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}

const instance = new WebAssembly.Instance(module, imports);

return __wbg_finalize_init(instance, module);
}

async function __wbg_init(module_or_path) {
if (wasm !== undefined) return wasm;

if (typeof module_or_path !== 'undefined') {
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
({ module_or_path } = module_or_path);
} else {
console.warn(
'using deprecated parameters for the initialization function; pass a single object instead',
);
}
}

if (typeof module_or_path === 'undefined') {
module_or_path = new URL('img_compress_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();

if (
typeof module_or_path === 'string' ||
(typeof Request === 'function' && module_or_path instanceof Request) ||
(typeof URL === 'function' && module_or_path instanceof URL)
) {
module_or_path = fetch(module_or_path);
}

__wbg_init_memory(imports);

const { instance, module } = await __wbg_load(await module_or_path, imports);

return __wbg_finalize_init(instance, module);
}

export { initSync };
export default __wbg_init;

2025-10-10 18:15 负责人:无 分享
已邀请:
k***@163.com

k***@163.com (作者)

h5环境是没问题的,就是APP很多都是没有的,URL、fetch这些,导致初始化直接报错了

要回复问题请先登录注册