vervewebasmbly使⽤TS开发教程关于使⽤typescript编写 webasmbly时的踩坑记录
⾸先根据官⽹的教程⼀步步创建代码,
创建⼀个Main.ts⽂件,代码如下:
declare function sayHello():void;
sayHello();
export function add(x: i32, y: i32): i32 {
return x + y;
}
JS调⽤代码:
WebAsmbly.instantiateStreaming(fetch("../out/main.wasm"),{
main:{
sayHello(){
weekendsconsole.log("Hello from WebAsmbly!");
}
},
var mem =new WebAsmbly.Memory({ initial:16*16});
//env这段代码⼀定需要,不然在调⽤WebAsmbly.instantiate(bytes, imports)的时候会报错,说没有abort的函数env:{王若琳好听的歌
//如果⽂件超过4KB,需要设置memory的话,这样设置invitation是什么意思
绯闻女孩主题曲
memory: mem,//如果没有超过,这句代码可以不需要
free walk>londonbridge>effort是什么意思abort(_msg, _file, line, column){
<("abort called at main.ts:"+ line +":"+ column);
}
gooole
},
}).then(result =>{
const exports = ports;
爱情无限谱
}).);
如果是Laya⾥⾯调⽤,就先⽤⼆进制格式load好wsam⽂件,然后⽤这个⽅法调⽤
WebAsmbly.instantiate(bytes, imports).then(results => {
console.log(results);
});