dal/dal/Bunding.ts

68 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-08-29 20:13:48 +00:00
/**
* This file is responsible for binding the C library to the Bun runtime.
*/
import { dlopen, FFIType, suffix, ptr, toBuffer } from "bun:ffi";
2024-08-29 20:35:08 +00:00
const libname = `../clib/clib.${suffix}`;
2024-08-29 20:13:48 +00:00
const libpath = libname;
const {
symbols: { InitSQLite, CreateRowIterator, NextRow, GetLen, Free, Cleanup },
} = dlopen(libpath, {
InitSQLite: {
args: [FFIType.cstring],
returns: FFIType.void,
},
CreateRowIterator: {
args: [FFIType.cstring, FFIType.i32],
returns: FFIType.i32,
},
NextRow: {
args: [FFIType.i32],
returns: FFIType.ptr,
},
GetLen: {
args: [FFIType.i32],
returns: FFIType.i32,
},
Free: {
args: [FFIType.ptr],
returns: FFIType.void,
},
Cleanup: {
args: [FFIType.i32],
returns: FFIType.void,
},
});
2024-08-29 20:39:27 +00:00
function initSQLite(pragmas: Buffer) {
InitSQLite(ptr(pragmas));
2024-08-29 20:13:48 +00:00
}
function rowIterator(buf: Buffer) {
const iter = CreateRowIterator(ptr(buf), buf.length);
const next = () => {
const pointer = NextRow(iter);
if (pointer === null) {
return null;
}
const buf = toBuffer(pointer, 0, GetLen(iter));
//Free(pointer) //should be resolved by GC;
return buf;
};
const cleanup = () => {
Cleanup(iter);
};
return {
next,
cleanup,
};
}
export default {
initSQLite,
rowIterator,
};