-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(loadmodule): fall back to asm.js if wasm load fails
- Loading branch information
Showing
1 changed file
with
29 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |