|
| 1 | +import { dlopen, FFIType } from 'bun:ffi'; |
| 2 | +import { UnsupportedPlatformError } from '../../../common/errors'; |
| 3 | +import { currentPlatform } from '../../../common/platform'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Loads GLib's main-context iteration symbols. |
| 7 | + * |
| 8 | + * GLib (not GTK directly) owns the main-loop primitives Sambar uses to pump the |
| 9 | + * Linux UI cooperatively, mirroring the macOS CoreFoundation pump (D020). |
| 10 | + * `libglib-2.0` is a hard dependency of GTK 4, so it is always present wherever |
| 11 | + * `libgtk-4` is. |
| 12 | + * |
| 13 | + * `g_main_context_iteration(context, may_block)` dispatches at most one set of |
| 14 | + * ready sources; a `NULL` context means the default one. `g_main_context_pending` |
| 15 | + * reports whether any sources are ready, letting us drain to quiescence without |
| 16 | + * ever blocking Bun's thread. |
| 17 | + * |
| 18 | + * Only callable on Linux — throws {@link UnsupportedPlatformError} otherwise so |
| 19 | + * the module stays safely importable on macOS for unit testing. |
| 20 | + */ |
| 21 | + |
| 22 | +const LIBGLIB_PATH = 'libglib-2.0.so.0'; |
| 23 | + |
| 24 | +export const loadGlibFFI = () => { |
| 25 | + const platform = currentPlatform(); |
| 26 | + if (platform !== 'linux') { |
| 27 | + throw new UnsupportedPlatformError( |
| 28 | + `loadGlibFFI() is only supported on Linux; current platform is ${platform}`, |
| 29 | + ); |
| 30 | + } |
| 31 | + return dlopen(LIBGLIB_PATH, { |
| 32 | + g_main_context_iteration: { |
| 33 | + args: [FFIType.pointer, FFIType.i32], |
| 34 | + returns: FFIType.i32, |
| 35 | + }, |
| 36 | + g_main_context_pending: { |
| 37 | + args: [FFIType.pointer], |
| 38 | + returns: FFIType.i32, |
| 39 | + }, |
| 40 | + }); |
| 41 | +}; |
0 commit comments