nandi/frqpublic Fork 0
438b247afa2e97c6a6954709771915e2e72203f6
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

README.md · 88 lines · 3.9 KBmarkdown Blame HistoryRaw
One frontend where there were three, and a core that is not Clojure 438b247 nandi 22h ago1# The Nim core
2
3The 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
8reason to move any of it is not that it is broken: it is that the logic under
9the screens — the IRC wire format, the atproto flows, the message signatures —
10is the part with the most rules per line and the least to do with Flutter, and
11it is the part worth having in a language with a type checker and a test runner
12that does not need a Flutter toolchain to run.
13
14So the plan is a seam, not a rewrite-in-place. Each module moves one at a time:
15the Nim implementation lands here with tests, the Dart binding lands in
16`flutter/src/frq/core/`, and the ClojureDart original stays until the binding
17is proven against it. Nothing is deleted on faith.
18
19## What is here
20
21```
22src/frq_core.nim the C ABI: every exported symbol, and nothing else
23src/frq/ircparse.nim the IRC wire format
24tests/ 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
29rather than the marshalling.
30
31## The ABI
32
33Strings in, strings out, and JSON where the answer is not a single string.
34
35That is a deliberate choice against a struct-based ABI. A struct means the Dart
36side and the Nim side have to agree on a memory layout, and every field added
37later is a version skew that segfaults instead of failing. JSON costs a parse
38per call, which is nothing against a network round trip, and it lets one side
39gain a field without the other crashing.
40
41Every function that returns a string returns memory the **caller must free**
42with `frq_free`. Nim's allocator is not Dart's; a `free()` from the Dart side
43on a Nim pointer is undefined. The bindings in `flutter/src/frq/core/` wrap
44that 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
47up Nim's runtime. The bindings do it on first use.
48
49## The web
50
51A native library does not load in a browser, so the web target cannot call this
52through `dart:ffi`. Nim compiles through C, so the route is emscripten to wasm
53and a JS binding rather than a second implementation — but that is not built
54yet, and until it is, **the web build must keep using the ClojureDart
55originals**. This is why the originals stay in `common/` rather than being
56deleted 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
61ABI is exercised from C through `dlopen`, including the allocation contract
62under a hundred thousand parse/free cycles. That half is real.
63
64The Dart binding (`flutter/src/frq/core/ffi.cljd`) is **not**: it is written
65but nothing requires it, so no build has compiled it. Nothing calls the core
66yet, and `common/frq/irc/parse.cljc` is still what every target actually runs.
67
68The next step is that binding, and it is the one with the unknown in it:
69`.lookupFunction` takes native and Dart type arguments, and how ClojureDart
70spells a generic interop call is the thing to establish before porting a
71second module. After it: the library has to reach the targets — `jniLibs` for
72the APK, beside the executable for the desktop bundle — and then
73`frq.core.irc` can choose between the Nim implementation and the ClojureDart
74one per target, which is also how the web keeps working.
75
76Modules still in `common/` and not yet here: `rooms`, `msgsig`, `crypto`,
77`atproto/core`, `oauth/core`, `store`, `irc/handshake`, `irc/mutate`,
78`profile`, `members`, `reactions`, `replies`, `edits`, `clock`.
79
80## Building
81
82```bash
83just nim-test # the Nim test suite
84just nim-lib # libfrqcore.so into build/nim
85```
86
87Both want `nix develop .#nim`, and re-enter it themselves if they are not
88already inside.