问题遇到的现象和发生背景
显示还是类型错误,js调用wasm
问题相关代码,请勿粘贴截图
MIME类型已设置
function importArrayToBuffer(memory,array,offset) {
const importBuffer = new Uint32Array(memory.buffer,offset,array.length);
for(let i=0;i<array.length;i++){
importBuffer[i] = array[i];
}
}
let startTime = performance.now();
WebAssembly.instantiateStreaming(fetch('http://192.168.0.116:8077//hello_world.wasm'),{
env:{
print (offset,len){
let strBuffer = new Uint32Array(memory.buffer,offset,len);
console.log(strBuffer);
document.querySelector('.sequence-after').innerText=JSON.stringify(Object.values(strBuffer));
}
}
}).then(resultObject =>{
console.log(performance.now()-startTime);
//WebAssembly.Module
console.log(resultObject.module);
//WebAssembly.Instance
console.log(resultObject.instance);
//总和
console.log(resultObject);
let exports = resultObject.instance.exports;
memory=exports.memory;
let arr =[];
for(let i=0;i<10;i++){
arr.push(Math.round(Math.random()*10));
}
document.querySelector('.sequence-before').innerText=JSON.stringify(arr);
importArrayToBuffer(memory,arr,exports.getArrayOffset());
//调用Wasm模块暴露函数
exports.testhello(1);
});
运行结果及报错内容
Uncaught (in promise) TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
我想要达到的结果
运行成功并成功调用wasm文件