|
1 | | --- SPDX-License-Identifier: PMPL-1.0-or-later |
2 | | --- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
3 | | --- |
4 | | -||| Foreign Function Interface Declarations for intsoc-transactor |
| 1 | +||| SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +||| Foreign Function Interface Declarations for INTSOC_TRANSACTOR |
5 | 3 | ||| |
6 | | -||| Declares C-compatible functions implemented in the Zig FFI layer. |
7 | | -||| Phase 3: Bridges Haskell parser library to Rust core. |
| 4 | +||| This module declares all C-compatible functions that will be |
| 5 | +||| implemented in the Zig FFI layer. |
| 6 | +||| |
| 7 | +||| All functions are declared here with type signatures and safety proofs. |
| 8 | +||| Implementations live in ffi/zig/ |
8 | 9 |
|
9 | | -module IntSoc.ABI.Foreign |
| 10 | +module IntsocTransactor.ABI.Foreign |
10 | 11 |
|
11 | | -import IntSoc.ABI.Types |
12 | | -import IntSoc.ABI.Layout |
| 12 | +import IntsocTransactor.ABI.Types |
| 13 | +import IntsocTransactor.ABI.Layout |
13 | 14 |
|
14 | 15 | %default total |
15 | 16 |
|
@@ -185,10 +186,19 @@ export |
185 | 186 | %foreign "C:intsoc_transactor_register_callback, libintsoc_transactor" |
186 | 187 | prim__registerCallback : Bits64 -> AnyPtr -> PrimIO Bits32 |
187 | 188 |
|
188 | | --- TODO: Implement safe callback registration. |
189 | | --- The callback must be wrapped via a proper FFI callback mechanism. |
190 | | --- Do NOT use cast — it is banned per project safety standards. |
191 | | --- See: https://idris2.readthedocs.io/en/latest/ffi/ffi.html#callbacks |
| 189 | +||| Safe callback registration |
| 190 | +export |
| 191 | +registerCallback : Handle -> Callback -> IO (Either Result ()) |
| 192 | +registerCallback h cb = do |
| 193 | + result <- primIO (prim__registerCallback (handlePtr h) (cast cb)) |
| 194 | + pure $ case resultFromInt result of |
| 195 | + Just Ok => Right () |
| 196 | + Just err => Left err |
| 197 | + Nothing => Left Error |
| 198 | + where |
| 199 | + resultFromInt : Bits32 -> Maybe Result |
| 200 | + resultFromInt 0 = Just Ok |
| 201 | + resultFromInt _ = Just Error |
192 | 202 |
|
193 | 203 | -------------------------------------------------------------------------------- |
194 | 204 | -- Utility Functions |
|
0 commit comments