| The binding is Dart, and it works f7aea3b nandi 17h ago | 1 | /// The Nim core, as Dart functions. |
| 2 | /// |
| 3 | /// This is the whole of what knows `libfrqcore.so` is a native library; see |
| 4 | /// `nim/README.md` for why the logic is there rather than under `common/`. |
| 5 | /// |
| 6 | /// **Dart and not ClojureDart, on purpose.** The point of the Nim core is to |
| 7 | /// have less Clojure, so new code on this side of the boundary is written in |
| 8 | /// the language the platform speaks. It also sidesteps a real problem: |
| 9 | /// `lookupFunction` takes two type arguments, and generic interop is the part |
| 10 | /// of ClojureDart least worth fighting for a file that is pure marshalling. |
| 11 | /// |
| 12 | /// No `package:ffi` either. That package exists mostly for `Utf8` |
| 13 | /// conversions, and doing them here against `dart:convert` costs about ten |
| 14 | /// lines and keeps `pubspec.yaml` unchanged — which matters because every |
| 15 | /// dependency added here has to work on three targets. |
| 16 | /// |
| 17 | /// The two rules of the ABI, wrapped so no call site repeats them: |
| 18 | /// |
| 19 | /// * `frq_init` runs once before anything else. [_lib] does it on the way |
| 20 | /// out, so holding the handle means it has happened. |
| 21 | /// * Every string the core returns is **ours to free**, with `frq_free`. |
| 22 | /// [_takeString] is that, in a `finally` so a throw between the read and |
| 23 | /// the free does not leak. Nim's allocator is not Dart's, so calling |
| 24 | /// `malloc.free` on one of these pointers is undefined rather than merely |
| 25 | /// untidy. |
| 26 | library; |
| 27 | |
| 28 | import 'dart:convert'; |
| 29 | import 'dart:ffi'; |
| 30 | import 'dart:io'; |
| 31 | |
| 32 | // ---------------------------------------------------------------- the ABI |
| 33 | |
| 34 | typedef _InitNative = Void Function(); |
| 35 | typedef _InitDart = void Function(); |
| 36 | |
| 37 | typedef _FreeNative = Void Function(Pointer<Uint8>); |
| 38 | typedef _FreeDart = void Function(Pointer<Uint8>); |
| 39 | |
| 40 | typedef _VersionNative = Pointer<Uint8> Function(); |
| 41 | typedef _VersionDart = Pointer<Uint8> Function(); |
| 42 | |
| 43 | typedef _Str1Native = Pointer<Uint8> Function(Pointer<Uint8>); |
| 44 | typedef _Str1Dart = Pointer<Uint8> Function(Pointer<Uint8>); |
| 45 | |
| 46 | typedef _Str2Native = Pointer<Uint8> Function(Pointer<Uint8>, Pointer<Uint8>); |
| 47 | typedef _Str2Dart = Pointer<Uint8> Function(Pointer<Uint8>, Pointer<Uint8>); |
| 48 | |
| Nim owns the screen, Dart owns the pixels 43a02c2 nandi 16h ago | 49 | typedef _Str0Native = Pointer<Uint8> Function(); |
| 50 | typedef _Str0Dart = Pointer<Uint8> Function(); |
| 51 | |
| 52 | typedef _VoidNative = Void Function(); |
| 53 | typedef _VoidDart = void Function(); |
| 54 | |
| The binding is Dart, and it works f7aea3b nandi 17h ago | 55 | /// Where to look for the library, in order. |
| 56 | /// |
| 57 | /// Android resolves a bare soname out of the APK's `lib/<abi>/`. A desktop |
| 58 | /// build has no such rule, so the bare name is tried first (it works when the |
| 59 | /// object sits beside the executable or on the loader path) and then the |
| 60 | /// development path `just nim-lib` writes to. Named explicitly rather than by |
| 61 | /// exporting `LD_LIBRARY_PATH` from a launcher, because a variable set in a |
| 62 | /// wrapper script is a thing that works until someone starts the binary |
| 63 | /// another way. |
| 64 | DynamicLibrary _open() { |
| 65 | if (Platform.isAndroid) return DynamicLibrary.open('libfrqcore.so'); |
| 66 | // The development paths are relative to whichever directory the process |
| 67 | // started in: `dart test` runs from `dart/frq_core`, a built desktop bundle |
| 68 | // from the repo root. All of them are tried rather than guessing which |
| 69 | // invocation this is, because the failure mode is a StateError at first use |
| 70 | // rather than anything a type checker would have caught. |
| 71 | for (final p in [ |
| 72 | 'libfrqcore.so', |
| 73 | 'build/nim/libfrqcore.so', |
| Nim owns the screen, Dart owns the pixels 43a02c2 nandi 16h ago | 74 | '../build/nim/libfrqcore.so', // `flutter test`, from flutter/ |
| 75 | '../../build/nim/libfrqcore.so', // `dart test`, from dart/frq_core/ |
| The binding is Dart, and it works f7aea3b nandi 17h ago | 76 | ]) { |
| 77 | try { |
| 78 | return DynamicLibrary.open(p); |
| 79 | } on ArgumentError { |
| 80 | continue; |
| 81 | } |
| 82 | } |
| 83 | throw StateError( |
| 84 | 'libfrqcore.so not found — build it with `just nim-lib`, or ship it ' |
| 85 | 'beside the executable'); |
| 86 | } |
| 87 | |
| 88 | final DynamicLibrary _lib = () { |
| 89 | final lib = _open(); |
| 90 | lib.lookupFunction<_InitNative, _InitDart>('frq_init')(); |
| 91 | return lib; |
| 92 | }(); |
| 93 | |
| 94 | final _free = _lib.lookupFunction<_FreeNative, _FreeDart>('frq_free'); |
| 95 | |
| 96 | /// The bytes at [p] as a string, with [p] freed afterwards. Null in, null out. |
| 97 | String? _takeString(Pointer<Uint8> p) { |
| 98 | if (p == nullptr) return null; |
| 99 | try { |
| 100 | // Walk to the NUL rather than asking for a length the ABI does not carry. |
| 101 | var len = 0; |
| 102 | while (p[len] != 0) { |
| 103 | len++; |
| 104 | } |
| 105 | return utf8.decode(p.asTypedList(len)); |
| 106 | } finally { |
| 107 | _free(p); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// [s] as a NUL-terminated C string that the CALLER must free with [_freeArg]. |
| 112 | /// |
| 113 | /// Allocated with `malloc` from Dart's side, so it is freed from Dart's side — |
| 114 | /// the mirror of the rule for what comes back. The core never takes ownership |
| 115 | /// of an argument. |
| 116 | Pointer<Uint8> _toC(String s) { |
| 117 | final bytes = utf8.encode(s); |
| 118 | final p = _malloc(bytes.length + 1).cast<Uint8>(); |
| 119 | for (var i = 0; i < bytes.length; i++) { |
| 120 | p[i] = bytes[i]; |
| 121 | } |
| 122 | p[bytes.length] = 0; |
| 123 | return p; |
| 124 | } |
| 125 | |
| 126 | // malloc/free out of libc rather than package:ffi's allocator, for the same |
| 127 | // reason the rest of this file avoids that package: one less dependency to |
| 128 | // carry to three targets, for two symbols that are always there. |
| 129 | final DynamicLibrary _libc = |
| 130 | Platform.isWindows ? DynamicLibrary.open('msvcrt.dll') : DynamicLibrary.process(); |
| 131 | final _malloc = _libc |
| 132 | .lookupFunction<Pointer<Void> Function(IntPtr), Pointer<Void> Function(int)>('malloc'); |
| 133 | final _freeArg = |
| 134 | _libc.lookupFunction<Void Function(Pointer<Uint8>), void Function(Pointer<Uint8>)>('free'); |
| 135 | |
| 136 | /// Call a one-string-in, one-string-out entry point. |
| 137 | String? _call1(String symbol, String arg) { |
| 138 | final f = _lib.lookupFunction<_Str1Native, _Str1Dart>(symbol); |
| 139 | final a = _toC(arg); |
| 140 | try { |
| 141 | return _takeString(f(a)); |
| 142 | } finally { |
| 143 | _freeArg(a); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // ------------------------------------------------------------------ public |
| 148 | |
| 149 | /// The core's version, for a caller that wants to check the library it found |
| 150 | /// is the one it was built against. Static storage on the Nim side: the one |
| 151 | /// return value that is NOT freed. |
| 152 | String get version { |
| 153 | final p = _lib.lookupFunction<_VersionNative, _VersionDart>('frq_version')(); |
| 154 | var len = 0; |
| 155 | while (p[len] != 0) { |
| 156 | len++; |
| 157 | } |
| 158 | return utf8.decode(p.asTypedList(len)); |
| 159 | } |
| 160 | |
| 161 | /// An IRC line, taken apart: `{raw, tags, account, prefix, command, params}`. |
| 162 | /// |
| 163 | /// `tags`, `account` and `prefix` are null where the line carried none, which |
| 164 | /// is the distinction `frq.irc.parse` draws with nil and every caller depends |
| 165 | /// on — a PRIVMSG from a server with no prefix is not the same line as one |
| 166 | /// from a nick. |
| 167 | Map<String, dynamic> parseLine(String line) { |
| 168 | final json = _call1('frq_irc_parse_line', line); |
| 169 | return jsonDecode(json ?? 'null') as Map<String, dynamic>; |
| 170 | } |
| 171 | |
| 172 | /// One IRCv3 tag's value, unescaped — null where the tag is absent OR empty, |
| 173 | /// which IRCv3 says are the same thing. |
| 174 | String? tagValue(String tags, String key) { |
| 175 | final f = _lib.lookupFunction<_Str2Native, _Str2Dart>('frq_irc_tag_value'); |
| 176 | final a = _toC(tags); |
| 177 | final b = _toC(key); |
| 178 | try { |
| 179 | return _takeString(f(a, b)); |
| 180 | } finally { |
| 181 | _freeArg(a); |
| 182 | _freeArg(b); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | String unescapeTag(String v) => _call1('frq_irc_unescape_tag', v) ?? ''; |
| 187 | |
| 188 | String escapeTagValue(String v) => _call1('frq_irc_escape_tag_value', v) ?? ''; |
| 189 | |
| 190 | /// The nick half of a `nick!user@host` prefix. |
| 191 | String nickOf(String prefix) => _call1('frq_irc_nick_of', prefix) ?? ''; |
| Nim owns the screen, Dart owns the pixels 43a02c2 nandi 16h ago | 192 | |
| 193 | |
| Nim under the existing UI, not instead of it 56551a8 nandi 16h ago | 194 | /// Log through the Nim core's trace facility, so `FRQ_TRACE=1` gives one |
| 195 | /// interleaved story rather than two half-ones in different places. |
| 196 | void trace(String topic, String msg) { |
| 197 | final f = _lib.lookupFunction<_Str2Native, _Str2Dart>('frq_trace'); |
| 198 | final a = _toC(topic); |
| 199 | final b = _toC(msg); |
| 200 | try { |
| 201 | f(a, b); |
| 202 | } finally { |
| 203 | _freeArg(a); |
| 204 | _freeArg(b); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // --------------------------------------------------------------- transport |
| 209 | // |
| 210 | // `frq.net`'s three operations, with a Nim socket behind them. This is the |
| 211 | // wiring that leaves the existing ClojureDart screens, cells and actions |
| 212 | // alone: only the transport underneath them is Nim. |
| 213 | |
| 214 | typedef _ConnOpenNative = Void Function(Pointer<Uint8>, Int32, Int32); |
| 215 | typedef _ConnOpenDart = void Function(Pointer<Uint8>, int, int); |
| 216 | |
| 217 | /// Dial. Non-blocking: the socket runs on a Nim thread and progress arrives |
| 218 | /// through [connEvent]. |
| 219 | void connOpen(String host, int port, {bool tls = true}) { |
| 220 | final f = _lib.lookupFunction<_ConnOpenNative, _ConnOpenDart>('frq_conn_open'); |
| 221 | final a = _toC(host); |
| 222 | try { |
| 223 | f(a, port, tls ? 1 : 0); |
| 224 | } finally { |
| 225 | _freeArg(a); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// Queue a line. The transport adds the CRLF. |
| 230 | void connSend(String line) { |
| 231 | final f = _lib.lookupFunction<_Str1Native, _Str1Dart>('frq_conn_send'); |
| 232 | final a = _toC(line); |
| 233 | try { |
| 234 | f(a); |
| 235 | } finally { |
| 236 | _freeArg(a); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void connClose() => |
| 241 | _lib.lookupFunction<_VoidNative, _VoidDart>('frq_conn_close')(); |
| 242 | |
| 243 | /// The next line, or null when none is waiting. Never blocks. |
| 244 | String? connRecv() => _takeString( |
| 245 | _lib.lookupFunction<_Str0Native, _Str0Dart>('frq_conn_recv')()); |
| 246 | |
| 247 | /// The next transport event — `open`, `close: …`, `error: …` — or null. |
| 248 | String? connEvent() => _takeString( |
| 249 | _lib.lookupFunction<_Str0Native, _Str0Dart>('frq_conn_event')()); |