|
1 | | -||| Foreign Function Interface Declarations |
| 1 | +||| VALENCE-SHELL (vsh) — FFI Bridge Declarations |
2 | 2 | ||| |
3 | | -||| This module declares all C-compatible functions that will be |
4 | | -||| implemented in the Zig FFI layer. |
5 | | -||| |
6 | | -||| All functions are declared here with type signatures and safety proofs. |
7 | | -||| Implementations live in ffi/zig/ |
| 3 | +||| This module defines the formal bridge to the valence-shell native |
| 4 | +||| implementation. It ensures that shell transmutations and command |
| 5 | +||| execution are handled with strict type safety. |
8 | 6 |
|
9 | | -module {{PROJECT}}.ABI.Foreign |
| 7 | +module VALENCE_SHELL.ABI.Foreign |
10 | 8 |
|
11 | | -import {{PROJECT}}.ABI.Types |
12 | | -import {{PROJECT}}.ABI.Layout |
| 9 | +import VALENCE_SHELL.ABI.Types |
| 10 | +import VALENCE_SHELL.ABI.Layout |
13 | 11 |
|
14 | 12 | %default total |
15 | 13 |
|
16 | 14 | -------------------------------------------------------------------------------- |
17 | | --- Library Lifecycle |
| 15 | +-- Lifecycle |
18 | 16 | -------------------------------------------------------------------------------- |
19 | 17 |
|
20 | | -||| Initialize the library |
21 | | -||| Returns a handle to the library instance, or Nothing on failure |
| 18 | +||| Initializes the shell transmutation engine. |
22 | 19 | export |
23 | | -%foreign "C:{{project}}_init, lib{{project}}" |
| 20 | +%foreign "C:vsh_init, libvsh" |
24 | 21 | prim__init : PrimIO Bits64 |
25 | 22 |
|
26 | | -||| Safe wrapper for library initialization |
| 23 | +||| Safe initialization wrapper. |
27 | 24 | export |
28 | 25 | init : IO (Maybe Handle) |
29 | 26 | init = do |
30 | 27 | ptr <- primIO prim__init |
31 | 28 | pure (createHandle ptr) |
32 | 29 |
|
33 | | -||| Clean up library resources |
| 30 | +||| Releases shell engine resources. |
34 | 31 | export |
35 | | -%foreign "C:{{project}}_free, lib{{project}}" |
| 32 | +%foreign "C:vsh_free, libvsh" |
36 | 33 | prim__free : Bits64 -> PrimIO () |
37 | 34 |
|
38 | | -||| Safe wrapper for cleanup |
| 35 | +||| Safe cleanup wrapper. |
39 | 36 | export |
40 | 37 | free : Handle -> IO () |
41 | 38 | free h = primIO (prim__free (handlePtr h)) |
42 | | - |
43 | | --------------------------------------------------------------------------------- |
44 | | --- Core Operations |
45 | | --------------------------------------------------------------------------------- |
46 | | - |
47 | | -||| Example operation: process data |
48 | | -export |
49 | | -%foreign "C:{{project}}_process, lib{{project}}" |
50 | | -prim__process : Bits64 -> Bits32 -> PrimIO Bits32 |
51 | | - |
52 | | -||| Safe wrapper with error handling |
53 | | -export |
54 | | -process : Handle -> Bits32 -> IO (Either Result Bits32) |
55 | | -process h input = do |
56 | | - result <- primIO (prim__process (handlePtr h) input) |
57 | | - pure $ case result of |
58 | | - 0 => Left Error |
59 | | - n => Right n |
60 | | - |
61 | | --------------------------------------------------------------------------------- |
62 | | --- String Operations |
63 | | --------------------------------------------------------------------------------- |
64 | | - |
65 | | -||| Convert C string to Idris String |
66 | | -export |
67 | | -%foreign "support:idris2_getString, libidris2_support" |
68 | | -prim__getString : Bits64 -> String |
69 | | - |
70 | | -||| Free C string |
71 | | -export |
72 | | -%foreign "C:{{project}}_free_string, lib{{project}}" |
73 | | -prim__freeString : Bits64 -> PrimIO () |
74 | | - |
75 | | -||| Get string result from library |
76 | | -export |
77 | | -%foreign "C:{{project}}_get_string, lib{{project}}" |
78 | | -prim__getResult : Bits64 -> PrimIO Bits64 |
79 | | - |
80 | | -||| Safe string getter |
81 | | -export |
82 | | -getString : Handle -> IO (Maybe String) |
83 | | -getString h = do |
84 | | - ptr <- primIO (prim__getResult (handlePtr h)) |
85 | | - if ptr == 0 |
86 | | - then pure Nothing |
87 | | - else do |
88 | | - let str = prim__getString ptr |
89 | | - primIO (prim__freeString ptr) |
90 | | - pure (Just str) |
91 | | - |
92 | | --------------------------------------------------------------------------------- |
93 | | --- Array/Buffer Operations |
94 | | --------------------------------------------------------------------------------- |
95 | | - |
96 | | -||| Process array data |
97 | | -export |
98 | | -%foreign "C:{{project}}_process_array, lib{{project}}" |
99 | | -prim__processArray : Bits64 -> Bits64 -> Bits32 -> PrimIO Bits32 |
100 | | - |
101 | | -||| Safe array processor |
102 | | -export |
103 | | -processArray : Handle -> (buffer : Bits64) -> (len : Bits32) -> IO (Either Result ()) |
104 | | -processArray h buf len = do |
105 | | - result <- primIO (prim__processArray (handlePtr h) buf len) |
106 | | - pure $ case resultFromInt result of |
107 | | - Just Ok => Right () |
108 | | - Just err => Left err |
109 | | - Nothing => Left Error |
110 | | - where |
111 | | - resultFromInt : Bits32 -> Maybe Result |
112 | | - resultFromInt 0 = Just Ok |
113 | | - resultFromInt 1 = Just Error |
114 | | - resultFromInt 2 = Just InvalidParam |
115 | | - resultFromInt 3 = Just OutOfMemory |
116 | | - resultFromInt 4 = Just NullPointer |
117 | | - resultFromInt _ = Nothing |
118 | | - |
119 | | --------------------------------------------------------------------------------- |
120 | | --- Error Handling |
121 | | --------------------------------------------------------------------------------- |
122 | | - |
123 | | -||| Get last error message |
124 | | -export |
125 | | -%foreign "C:{{project}}_last_error, lib{{project}}" |
126 | | -prim__lastError : PrimIO Bits64 |
127 | | - |
128 | | -||| Retrieve last error as string |
129 | | -export |
130 | | -lastError : IO (Maybe String) |
131 | | -lastError = do |
132 | | - ptr <- primIO prim__lastError |
133 | | - if ptr == 0 |
134 | | - then pure Nothing |
135 | | - else pure (Just (prim__getString ptr)) |
136 | | - |
137 | | -||| Get error description for result code |
138 | | -export |
139 | | -errorDescription : Result -> String |
140 | | -errorDescription Ok = "Success" |
141 | | -errorDescription Error = "Generic error" |
142 | | -errorDescription InvalidParam = "Invalid parameter" |
143 | | -errorDescription OutOfMemory = "Out of memory" |
144 | | -errorDescription NullPointer = "Null pointer" |
145 | | - |
146 | | --------------------------------------------------------------------------------- |
147 | | --- Version Information |
148 | | --------------------------------------------------------------------------------- |
149 | | - |
150 | | -||| Get library version |
151 | | -export |
152 | | -%foreign "C:{{project}}_version, lib{{project}}" |
153 | | -prim__version : PrimIO Bits64 |
154 | | - |
155 | | -||| Get version as string |
156 | | -export |
157 | | -version : IO String |
158 | | -version = do |
159 | | - ptr <- primIO prim__version |
160 | | - pure (prim__getString ptr) |
161 | | - |
162 | | -||| Get library build info |
163 | | -export |
164 | | -%foreign "C:{{project}}_build_info, lib{{project}}" |
165 | | -prim__buildInfo : PrimIO Bits64 |
166 | | - |
167 | | -||| Get build information |
168 | | -export |
169 | | -buildInfo : IO String |
170 | | -buildInfo = do |
171 | | - ptr <- primIO prim__buildInfo |
172 | | - pure (prim__getString ptr) |
173 | | - |
174 | | --------------------------------------------------------------------------------- |
175 | | --- Callback Support |
176 | | --------------------------------------------------------------------------------- |
177 | | - |
178 | | -||| Callback function type (C ABI) |
179 | | -public export |
180 | | -Callback : Type |
181 | | -Callback = Bits64 -> Bits32 -> Bits32 |
182 | | - |
183 | | -||| Register a callback |
184 | | -export |
185 | | -%foreign "C:{{project}}_register_callback, lib{{project}}" |
186 | | -prim__registerCallback : Bits64 -> AnyPtr -> PrimIO Bits32 |
187 | | - |
188 | | -||| Safe callback registration |
189 | | -export |
190 | | -registerCallback : Handle -> Callback -> IO (Either Result ()) |
191 | | -registerCallback h cb = do |
192 | | - result <- primIO (prim__registerCallback (handlePtr h) (believe_me cb)) |
193 | | - pure $ case resultFromInt result of |
194 | | - Just Ok => Right () |
195 | | - Just err => Left err |
196 | | - Nothing => Left Error |
197 | | - where |
198 | | - resultFromInt : Bits32 -> Maybe Result |
199 | | - resultFromInt 0 = Just Ok |
200 | | - resultFromInt _ = Just Error |
201 | | - |
202 | | --------------------------------------------------------------------------------- |
203 | | --- Utility Functions |
204 | | --------------------------------------------------------------------------------- |
205 | | - |
206 | | -||| Check if library is initialized |
207 | | -export |
208 | | -%foreign "C:{{project}}_is_initialized, lib{{project}}" |
209 | | -prim__isInitialized : Bits64 -> PrimIO Bits32 |
210 | | - |
211 | | -||| Check initialization status |
212 | | -export |
213 | | -isInitialized : Handle -> IO Bool |
214 | | -isInitialized h = do |
215 | | - result <- primIO (prim__isInitialized (handlePtr h)) |
216 | | - pure (result /= 0) |
0 commit comments