From f19fc1d2e0e534ba57b296a1191cb2a42c5269e3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 11 Jan 2024 11:08:48 -0500 Subject: [PATCH] Slight FFI tweaks to make deno less crashy on macos --- src/bun/ffi.ts | 10 +++++++++- src/common/termios.ts | 2 +- src/deno/ffi.ts | 17 ++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/bun/ffi.ts b/src/bun/ffi.ts index 95314b6..0e81fb8 100644 --- a/src/bun/ffi.ts +++ b/src/bun/ffi.ts @@ -37,12 +37,20 @@ try { } const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols; +let closed = false; const BunFFI: IFFI = { tcgetattr, tcsetattr, cfmakeraw, getPointer: ptr, - close: cStdLib.close, + close: () => { + if (!closed) { + cStdLib.close(); + closed = true; + } + + // Do nothing if FFI library was already closed + }, }; export default BunFFI; diff --git a/src/common/termios.ts b/src/common/termios.ts index e49cd0e..fc7f464 100644 --- a/src/common/termios.ts +++ b/src/common/termios.ts @@ -43,7 +43,7 @@ class Termios { * The pointer to the termios struct * @private */ - #ptr: unknown; + readonly #ptr: unknown; constructor(ffi: IFFI) { this.#ffi = ffi; diff --git a/src/deno/ffi.ts b/src/deno/ffi.ts index e10530b..f9c2f32 100644 --- a/src/deno/ffi.ts +++ b/src/deno/ffi.ts @@ -1,20 +1,20 @@ // Deno-specific ffi code import { IFFI } from '../common/runtime.ts'; -let libSuffix = ''; +let suffix = ''; switch (Deno.build.os) { case 'windows': - libSuffix = 'dll'; + suffix = 'dll'; break; case 'darwin': - libSuffix = 'dylib'; + suffix = 'dylib'; break; default: - libSuffix = 'so.6'; + suffix = 'so.6'; break; } -const cSharedLib = `libc.${libSuffix}`; +const cSharedLib = `libc.${suffix}`; const cStdLib = Deno.dlopen( cSharedLib, { @@ -41,8 +41,11 @@ const DenoFFI: IFFI = { cfmakeraw, getPointer: Deno.UnsafePointer.of, close: () => { - if (closed === false) { - cStdLib.close(); + if (!closed) { + // Closing appears to do bad things on macOS + if (Deno.build.os !== 'darwin') { + cStdLib.close(); + } closed = true; }