3dgen's recent timeline updates
3dgen

3dgen

V2EX member #341644, joined on 2018-08-16 11:52:55 +08:00
3dgen's recent replies
看楼主的写法,是希望递归调用来计算斐波拉契序列的 n 项值,但问题是:Fibonacci 这个函数应该调用自身,但是你在 Fibonacci 里面调用的是函数 f,而不是自身,这根本就不是递归调用的写法啊。正确的写法是这样:

export function f(x:i32):i32{
if(x === 1 || x === 2) {
return 1
}
return f(x - 1) + f(x - 2)
}

当然你把 f 全部换成 Fibonacci 效果一样,JS 部分亲测可用:

function fetchAndInstantiate(url, importObject) {
return fetch(url).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results =>
results.instance
);
}

fetchAndInstantiate('f.wasm').then(
function(instance) {
console.log(instance.exports.f(10));
}
);

控制台输出 55。

另外,楼上的链接给错了,我帮他改改,应该是这个:
https://github.com/chai2010/awesome-wasm-zh
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   948 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 21:25 · PVG 05:25 · LAX 14:25 · JFK 17:25
♥ Do have faith in what you're doing.