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

Commit ded9036

Browse files
committed
feat(interoputility): export interop methods
1 parent ad30a11 commit ded9036

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

src/interop/context.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { mountDirectory, unmount } from 'emscripten-wasm-loader';
2-
import * as nanoid from 'nanoid';
3-
import { SassAsmModule } from '../SassAsmModule';
4-
import { log } from '../util/logger';
1+
import { SassFileContext, SassFileContextInterface } from './file/sassFileContext';
2+
import { buildInteropUtility } from './interopUtility';
53
import { SassOptions, SassOptionsInterface } from './options/sassOptions';
64
import { wrapSassOptions } from './options/wrapSassOptions';
75
import { wrapSassContext } from './wrapSassContext';
@@ -12,31 +10,19 @@ import { wrapSassContext } from './wrapSassContext';
1210
*
1311
* @param asmModule
1412
*/
15-
const buildContext = (asmModule: SassAsmModule) => {
16-
const { cwrap, FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;
17-
const cwrapCtx = wrapSassContext(cwrap);
18-
const cwrapOptions = wrapSassOptions(cwrap);
19-
20-
const str = {
21-
alloc: (value: string) => {
22-
const len = (value.length << 2) + 1;
23-
const ret = stackAlloc(len);
24-
stringToUTF8(value, ret, len);
25-
return ret;
26-
},
27-
ptrToString: Pointer_stringify
28-
};
29-
30-
const nodePathId = `/${nanoid(45)}`;
31-
FS.mkdir(nodePathId);
32-
log(`buildContext: root mounting point created`, { nodePathId });
33-
34-
const mountPath = mountDirectory(FS, nodePathId);
35-
const unmountPath = unmount(FS, nodePathId);
13+
const buildContext = (
14+
cwrapContext: ReturnType<typeof wrapSassContext>,
15+
cwrapOptions: ReturnType<typeof wrapSassOptions>,
16+
interop: ReturnType<typeof buildInteropUtility>
17+
) => {
18+
const { str, mount, unmount } = interop;
3619

3720
return {
3821
options: {
39-
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, str) as SassOptionsInterface
22+
create: () => new SassOptions(cwrapContext, cwrapOptions, mount, unmount, str) as SassOptionsInterface
23+
},
24+
file: {
25+
create: () => new SassFileContext() as SassFileContextInterface
4026
}
4127
};
4228
};

src/interop/interopUtility.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { mountDirectory, unmount } from 'emscripten-wasm-loader';
2+
import * as nanoid from 'nanoid';
3+
import { SassAsmModule } from '../SassAsmModule';
4+
import { log } from '../util/logger';
5+
6+
/**
7+
* Construct few utility function to be used interop between raw libsass interface.
8+
*
9+
* @param {SassAsmModule} asmModule
10+
*/
11+
const buildInteropUtility = (asmModule: SassAsmModule) => {
12+
const { FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;
13+
14+
const str = {
15+
alloc: (value: string) => {
16+
const len = (value.length << 2) + 1;
17+
const ret = stackAlloc(len);
18+
stringToUTF8(value, ret, len);
19+
return ret;
20+
},
21+
ptrToString: Pointer_stringify
22+
};
23+
24+
const nodePathId = `/${nanoid(45)}`;
25+
FS.mkdir(nodePathId);
26+
log(`sassLoader: root mounting point created`, { nodePathId });
27+
28+
const mountPath = mountDirectory(FS, nodePathId);
29+
const unmountPath = unmount(FS, nodePathId);
30+
return {
31+
str,
32+
mount: mountPath,
33+
unmount: unmountPath
34+
};
35+
};
36+
37+
export { buildInteropUtility };

src/sassLoader.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { buildContext } from './interop/context';
2+
import { buildInteropUtility } from './interop/interopUtility';
23
import { getVersion } from './interop/miscellaneous';
4+
import { wrapSassOptions } from './interop/options/wrapSassOptions';
5+
import { wrapSassContext } from './interop/wrapSassContext';
36
import { SassAsmModule } from './SassAsmModule';
47
import { SassFactory } from './SassFactory';
58

@@ -10,8 +13,18 @@ import { SassFactory } from './SassFactory';
1013
* @returns {SassFactory}
1114
*/
1215
export const sassLoader = (asmModule: SassAsmModule): SassFactory => {
16+
const { cwrap } = asmModule;
17+
const cwrapCtx = wrapSassContext(cwrap);
18+
const cwrapOptions = wrapSassOptions(cwrap);
19+
const interop = buildInteropUtility(asmModule);
20+
1321
return {
1422
getVersion: getVersion(asmModule),
15-
context: buildContext(asmModule)
23+
context: buildContext(cwrapCtx, cwrapOptions, interop),
24+
interop,
25+
raw: {
26+
context: cwrapCtx,
27+
options: cwrapOptions
28+
}
1629
};
1730
};

0 commit comments

Comments
 (0)