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