This is achieved by storing the resources we need to acquire for interacting with the flipdot (socket fd and addrinfo struct) in a `struct flipdot` that is dynamically allocated and treated as an opaque pointer object via the BQN FFI. To make sure these resources are released correctly, we only provide a lisp style WithFlipdot to the user which takes care of acquiring and releasing the `struct flipdot`. This works even if an error occurs in the function the user provides thanks to _defer_. I'm not sure if calling it _defer_ is right since Go's error handling works differently, so defer really is deferred execution in a sense which doesn't really fit what we're doing here. The closest is probably Haskell's bracket, but that name references it's triadic nature which doesn't fit our implementation. Change-Id: Iff65d277a448dbe0c6ac93e816ece5ab6fa10190 Reviewed-on: https://cl.tvl.fyi/c/depot/+/13011 Reviewed-by: sterni <sternenseemann@systemli.org> Tested-by: BuildkiteCI
27 lines
753 B
BQN
Executable file
27 lines
753 B
BQN
Executable file
#!/usr/bin/env BQN
|
||
|
||
⟨WithFlipdot⟩ ⇐ •Import "../lib/"⊸∾⍟("bin" (⊣≡(-∘≠⊸↑)) •path) "blipqn.bqn"
|
||
|
||
examples ← ⍉>⟨
|
||
"random"‿{𝕩.Send 𝕩.shape •rand.Range 2},
|
||
"235"‿{𝕩.Send 1˙⌾(4‿22⊸⊑) 𝕩.empty},
|
||
⟩
|
||
|
||
usage ← "Usage: "∾•name∾" HOST PORT WIDTH HEIGHT EXAMPLE"∾"
|
||
|
||
where EXAMPLE is one of
|
||
|
||
"∾∾{(7↑"")∾"- "∾𝕩∾@+10}¨⊏examples
|
||
|
||
# TODO(sterni): this is an atrocious interface
|
||
opts ← {
|
||
(•Exit 1˙∘•Out)⍟(5⊸≠≠•args) usage
|
||
host‿port‿width‿height‿example ← •args
|
||
|
||
example ⇐
|
||
cfg ⇐ ⟨host⟩∾•ParseFloat¨ port‿width‿height
|
||
}
|
||
|
||
action ← examples (⊏⊸(⊑∘⊐) ⊑ ⊏˜⟜1)⟜< opts.example
|
||
|
||
opts.cfg WithFlipdot action
|