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

Commit

Permalink
feat(getfnptrhandler): wrapper to add, removefunction
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 20, 2018
1 parent 3e8fb58 commit 356bf24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/SassAsmModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface SassAsmModule extends Required<BaseAsmModule> {
_libsass_version: () => number;
_libsass_language_version: () => number;
_sass2scss_version: () => number;
addFunction: (fn: Function) => number;
removeFunction: (fnPtr: number) => void;
}

export { SassAsmModule };
26 changes: 26 additions & 0 deletions src/interop/fnPtrHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SassAsmModule } from '../SassAsmModule';

/**
* Returns handler object to wrap `addFunction` and `removeFunction`
*/
const getFnPtrHandler = ({ addFunction, removeFunction }: SassAsmModule) => {
let count = 0;

return {
add: (fn: Function) => {
if (count === 20) {
throw new Error('Maximum number of function pointer added');
}
count++;
return addFunction(fn);
},
remove: (fnPtr: number) => {
if (count > 0) {
count--;
removeFunction(fnPtr);
}
}
};
};

export { getFnPtrHandler };

0 comments on commit 356bf24

Please sign in to comment.