@gear-js/api
Version:
A JavaScript library that provides functionality to connect GEAR Component APIs.
26 lines (23 loc) • 1.06 kB
JavaScript
import { isHex, hexToU8a, isU8a } from '@polkadot/util';
import importObj from './importObj.js';
function getGrReply(wasm, fn) {
const buffer = isHex(wasm) ? hexToU8a(wasm).buffer : isU8a(wasm) ? wasm.buffer : wasm;
const memory = new WebAssembly.Memory({ initial: 256 });
return new Promise((resolve, reject) => {
WebAssembly.instantiate(buffer, importObj(memory, false, undefined, undefined, undefined, (payload, len) => resolve(new Uint8Array(memory.buffer.slice(payload, payload + len)))))
// @ts-expect-error: Invalid typings here. instance is actually a prop of WebAssembly.Instance
.then(({ instance: { exports } }) => {
if (!(fn in exports)) {
reject(`${fn} function not found in exports`);
}
else if (typeof exports[fn] !== 'function') {
reject(`${fn} is not a function`);
}
else {
exports[fn]();
}
})
.catch((error) => reject(error));
});
}
export { getGrReply };