null on WASM non-available (#317)

This commit is contained in:
Jaco Greeff
2019-02-16 12:09:07 +01:00
committed by GitHub
parent d045ba2326
commit b15abe1167
3 changed files with 38 additions and 5 deletions
@@ -0,0 +1,25 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('ready', () => {
let globalWA: typeof WebAssembly;
beforeEach(() => {
globalWA = (global as any).WebAssembly;
(global as any).WebAssembly = null;
});
afterEach(() => {
(global as any).WebAssembly = globalWA;
});
it('returns false when Webassembly is not available', async () => {
(global as any).WebAssembly = null;
const schnorrkelWaitReady = require('./ready').default;
const isReady = await schnorrkelWaitReady();
expect(isReady).toBe(false);
});
});
@@ -255,9 +255,9 @@ wasmImports.__wbindgen_jsval_eq = function(a, b) {
const createPromise = require('./schnorrkel_js_bg');
const wasmPromise = createPromise(wasmImports);
const wasmPromise = createPromise(wasmImports).catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm }).catch(() => {});
wasmPromise.then((_wasm) => { wasm = _wasm });
@@ -6,7 +6,15 @@ module.exports = async function createExportPromise (wasmImports) {
'./schnorrkel_js': wasmImports
};
return WebAssembly
.instantiate(bytes, imports)
.then((wasm) => wasm.instance.exports);
if (!WebAssembly) {
return null;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
return null;
}
}