Slight FFI tweaks to make deno less crashy on macos

This commit is contained in:
Timothy Warren 2024-01-11 11:08:48 -05:00
parent 15496646d6
commit f19fc1d2e0
3 changed files with 20 additions and 9 deletions

View File

@ -37,12 +37,20 @@ try {
} }
const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols; const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols;
let closed = false;
const BunFFI: IFFI = { const BunFFI: IFFI = {
tcgetattr, tcgetattr,
tcsetattr, tcsetattr,
cfmakeraw, cfmakeraw,
getPointer: ptr, getPointer: ptr,
close: cStdLib.close, close: () => {
if (!closed) {
cStdLib.close();
closed = true;
}
// Do nothing if FFI library was already closed
},
}; };
export default BunFFI; export default BunFFI;

View File

@ -43,7 +43,7 @@ class Termios {
* The pointer to the termios struct * The pointer to the termios struct
* @private * @private
*/ */
#ptr: unknown; readonly #ptr: unknown;
constructor(ffi: IFFI) { constructor(ffi: IFFI) {
this.#ffi = ffi; this.#ffi = ffi;

View File

@ -1,20 +1,20 @@
// Deno-specific ffi code // Deno-specific ffi code
import { IFFI } from '../common/runtime.ts'; import { IFFI } from '../common/runtime.ts';
let libSuffix = ''; let suffix = '';
switch (Deno.build.os) { switch (Deno.build.os) {
case 'windows': case 'windows':
libSuffix = 'dll'; suffix = 'dll';
break; break;
case 'darwin': case 'darwin':
libSuffix = 'dylib'; suffix = 'dylib';
break; break;
default: default:
libSuffix = 'so.6'; suffix = 'so.6';
break; break;
} }
const cSharedLib = `libc.${libSuffix}`; const cSharedLib = `libc.${suffix}`;
const cStdLib = Deno.dlopen( const cStdLib = Deno.dlopen(
cSharedLib, cSharedLib,
{ {
@ -41,8 +41,11 @@ const DenoFFI: IFFI = {
cfmakeraw, cfmakeraw,
getPointer: Deno.UnsafePointer.of, getPointer: Deno.UnsafePointer.of,
close: () => { close: () => {
if (closed === false) { if (!closed) {
// Closing appears to do bad things on macOS
if (Deno.build.os !== 'darwin') {
cStdLib.close(); cStdLib.close();
}
closed = true; closed = true;
} }