This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sassdatacontext): implement sassdatacontext
- Loading branch information
Showing
2 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { log } from '../../util/logger'; | ||
import { StringMethodInterface } from '../interopUtility'; | ||
import { SassOptions, SassOptionsInterface } from '../options/sassOptions'; | ||
import { SassContext, SassContextInterface } from '../sassContext'; | ||
import { SassSourceContext } from '../SassSourceContext'; | ||
import { wrapSassContext } from '../wrapSassContext'; | ||
|
||
class SassDataContext implements SassSourceContext { | ||
private readonly sassDataContextPtr: number; | ||
private sassOptions: SassOptionsInterface | null; | ||
private sassContext: SassContextInterface | null; | ||
|
||
/** | ||
* | ||
*/ | ||
constructor( | ||
input: string, | ||
private readonly cwrapCtx: ReturnType<typeof wrapSassContext>, | ||
private readonly strMethod: StringMethodInterface | ||
) { | ||
const inputPtr = this.strMethod.alloc(input); | ||
this.sassDataContextPtr = this.cwrapCtx.make_data_context(inputPtr); | ||
log(`SassDataContext: created new instance`, { sassDataContextPtr: this.sassDataContextPtr }); | ||
} | ||
|
||
//Instead of reconstructing js object from raw pointer, | ||
//returns reference of option instance already created. | ||
public get options(): SassOptionsInterface | null { | ||
const sassOptionPtr = this.cwrapCtx.data_context_get_options(this.sassDataContextPtr); | ||
|
||
//internal access to raw pointer - interface doesn't expose it. | ||
if (!!this.sassOptions && (this.sassOptions as SassOptions).sassOptionsPtr === sassOptionPtr) { | ||
return this.sassOptions; | ||
} | ||
throw new Error(`Cannot get option`); | ||
} | ||
|
||
public set options(option: SassOptionsInterface | null) { | ||
if (!option) { | ||
throw new Error('Cannot set empty options'); | ||
} | ||
this.sassOptions = null; | ||
this.sassOptions = option; | ||
|
||
//internal access to raw pointer - interface doesn't expose it. | ||
this.cwrapCtx.data_context_set_options(this.sassDataContextPtr, (this.sassOptions as SassOptions).sassOptionsPtr); | ||
} | ||
|
||
public getContext(): SassContextInterface { | ||
const sassContextPtr = this.cwrapCtx.data_context_get_context(this.sassDataContextPtr); | ||
if (!!this.sassContext && (this.sassContext as SassContext).sassContextPtr !== sassContextPtr) { | ||
throw new Error(`Unexpected: context has changed`); | ||
} | ||
|
||
return !!this.sassContext | ||
? this.sassContext | ||
: (this.sassContext = new SassContext(sassContextPtr, this.cwrapCtx, this.strMethod) as SassContextInterface); | ||
} | ||
|
||
public compile(): number { | ||
return this.cwrapCtx.compile_data_context(this.sassDataContextPtr); | ||
} | ||
|
||
public dispose(): void { | ||
this.cwrapCtx.delete_data_context(this.sassDataContextPtr); | ||
} | ||
} | ||
|
||
export { SassDataContext }; |