|
1 |
| -interface SassFileContextInterface {} |
| 1 | +import { log } from '../../util/logger'; |
| 2 | +import { SassOptions, SassOptionsInterface } from '../options/sassOptions'; |
| 3 | +import { wrapSassContext } from '../wrapSassContext'; |
2 | 4 |
|
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 | +} |
4 | 82 |
|
5 | 83 | export { SassFileContext, SassFileContextInterface };
|
0 commit comments