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-30 06:53:32 +00:00
|
|
|
import { join } from "path";
|
2024-08-29 20:13:48 +00:00
|
|
|
|
2024-08-30 06:53:32 +00:00
|
|
|
const libname = `clib.${suffix}`;
|
|
|
|
const libpath = join("clib", libname);
|
2024-08-29 20:13:48 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2024-08-30 06:53:32 +00:00
|
|
|
const buf = Buffer.from(toBuffer(pointer, 0, GetLen(iter)));
|
|
|
|
Free(pointer);
|
2024-08-29 20:13:48 +00:00
|
|
|
return buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
Cleanup(iter);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
next,
|
|
|
|
cleanup,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
initSQLite,
|
|
|
|
rowIterator,
|
|
|
|
};
|