| One frontend where there were three, and a core that is not Clojure 438b247 nandi 20h ago | 1 | ## The C ABI, and nothing else. |
| 2 | ## |
| 3 | ## Every exported symbol is here so there is one file to read when asking what |
| 4 | ## the Dart side can call. The logic lives under `frq/` in ordinary Nim with |
| 5 | ## ordinary Nim types, which is what lets the tests test the rules rather than |
| 6 | ## the marshalling. |
| 7 | ## |
| 8 | ## Two conventions, both of which the bindings in `flutter/src/frq/core/` |
| 9 | ## wrap so no call site has to remember them: |
| 10 | ## |
| 11 | ## * Every returned string is the **caller's** to free, with `frq_free`. Nim's |
| 12 | ## allocator is not Dart's, so a `free()` on this side of the boundary is |
| 13 | ## undefined behaviour rather than a leak you can live with. |
| 14 | ## * `frq_init` runs once before anything else. Nim's runtime needs setting up |
| 15 | ## and `--app:lib` does not do it for you on every platform. |
| 16 | ## |
| 17 | ## Answers that are not a single string come back as JSON. A struct would mean |
| 18 | ## both sides agreeing on a memory layout, and a field added later would be a |
| 19 | ## version skew that segfaults rather than one that fails; JSON costs a parse |
| 20 | ## per call, which is nothing against the network round trip that produced the |
| 21 | ## line being parsed. |
| 22 | |
| 23 | import std/json |
| 24 | import frq/ircparse |
| 25 | |
| 26 | proc NimMain() {.importc.} |
| 27 | |
| 28 | var initialised = false |
| 29 | |
| 30 | proc frq_init*() {.exportc, dynlib.} = |
| 31 | ## Set Nim's runtime up. Idempotent, because a binding that guesses wrong |
| 32 | ## about whether it has been called should be harmless rather than fatal. |
| 33 | if not initialised: |
| 34 | NimMain() |
| 35 | initialised = true |
| 36 | |
| 37 | proc dup(s: string): cstring = |
| 38 | ## A copy of `s` that outlives this call, for the caller to `frq_free`. |
| 39 | ## `allocShared0` and not `alloc0`: the Dart side may free it from a |
| 40 | ## different thread than the one that made it. |
| 41 | let n = s.len |
| 42 | let p = cast[cstring](allocShared0(n + 1)) |
| 43 | if n > 0: |
| 44 | copyMem(p, unsafeAddr s[0], n) |
| 45 | p |
| 46 | |
| 47 | proc frq_free*(p: cstring) {.exportc, dynlib.} = |
| 48 | ## Free what one of the functions below returned. Null is fine. |
| 49 | if p != nil: |
| 50 | deallocShared(p) |
| 51 | |
| 52 | proc frq_version*(): cstring {.exportc, dynlib.} = |
| 53 | ## Static storage, deliberately: this one is NOT freed, and is the only |
| 54 | ## exception to the rule above. It exists so a binding can check at load |
| 55 | ## time that the library it found is the one it was built against. |
| 56 | "0.1.0" |
| 57 | |
| 58 | # ----------------------------------------------------------------- irc/parse |
| 59 | |
| 60 | proc frq_irc_parse_line*(line: cstring): cstring {.exportc, dynlib.} = |
| 61 | ## An IRC line as JSON: `{raw, tags, account, prefix, command, params}`. |
| 62 | ## |
| 63 | ## `tags`, `account` and `prefix` are JSON null where the line carried none, |
| 64 | ## which is the distinction `frq.irc.parse` draws with nil and every caller |
| 65 | ## of it depends on — a PRIVMSG from a server with no prefix is not the same |
| 66 | ## line as one from a nick. |
| 67 | if line == nil: return dup("null") |
| 68 | let p = parseLine($line) |
| 69 | var o = newJObject() |
| 70 | o["raw"] = %p.raw |
| 71 | o["tags"] = if p.hasTags: %p.tags else: newJNull() |
| 72 | o["account"] = if p.hasAccount: %p.account else: newJNull() |
| 73 | o["prefix"] = if p.hasPrefix: %p.prefix else: newJNull() |
| 74 | o["command"] = %p.command |
| 75 | o["params"] = %p.params |
| 76 | dup($o) |
| 77 | |
| 78 | proc frq_irc_tag_value*(tags, key: cstring): cstring {.exportc, dynlib.} = |
| 79 | ## One tag's value, unescaped — or **null** where the tag is absent or |
| 80 | ## empty, which IRCv3 says are the same thing. Null and not "" on purpose: |
| 81 | ## see `tagValue`. |
| 82 | if tags == nil or key == nil: return nil |
| 83 | let (v, ok) = tagValue($tags, $key) |
| 84 | if ok: dup(v) else: nil |
| 85 | |
| 86 | proc frq_irc_unescape_tag*(v: cstring): cstring {.exportc, dynlib.} = |
| 87 | if v == nil: return nil |
| 88 | dup(unescapeTag($v)) |
| 89 | |
| 90 | proc frq_irc_escape_tag_value*(v: cstring): cstring {.exportc, dynlib.} = |
| 91 | if v == nil: return dup("") |
| 92 | dup(escapeTagValue($v)) |
| 93 | |
| 94 | proc frq_irc_nick_of*(prefix: cstring): cstring {.exportc, dynlib.} = |
| 95 | if prefix == nil: return nil |
| 96 | dup(nickOf($prefix)) |