Skip to content

Commit

Permalink
feat(loadmodule): fall back to asm.js if wasm load fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Sep 19, 2017
1 parent d7bfa68 commit aef1307
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/loadModule.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import { ENVIRONMENT, getModuleLoader, isNode, isWasmEnabled } from 'emscripten-wasm-loader';
import { CldAsmModule } from './cldAsmModule';
import { ENVIRONMENT, isWasmEnabled } from 'emscripten-wasm-loader';
import { CldFactory } from './cldFactory';
import { cldLoader } from './cldLoader';
import { getLoader } from './getLoader';
import { log } from './util/logger';

const asmPath = `./lib/${isWasmEnabled() ? 'wasm' : 'asm'}`;
log(`loadModule: load cld3 module loader from `, asmPath);
/**
* Load, initialize wasm / asm.js binary to use actual cld wasm instances.
*
* @param {binaryEndpoint} [string] For overring path to wasm binary on node.js or browser.
* @param {environment} [ENVIRONMENT] For overriding running environment
*
* @returns {Promise<CldFactory>} Factory function of cld to allow create instance of cld.
*/
const loadModule: (binaryEndpoint?: string, environment?: ENVIRONMENT) => Promise<CldFactory> = async (
binaryEndpoint?: string,
environment?: ENVIRONMENT
) => {
const binaryPath = `./lib/${isWasmEnabled() ? 'wasm' : 'asm'}`;

//imports MODULARIZED emscripten preamble
//tslint:disable-next-line:no-require-imports no-var-requires
const runtimeModule = require(`${asmPath}/cld3`);
try {
return await getLoader(binaryPath, binaryEndpoint, environment);
} catch (e) {
log(`loadModule: cannot load module from `, binaryPath);

export const loadModule: (binaryEndpoint?: string, environment?: ENVIRONMENT) => Promise<CldFactory> = getModuleLoader<
CldFactory,
CldAsmModule
>((runtime: CldAsmModule) => cldLoader(runtime), {
//tslint:disable-next-line:no-require-imports
dir: isNode() ? require('path').dirname(require.resolve(`${asmPath}/cld3`)) : null,
runtimeModule
});
if (!isWasmEnabled()) {
throw e;
} else {
log(`loadModule: try to fallback to asm.js runtime`);
return await getLoader(`./lib/asm`, binaryEndpoint, environment);
}
}
};

export { loadModule };

0 comments on commit aef1307

Please sign in to comment.