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

Commit e668994

Browse files
committed
feat(sassfilecontext): implement initial interface
1 parent 90eba25 commit e668994

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

src/interop/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const buildContext = (
2222
create: () => new SassOptions(cwrapContext, cwrapOptions, mount, unmount, str) as SassOptionsInterface
2323
},
2424
file: {
25-
create: () => new SassFileContext() as SassFileContextInterface
25+
create: (inputPath: string) => new SassFileContext(inputPath, cwrapContext, str) as SassFileContextInterface
2626
}
2727
};
2828
};

src/interop/file/sassFileContext.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
1-
interface SassFileContextInterface {}
1+
import { log } from '../../util/logger';
2+
import { SassOptions, SassOptionsInterface } from '../options/sassOptions';
3+
import { wrapSassContext } from '../wrapSassContext';
24

3-
class SassFileContext implements SassFileContextInterface {}
5+
interface SassFileContextInterface {
6+
/**
7+
* Property accessor to `file_context_(get|set)_options`
8+
*/
9+
options: SassOptionsInterface | null;
10+
11+
/**
12+
* Accessor to file_context_get_context
13+
*/
14+
getContext(): SassContextInterface;
15+
16+
compile(): void;
17+
18+
/**
19+
* Release allocated memory with created instance.
20+
* Accessor to sass_delete_file_context
21+
*/
22+
dispose(): void;
23+
}
24+
25+
/**
26+
* @internal
27+
*/
28+
class SassFileContext implements SassFileContextInterface {
29+
private readonly sassFileContextPtr: number;
30+
private sassOptions: SassOptionsInterface | null;
31+
32+
constructor(
33+
inputPath: string,
34+
private readonly cwrapCtx: ReturnType<typeof wrapSassContext>,
35+
private readonly strMethod: {
36+
alloc: (value: string) => number;
37+
ptrToString: (value: number) => string;
38+
}
39+
) {
40+
const inputPathPtr = this.strMethod.alloc(inputPath);
41+
this.sassFileContextPtr = this.cwrapCtx.make_file_context(inputPathPtr);
42+
log(`SassFileContext: created new instance`, { sassFileContextPtr: this.sassFileContextPtr });
43+
}
44+
45+
//Instead of reconstructing js object from raw pointer,
46+
//returns reference of option instance already created.
47+
public get options(): SassOptionsInterface | null {
48+
const sassOptionPtr = this.cwrapCtx.file_context_get_options(this.sassFileContextPtr);
49+
50+
//internal access to raw pointer - interface doesn't expose it.
51+
if (!!this.sassOptions && (this.sassOptions as SassOptions).sassOptionsPtr === sassOptionPtr) {
52+
return this.sassOptions;
53+
}
54+
throw new Error(`Cannot get option`);
55+
}
56+
57+
public set options(option: SassOptionsInterface | null) {
58+
if (!option) {
59+
throw new Error('Cannot set empty options');
60+
}
61+
this.sassOptions = null;
62+
this.sassOptions = option;
63+
64+
//internal access to raw pointer - interface doesn't expose it.
65+
this.cwrapCtx.file_context_set_options(this.sassFileContextPtr, (this.sassOptions as SassOptions).sassOptionsPtr);
66+
}
67+
68+
public getContext(): SassContextInterface {
69+
this.cwrapCtx.file_context_get_context(this.sassFileContextPtr);
70+
71+
throw new Error('m');
72+
}
73+
74+
public compile(): void {
75+
throw new Error('meh');
76+
}
77+
78+
public dispose(): void {
79+
this.cwrapCtx.delete_file_context(this.sassFileContextPtr);
80+
}
81+
}
482

583
export { SassFileContext, SassFileContextInterface };

0 commit comments

Comments
 (0)