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

Commit

Permalink
feat(sassimportentry): implement sassimportentry
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 20, 2018
1 parent e8c6c70 commit c099763
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/interop/importer/sassImportEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { log } from '../../util/logger';
import { StringMethodInterface } from '../interopUtility';
import { wrapSassImporter } from './wrapSassImporter';

/**
* Interop interface to `Sass_Import_Entry`
*
* This interface doesn't implements ownership api
* sass_import_take_source / sass_import_take_srcmap
*/
interface SassImportEntryInterface {
/**
* Property accessor to `sass_import_set_error` and `sass_import_get_error_(line|column|message)`
*/
error: { message: string; line: number; column: number };
/**
* Property accessor to `sass_import_get_imp_path`
*/
readonly importPath: string;
/**
* Property accessor to `sass_import_get_abs_path`
*/
readonly absPath: string;
/**
* Property accessor to `sass_import_get_source`
*/
readonly source: string;
/**
* Property accessor to `sass_import_get_srcmap`
*/
readonly sourceMap: string;

/**
* Release allocated memory with created instance.
* Accessor to sass_delete_import
*/
dispose(): void;
}

class SassImportEntry implements SassImportEntryInterface {
/**
* Raw pointer to `struct Sass_Import_Entry*`
* @internal
*/
private readonly sassImportEntryPtr: number;
constructor(
private readonly cwrapImporter: ReturnType<typeof wrapSassImporter>,
private readonly strMethod: StringMethodInterface,
rel: string,
abs: string,
source: string,
sourceMap: string
) {
//make_import_entry internally just calls make_import
this.sassImportEntryPtr = this.cwrapImporter.make_import(
this.strMethod.alloc(rel),
this.strMethod.alloc(abs),
this.strMethod.alloc(source),
this.strMethod.alloc(sourceMap)
);
log(`SassImportEntry: created new instance`, { sassOptionsPtr: this.sassImportEntryPtr });
}

public get error(): { message: string; line: number; column: number } {
const column = this.cwrapImporter.import_get_error_column(this.sassImportEntryPtr);
const line = this.cwrapImporter.import_get_error_line(this.sassImportEntryPtr);
const error = this.cwrapImporter.import_get_error_message(this.sassImportEntryPtr);

return {
column,
line,
message: this.strMethod.ptrToString(error)
};
}

public set error({ message, line, column }: { message: string; line: number; column: number }) {
const messagePtr = this.strMethod.alloc(message);
this.cwrapImporter.import_set_error(this.sassImportEntryPtr, messagePtr, line, column);
}

public get importPath(): string {
const pathPtr = this.cwrapImporter.import_get_imp_path(this.sassImportEntryPtr);
return this.strMethod.ptrToString(pathPtr);
}

public get absPath(): string {
const pathPtr = this.cwrapImporter.import_get_abs_path(this.sassImportEntryPtr);
return this.strMethod.ptrToString(pathPtr);
}

public get source(): string {
const sourcePtr = this.cwrapImporter.import_get_source(this.sassImportEntryPtr);
return this.strMethod.ptrToString(sourcePtr);
}

public get sourceMap(): string {
const sourceMapPtr = this.cwrapImporter.import_get_srcmap(this.sassImportEntryPtr);
return this.strMethod.ptrToString(sourceMapPtr);
}

public dispose(): void {
this.cwrapImporter.delete_import(this.sassImportEntryPtr);
}
}

export { SassImportEntryInterface, SassImportEntry };

0 comments on commit c099763

Please sign in to comment.