2023-11-01 14:00:40 -04:00
|
|
|
/**
|
|
|
|
* This is all the nasty ffi setup for the bun runtime
|
|
|
|
*/
|
2023-11-01 15:27:31 -04:00
|
|
|
import { dlopen, ptr, suffix } from 'bun:ffi';
|
2023-11-16 11:10:33 -05:00
|
|
|
import { IFFI } from '../common/runtime.ts';
|
2023-10-31 17:38:15 -04:00
|
|
|
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2023-11-01 15:27:31 -04:00
|
|
|
};
|
2023-10-31 17:38:15 -04:00
|
|
|
|
2023-11-08 15:53:14 -05:00
|
|
|
let cStdLib: any = { symbols: {} };
|
2023-10-31 17:38:15 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
cStdLib = getLib(`libc.${suffix}`);
|
|
|
|
} catch {
|
2023-11-01 14:00:40 -04:00
|
|
|
try {
|
|
|
|
cStdLib = getLib(`libc.${suffix}.6`);
|
2023-11-01 15:27:31 -04:00
|
|
|
} catch {
|
2023-11-01 14:00:40 -04:00
|
|
|
throw new Error('Could not find c standard library');
|
2023-10-31 17:38:15 -04:00
|
|
|
}
|
|
|
|
}
|
2023-11-01 14:00:40 -04:00
|
|
|
|
2023-11-08 15:53:14 -05:00
|
|
|
const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols;
|
|
|
|
const BunFFI: IFFI = {
|
|
|
|
tcgetattr,
|
|
|
|
tcsetattr,
|
|
|
|
cfmakeraw,
|
|
|
|
getPointer: ptr,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BunFFI;
|