WebAssemblyでScratchプラグインを作ろう!
実装例
constructor(runtime) {
this.runtime = runtime;
this.fib = null;
WebAssembly.instantiateStreaming(fetch("/static/wasm/fib.wasm"), {}).then(
(wasm) => {
this.fib = wasm.instance;
},
);
}
getInfo() {
return {
id: 'fiboka',
name: 'Fib calc',
menuIconURI: fukuokaIcon,
blockIconURI: fukuokaIcon,
blocks: [
{
opcode: 'reportFib',
blockType: BlockType.REPORTER,
text: 'get fib of [N]',
arguments: {
N: {
type: ArgumentType.NUMBER,
defaultValue: 1,
}
}
},
],
menus: {},
};
}
reportFib(args) {
const n = args.N;
const result = this.fib.exports.fib(n);
log.debug(`answer: fib(${n}) = ${result}`);
return result;
}