Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(interoputility): export interop methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 12, 2018
1 parent ad30a11 commit ded9036
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
38 changes: 12 additions & 26 deletions src/interop/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { mountDirectory, unmount } from 'emscripten-wasm-loader';
import * as nanoid from 'nanoid';
import { SassAsmModule } from '../SassAsmModule';
import { log } from '../util/logger';
import { SassFileContext, SassFileContextInterface } from './file/sassFileContext';
import { buildInteropUtility } from './interopUtility';
import { SassOptions, SassOptionsInterface } from './options/sassOptions';
import { wrapSassOptions } from './options/wrapSassOptions';
import { wrapSassContext } from './wrapSassContext';
Expand All @@ -12,31 +10,19 @@ import { wrapSassContext } from './wrapSassContext';
*
* @param asmModule
*/
const buildContext = (asmModule: SassAsmModule) => {
const { cwrap, FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;
const cwrapCtx = wrapSassContext(cwrap);
const cwrapOptions = wrapSassOptions(cwrap);

const str = {
alloc: (value: string) => {
const len = (value.length << 2) + 1;
const ret = stackAlloc(len);
stringToUTF8(value, ret, len);
return ret;
},
ptrToString: Pointer_stringify
};

const nodePathId = `/${nanoid(45)}`;
FS.mkdir(nodePathId);
log(`buildContext: root mounting point created`, { nodePathId });

const mountPath = mountDirectory(FS, nodePathId);
const unmountPath = unmount(FS, nodePathId);
const buildContext = (
cwrapContext: ReturnType<typeof wrapSassContext>,
cwrapOptions: ReturnType<typeof wrapSassOptions>,
interop: ReturnType<typeof buildInteropUtility>
) => {
const { str, mount, unmount } = interop;

return {
options: {
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, str) as SassOptionsInterface
create: () => new SassOptions(cwrapContext, cwrapOptions, mount, unmount, str) as SassOptionsInterface
},
file: {
create: () => new SassFileContext() as SassFileContextInterface
}
};
};
Expand Down
37 changes: 37 additions & 0 deletions src/interop/interopUtility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { mountDirectory, unmount } from 'emscripten-wasm-loader';
import * as nanoid from 'nanoid';
import { SassAsmModule } from '../SassAsmModule';
import { log } from '../util/logger';

/**
* Construct few utility function to be used interop between raw libsass interface.
*
* @param {SassAsmModule} asmModule
*/
const buildInteropUtility = (asmModule: SassAsmModule) => {
const { FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;

const str = {
alloc: (value: string) => {
const len = (value.length << 2) + 1;
const ret = stackAlloc(len);
stringToUTF8(value, ret, len);
return ret;
},
ptrToString: Pointer_stringify
};

const nodePathId = `/${nanoid(45)}`;
FS.mkdir(nodePathId);
log(`sassLoader: root mounting point created`, { nodePathId });

const mountPath = mountDirectory(FS, nodePathId);
const unmountPath = unmount(FS, nodePathId);
return {
str,
mount: mountPath,
unmount: unmountPath
};
};

export { buildInteropUtility };
15 changes: 14 additions & 1 deletion src/sassLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { buildContext } from './interop/context';
import { buildInteropUtility } from './interop/interopUtility';
import { getVersion } from './interop/miscellaneous';
import { wrapSassOptions } from './interop/options/wrapSassOptions';
import { wrapSassContext } from './interop/wrapSassContext';
import { SassAsmModule } from './SassAsmModule';
import { SassFactory } from './SassFactory';

Expand All @@ -10,8 +13,18 @@ import { SassFactory } from './SassFactory';
* @returns {SassFactory}
*/
export const sassLoader = (asmModule: SassAsmModule): SassFactory => {
const { cwrap } = asmModule;
const cwrapCtx = wrapSassContext(cwrap);
const cwrapOptions = wrapSassOptions(cwrap);
const interop = buildInteropUtility(asmModule);

return {
getVersion: getVersion(asmModule),
context: buildContext(asmModule)
context: buildContext(cwrapCtx, cwrapOptions, interop),
interop,
raw: {
context: cwrapCtx,
options: cwrapOptions
}
};
};

0 comments on commit ded9036

Please sign in to comment.