| One frontend where there were three, and a core that is not Clojure 438b247 nandi 20h ago | 1 | # The Nim core |
| 2 | |
| 3 | The portable half of frq, as a native library the Dart side calls through FFI. |
| 4 | |
| 5 | ## Why this exists |
| 6 | |
| 7 | `common/` is ClojureDart, compiled into every target. That works, and the |
| 8 | reason to move any of it is not that it is broken: it is that the logic under |
| 9 | the screens — the IRC wire format, the atproto flows, the message signatures — |
| 10 | is the part with the most rules per line and the least to do with Flutter, and |
| 11 | it is the part worth having in a language with a type checker and a test runner |
| 12 | that does not need a Flutter toolchain to run. |
| 13 | |
| 14 | So the plan is a seam, not a rewrite-in-place. Each module moves one at a time: |
| 15 | the Nim implementation lands here with tests, the Dart binding lands in |
| 16 | `flutter/src/frq/core/`, and the ClojureDart original stays until the binding |
| 17 | is proven against it. Nothing is deleted on faith. |
| 18 | |
| 19 | ## What is here |
| 20 | |
| 21 | ``` |
| 22 | src/frq_core.nim the C ABI: every exported symbol, and nothing else |
| 23 | src/frq/ircparse.nim the IRC wire format |
| 24 | tests/ one per module, run by `just nim-test` |
| 25 | ``` |
| 26 | |
| 27 | `src/frq_core.nim` is the only file that knows about C. Everything under |
| 28 | `src/frq/` is ordinary Nim with ordinary Nim types, so the tests test the logic |
| 29 | rather than the marshalling. |
| 30 | |
| 31 | ## The ABI |
| 32 | |
| 33 | Strings in, strings out, and JSON where the answer is not a single string. |
| 34 | |
| 35 | That is a deliberate choice against a struct-based ABI. A struct means the Dart |
| 36 | side and the Nim side have to agree on a memory layout, and every field added |
| 37 | later is a version skew that segfaults instead of failing. JSON costs a parse |
| 38 | per call, which is nothing against a network round trip, and it lets one side |
| 39 | gain a field without the other crashing. |
| 40 | |
| 41 | Every function that returns a string returns memory the **caller must free** |
| 42 | with `frq_free`. Nim's allocator is not Dart's; a `free()` from the Dart side |
| 43 | on a Nim pointer is undefined. The bindings in `flutter/src/frq/core/` wrap |
| 44 | that in a `try/finally` so no call site has to remember. |
| 45 | |
| 46 | `frq_init` must be called once before anything else, and calls `NimMain` to set |
| 47 | up Nim's runtime. The bindings do it on first use. |
| 48 | |
| 49 | ## The web |
| 50 | |
| 51 | A native library does not load in a browser, so the web target cannot call this |
| 52 | through `dart:ffi`. Nim compiles through C, so the route is emscripten to wasm |
| 53 | and a JS binding rather than a second implementation — but that is not built |
| 54 | yet, and until it is, **the web build must keep using the ClojureDart |
| 55 | originals**. This is why the originals stay in `common/` rather than being |
| 56 | deleted as each module lands: they are the web's implementation, not dead code. |
| 57 | |
| 58 | ## Status |
| 59 | |
| 60 | `frq/ircparse.nim` is ported and tested — 29 cases, `just nim-test` — and the |
| 61 | ABI is exercised from C through `dlopen`, including the allocation contract |
| 62 | under a hundred thousand parse/free cycles. That half is real. |
| 63 | |
| The binding is Dart, and it works f7aea3b nandi 20h ago | 64 | The Dart binding is real too, and is `dart/frq_core` — **plain Dart, not |
| 65 | ClojureDart**. It calls the library over `dart:ffi` and is covered by 20 tests |
| 66 | on the Dart VM, including the UTF-8 round trip and ten thousand calls against |
| 67 | the ownership rules. `just dart-test` runs the pair of them in about a second. |
| 68 | |
| 69 | The binding was ClojureDart for one commit and should not have been: the Nim |
| 70 | core exists to have less Clojure in the tree, and `lookupFunction` takes two |
| 71 | type arguments, so it meant fighting generic interop to write more of the |
| 72 | thing being removed. In Dart it is a typedef. See `dart/README.md`. |
| 73 | |
| 74 | What is **not** done is the wiring: nothing imports `frq_core`, so |
| 75 | `common/frq/irc/parse.cljc` is still what every target runs. That step is its |
| 76 | own piece of work — the Flutter app takes the package as a path dependency |
| 77 | (which means a `pubspec.lock` regeneration and widening the nix build's source |
| 78 | root), the library has to reach each target (`jniLibs` for the APK, beside the |
| 79 | executable for the desktop bundle), and only then can a call site choose Nim |
| 80 | on native and the ClojureDart original on the web. |
| One frontend where there were three, and a core that is not Clojure 438b247 nandi 20h ago | 81 | |
| 82 | Modules still in `common/` and not yet here: `rooms`, `msgsig`, `crypto`, |
| 83 | `atproto/core`, `oauth/core`, `store`, `irc/handshake`, `irc/mutate`, |
| 84 | `profile`, `members`, `reactions`, `replies`, `edits`, `clock`. |
| 85 | |
| 86 | ## Building |
| 87 | |
| 88 | ```bash |
| 89 | just nim-test # the Nim test suite |
| 90 | just nim-lib # libfrqcore.so into build/nim |
| 91 | ``` |
| 92 | |
| 93 | Both want `nix develop .#nim`, and re-enter it themselves if they are not |
| 94 | already inside. |