Skip to content

Commit

Permalink
feat(loadmodule): support single file binary load
Browse files Browse the repository at this point in the history
BREAKING CHANGE: now runs on native-wasm supported runtime only
  • Loading branch information
kwonoj committed Nov 29, 2017
1 parent cc98f9e commit d7ab905
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 58 deletions.
5 changes: 5 additions & 0 deletions src/cldLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { CldAsmModule, ResultVector } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { LanguageCode } from './languageCode';

/**
* @internal
* Flatten vector object returned by cld3 into array.
*
*/
const munge_vector = (vector: ResultVector) => {
const size = vector.size();
const ret = [];
Expand Down
38 changes: 0 additions & 38 deletions src/getLoader.ts

This file was deleted.

34 changes: 14 additions & 20 deletions src/loadModule.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { ENVIRONMENT, isWasmEnabled } from 'emscripten-wasm-loader';
import { ENVIRONMENT, getModuleLoader } from 'emscripten-wasm-loader';
import { CldAsmModule } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { getLoader } from './getLoader';
import { cldLoader } from './cldLoader';
import { log } from './util/logger';

/**
* 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.
* @returns {(environment?: ENVIRONMENT) => Promise<CldFactory>} Function to load module
*/
const loadModule: (binaryEndpoint?: string, environment?: ENVIRONMENT) => Promise<CldFactory> = async (
binaryEndpoint?: string,
environment?: ENVIRONMENT
) => {
const binaryPath = `./lib/${isWasmEnabled() ? 'wasm' : 'asm'}`;
const loadModule = async (environment?: ENVIRONMENT) => {
log(`loadModule: loading cld3 module`);

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

if (!isWasmEnabled()) {
throw e;
} else {
log(`loadModule: try to fallback to asm.js runtime`);
return await getLoader(`./lib/asm`, binaryEndpoint, environment);
}
}
const moduleLoader = await getModuleLoader<CldFactory, CldAsmModule>(
(runtime: CldAsmModule, env: ENVIRONMENT) => cldLoader(runtime, env),
runtimeModule
);

return moduleLoader(environment);
};

export { loadModule };

0 comments on commit d7ab905

Please sign in to comment.