scroll/src/bun/ffi.ts

41 lines
706 B
JavaScript
Raw Normal View History

2023-11-01 14:00:40 -04:00
/**
* This is all the nasty ffi setup for the bun runtime
*/
import { dlopen, ptr, suffix } from "bun:ffi";
const getLib = (name: string) => {
return dlopen(
name,
{
tcgetattr: {
args: ['i32', 'pointer'],
returns: 'i32',
},
tcsetattr: {
args: ['i32', 'i32', 'pointer'],
returns: 'i32',
},
cfmakeraw: {
args: ['pointer'],
returns: 'void',
},
},
);
}
let cStdLib = {symbols: {}};
try {
cStdLib = getLib(`libc.${suffix}`);
} catch {
2023-11-01 14:00:40 -04:00
try {
cStdLib = getLib(`libc.${suffix}.6`);
}
catch {
throw new Error('Could not find c standard library');
}
}
2023-11-01 14:00:40 -04:00
export const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols;
2023-11-01 15:25:52 -04:00
export const getPointer = ptr;