|
| 1 | +import { log } from '../../util/logger'; |
| 2 | +import { StringMethodInterface } from '../interopUtility'; |
| 3 | +import { wrapSassImporter } from './wrapSassImporter'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Interop interface to `Sass_Import_Entry` |
| 7 | + * |
| 8 | + * This interface doesn't implements ownership api |
| 9 | + * sass_import_take_source / sass_import_take_srcmap |
| 10 | + */ |
| 11 | +interface SassImportEntryInterface { |
| 12 | + /** |
| 13 | + * Property accessor to `sass_import_set_error` and `sass_import_get_error_(line|column|message)` |
| 14 | + */ |
| 15 | + error: { message: string; line: number; column: number }; |
| 16 | + /** |
| 17 | + * Property accessor to `sass_import_get_imp_path` |
| 18 | + */ |
| 19 | + readonly importPath: string; |
| 20 | + /** |
| 21 | + * Property accessor to `sass_import_get_abs_path` |
| 22 | + */ |
| 23 | + readonly absPath: string; |
| 24 | + /** |
| 25 | + * Property accessor to `sass_import_get_source` |
| 26 | + */ |
| 27 | + readonly source: string; |
| 28 | + /** |
| 29 | + * Property accessor to `sass_import_get_srcmap` |
| 30 | + */ |
| 31 | + readonly sourceMap: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * Release allocated memory with created instance. |
| 35 | + * Accessor to sass_delete_import |
| 36 | + */ |
| 37 | + dispose(): void; |
| 38 | +} |
| 39 | + |
| 40 | +class SassImportEntry implements SassImportEntryInterface { |
| 41 | + /** |
| 42 | + * Raw pointer to `struct Sass_Import_Entry*` |
| 43 | + * @internal |
| 44 | + */ |
| 45 | + private readonly sassImportEntryPtr: number; |
| 46 | + constructor( |
| 47 | + private readonly cwrapImporter: ReturnType<typeof wrapSassImporter>, |
| 48 | + private readonly strMethod: StringMethodInterface, |
| 49 | + rel: string, |
| 50 | + abs: string, |
| 51 | + source: string, |
| 52 | + sourceMap: string |
| 53 | + ) { |
| 54 | + //make_import_entry internally just calls make_import |
| 55 | + this.sassImportEntryPtr = this.cwrapImporter.make_import( |
| 56 | + this.strMethod.alloc(rel), |
| 57 | + this.strMethod.alloc(abs), |
| 58 | + this.strMethod.alloc(source), |
| 59 | + this.strMethod.alloc(sourceMap) |
| 60 | + ); |
| 61 | + log(`SassImportEntry: created new instance`, { sassOptionsPtr: this.sassImportEntryPtr }); |
| 62 | + } |
| 63 | + |
| 64 | + public get error(): { message: string; line: number; column: number } { |
| 65 | + const column = this.cwrapImporter.import_get_error_column(this.sassImportEntryPtr); |
| 66 | + const line = this.cwrapImporter.import_get_error_line(this.sassImportEntryPtr); |
| 67 | + const error = this.cwrapImporter.import_get_error_message(this.sassImportEntryPtr); |
| 68 | + |
| 69 | + return { |
| 70 | + column, |
| 71 | + line, |
| 72 | + message: this.strMethod.ptrToString(error) |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + public set error({ message, line, column }: { message: string; line: number; column: number }) { |
| 77 | + const messagePtr = this.strMethod.alloc(message); |
| 78 | + this.cwrapImporter.import_set_error(this.sassImportEntryPtr, messagePtr, line, column); |
| 79 | + } |
| 80 | + |
| 81 | + public get importPath(): string { |
| 82 | + const pathPtr = this.cwrapImporter.import_get_imp_path(this.sassImportEntryPtr); |
| 83 | + return this.strMethod.ptrToString(pathPtr); |
| 84 | + } |
| 85 | + |
| 86 | + public get absPath(): string { |
| 87 | + const pathPtr = this.cwrapImporter.import_get_abs_path(this.sassImportEntryPtr); |
| 88 | + return this.strMethod.ptrToString(pathPtr); |
| 89 | + } |
| 90 | + |
| 91 | + public get source(): string { |
| 92 | + const sourcePtr = this.cwrapImporter.import_get_source(this.sassImportEntryPtr); |
| 93 | + return this.strMethod.ptrToString(sourcePtr); |
| 94 | + } |
| 95 | + |
| 96 | + public get sourceMap(): string { |
| 97 | + const sourceMapPtr = this.cwrapImporter.import_get_srcmap(this.sassImportEntryPtr); |
| 98 | + return this.strMethod.ptrToString(sourceMapPtr); |
| 99 | + } |
| 100 | + |
| 101 | + public dispose(): void { |
| 102 | + this.cwrapImporter.delete_import(this.sassImportEntryPtr); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export { SassImportEntryInterface, SassImportEntry }; |
0 commit comments