Skip to content

Commit

Permalink
feat(loadmodule): no longer accepts environment
Browse files Browse the repository at this point in the history
BREAKING CHANGES: do not support environment override
  • Loading branch information
kwonoj committed Jun 13, 2019
1 parent a4cae0a commit 4a6ecdc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '*/lib/node/cld3';
9 changes: 4 additions & 5 deletions src/cldLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ENVIRONMENT } from 'emscripten-wasm-loader';
import { CldAsmModule, LanguageResult } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { LanguageCode } from './languageCode';
Expand All @@ -17,8 +16,8 @@ const PTR_SIZE = 4;
*
* @returns {CldFactory} Factory function manages lifecycle of cld3 language identifier.
*/
export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT): CldFactory => {
const { cwrap, _free, allocateUTF8, _malloc, getValue, Pointer_stringify, setValue } = asmModule;
export const cldLoader = (asmModule: CldAsmModule): CldFactory => {
const { cwrap, _free, allocateUTF8, _malloc, getValue, UTF8ToString, setValue } = asmModule;
const cldInterface = wrapCldInterface(cwrap);

/**
Expand All @@ -35,7 +34,7 @@ export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT):
};

// grab constant values from cld3 library
const unknownIdentifier = Pointer_stringify(cldInterface.getUnknownIdentifier());
const unknownIdentifier = UTF8ToString(cldInterface.getUnknownIdentifier());
const minBytesDefault = cldInterface.getMinNumBytesDefault();
const maxBytesDefault = cldInterface.getMaxNumBytesDefault();
const maxBytesInput = cldInterface.getMaxNumBytesInput();
Expand Down Expand Up @@ -66,7 +65,7 @@ export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT):

// be careful to match order of properties to match pointer to struct field.
const ret: LanguageResult = {
language: Pointer_stringify(languageStringPtr) as LanguageCode,
language: UTF8ToString(languageStringPtr) as LanguageCode,
probability: getValue(structPtr + PTR_SIZE * 1, 'float'),
is_reliable: !!getValue(structPtr + PTR_SIZE * 2, 'i8'),
proportion: getValue(structPtr + PTR_SIZE * 3, 'float')
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export { log, enableLogger } from './util/logger';
export { LanguageIdentifier, CldFactory } from './cldFactory';
export { LanguageResult } from './cldAsmModule';
export { LanguageCode } from './languageCode';
export { ENVIRONMENT } from 'emscripten-wasm-loader';
23 changes: 7 additions & 16 deletions src/loadModule.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
import { ENVIRONMENT, getModuleLoader, isNode } from 'emscripten-wasm-loader';
import { getModuleLoader } from 'emscripten-wasm-loader';
import { CldAsmModule } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { cldLoader } from './cldLoader';
import { log } from './util/logger';

//imports MODULARIZED emscripten preamble
import * as runtime from './lib/node/cld3';

/**
* Load, initialize wasm binary to use actual cld wasm instances.
*
* @param [InitOptions] Options to initialize cld3 wasm binary.
* @param {number} [InitOptions.timeout] - timeout to wait wasm binary compilation & load.
* @param {string | object} [InitOptions.locateBinary] - custom resolution logic for wasm binary. (not supported)
* @param {ENVIRONMENT} [InitOptions.environment] For overriding running environment
* It could be either remote endpoint url, or loader-returned object for bundler. Check examples/browser_* for references.
*
* @returns {() => Promise<CldFactory>} Function to load module
*/
const loadModule = async (
initOptions: Partial<{
timeout: number;
environment?: ENVIRONMENT;
}> = {}
) => {
//imports MODULARIZED emscripten preamble
//tslint:disable-next-line:no-require-imports no-var-requires
const runtime = require(`./lib/cld3`);

const { environment, timeout } = initOptions;
const env = environment ? environment : isNode() ? ENVIRONMENT.NODE : ENVIRONMENT.WEB;
const { timeout } = initOptions;

log(`loadModule: loading cld3 wasm binary`, { initOptions });

//https://github.com/kwonoj/docker-hunspell-wasm/issues/63
//Build module object to construct wasm binary module via emscripten preamble.
//apply overridden environment values to custom patched hunspell preamble.
const overriddenModule = { ENVIRONMENT: env };

const moduleLoader = await getModuleLoader<CldFactory, CldAsmModule>(
(runtime: CldAsmModule) => cldLoader(runtime, env),
(runtime: CldAsmModule) => cldLoader(runtime),
runtime,
overriddenModule,
undefined,
{ timeout }
);

Expand Down

0 comments on commit 4a6ecdc

Please sign in to comment.