Skip to content

Commit 3b78fb8

Browse files
committed
chore: add mandatory SPDX header and sync
1 parent bec97b9 commit 3b78fb8

2 files changed

Lines changed: 173 additions & 79 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-License-Identifier: PMPL-1.0-or-later
2+
# Invariant Path Scan: language-design-whitepaper.md
3+

src/abi/Foreign.idr

Lines changed: 170 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,217 @@
1-
-- SPDX-License-Identifier: PMPL-1.0-or-later
2-
-- SPDX-FileCopyrightText: 2025 Jonathan D.A. Jewell
3-
-- Foreign.idr - FFI declarations for WokeLang
1+
||| SPDX-License-Identifier: PMPL-1.0-or-later
2+
||| Foreign Function Interface Declarations for WOKELANG
3+
|||
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/
49

5-
module WokeLang.ABI.Foreign
10+
module Wokelang.ABI.Foreign
611

7-
import WokeLang.ABI.Types
8-
import WokeLang.ABI.Layout
12+
import Wokelang.ABI.Types
13+
import Wokelang.ABI.Layout
914

1015
%default total
1116

12-
-- | C string type
13-
public export
14-
CString : Type
15-
CString = Ptr String
17+
--------------------------------------------------------------------------------
18+
-- Library Lifecycle
19+
--------------------------------------------------------------------------------
1620

17-
-- | C int type
18-
public export
19-
CInt : Type
20-
CInt = Bits32
21+
||| Initialize the library
22+
||| Returns a handle to the library instance, or Nothing on failure
23+
export
24+
%foreign "C:wokelang_init, libwokelang"
25+
prim__init : PrimIO Bits64
2126

22-
-- | C long long type
23-
public export
24-
CLongLong : Type
25-
CLongLong = Bits64
27+
||| Safe wrapper for library initialization
28+
export
29+
init : IO (Maybe Handle)
30+
init = do
31+
ptr <- primIO prim__init
32+
pure (createHandle ptr)
2633

27-
-- | C double type
28-
public export
29-
CDouble : Type
30-
CDouble = Double
34+
||| Clean up library resources
35+
export
36+
%foreign "C:wokelang_free, libwokelang"
37+
prim__free : Bits64 -> PrimIO ()
38+
39+
||| Safe wrapper for cleanup
40+
export
41+
free : Handle -> IO ()
42+
free h = primIO (prim__free (handlePtr h))
3143

32-
-- ==== Interpreter Lifecycle ====
44+
--------------------------------------------------------------------------------
45+
-- Core Operations
46+
--------------------------------------------------------------------------------
3347

34-
-- | Create a new WokeLang interpreter
35-
-- Returns null on failure
36-
%foreign "C:woke_interpreter_new,libwokelang"
48+
||| Example operation: process data
3749
export
38-
woke_interpreter_new : PrimIO (Ptr InterpreterHandle)
50+
%foreign "C:wokelang_process, libwokelang"
51+
prim__process : Bits64 -> Bits32 -> PrimIO Bits32
3952

40-
-- | Free a WokeLang interpreter
41-
%foreign "C:woke_interpreter_free,libwokelang"
53+
||| Safe wrapper with error handling
4254
export
43-
woke_interpreter_free : Ptr InterpreterHandle -> PrimIO ()
55+
process : Handle -> Bits32 -> IO (Either Result Bits32)
56+
process h input = do
57+
result <- primIO (prim__process (handlePtr h) input)
58+
pure $ case result of
59+
0 => Left Error
60+
n => Right n
4461

45-
-- ==== Execution ====
62+
--------------------------------------------------------------------------------
63+
-- String Operations
64+
--------------------------------------------------------------------------------
4665

47-
-- | Execute WokeLang source code
48-
%foreign "C:woke_exec,libwokelang"
66+
||| Convert C string to Idris String
4967
export
50-
woke_exec : Ptr InterpreterHandle -> CString -> PrimIO CInt
68+
%foreign "support:idris2_getString, libidris2_support"
69+
prim__getString : Bits64 -> String
5170

52-
-- | Execute and get return value
53-
%foreign "C:woke_eval,libwokelang"
71+
||| Free C string
5472
export
55-
woke_eval : Ptr InterpreterHandle -> CString -> Ptr (Ptr ValueHandle) -> PrimIO CInt
73+
%foreign "C:wokelang_free_string, libwokelang"
74+
prim__freeString : Bits64 -> PrimIO ()
5675

57-
-- ==== Value Operations ====
76+
||| Get string result from library
77+
export
78+
%foreign "C:wokelang_get_string, libwokelang"
79+
prim__getResult : Bits64 -> PrimIO Bits64
5880

59-
-- | Free a WokeLang value
60-
%foreign "C:woke_value_free,libwokelang"
81+
||| Safe string getter
6182
export
62-
woke_value_free : Ptr ValueHandle -> PrimIO ()
83+
getString : Handle -> IO (Maybe String)
84+
getString h = do
85+
ptr <- primIO (prim__getResult (handlePtr h))
86+
if ptr == 0
87+
then pure Nothing
88+
else do
89+
let str = prim__getString ptr
90+
primIO (prim__freeString ptr)
91+
pure (Just str)
92+
93+
--------------------------------------------------------------------------------
94+
-- Array/Buffer Operations
95+
--------------------------------------------------------------------------------
6396

64-
-- | Get value type
65-
%foreign "C:woke_value_type,libwokelang"
97+
||| Process array data
6698
export
67-
woke_value_type : Ptr ValueHandle -> PrimIO CInt
99+
%foreign "C:wokelang_process_array, libwokelang"
100+
prim__processArray : Bits64 -> Bits64 -> Bits32 -> PrimIO Bits32
68101

69-
-- | Get integer from value
70-
%foreign "C:woke_value_as_int,libwokelang"
102+
||| Safe array processor
71103
export
72-
woke_value_as_int : Ptr ValueHandle -> Ptr CLongLong -> PrimIO CInt
104+
processArray : Handle -> (buffer : Bits64) -> (len : Bits32) -> IO (Either Result ())
105+
processArray h buf len = do
106+
result <- primIO (prim__processArray (handlePtr h) buf len)
107+
pure $ case resultFromInt result of
108+
Just Ok => Right ()
109+
Just err => Left err
110+
Nothing => Left Error
111+
where
112+
resultFromInt : Bits32 -> Maybe Result
113+
resultFromInt 0 = Just Ok
114+
resultFromInt 1 = Just Error
115+
resultFromInt 2 = Just InvalidParam
116+
resultFromInt 3 = Just OutOfMemory
117+
resultFromInt 4 = Just NullPointer
118+
resultFromInt _ = Nothing
73119

74-
-- | Get float from value
75-
%foreign "C:woke_value_as_float,libwokelang"
120+
--------------------------------------------------------------------------------
121+
-- Error Handling
122+
--------------------------------------------------------------------------------
123+
124+
||| Get last error message
76125
export
77-
woke_value_as_float : Ptr ValueHandle -> Ptr CDouble -> PrimIO CInt
126+
%foreign "C:wokelang_last_error, libwokelang"
127+
prim__lastError : PrimIO Bits64
78128

79-
-- | Get boolean from value
80-
%foreign "C:woke_value_as_bool,libwokelang"
129+
||| Retrieve last error as string
81130
export
82-
woke_value_as_bool : Ptr ValueHandle -> Ptr CInt -> PrimIO CInt
131+
lastError : IO (Maybe String)
132+
lastError = do
133+
ptr <- primIO prim__lastError
134+
if ptr == 0
135+
then pure Nothing
136+
else pure (Just (prim__getString ptr))
83137

84-
-- | Get string from value (must free with woke_string_free)
85-
%foreign "C:woke_value_as_string,libwokelang"
138+
||| Get error description for result code
86139
export
87-
woke_value_as_string : Ptr ValueHandle -> PrimIO CString
140+
errorDescription : Result -> String
141+
errorDescription Ok = "Success"
142+
errorDescription Error = "Generic error"
143+
errorDescription InvalidParam = "Invalid parameter"
144+
errorDescription OutOfMemory = "Out of memory"
145+
errorDescription NullPointer = "Null pointer"
146+
147+
--------------------------------------------------------------------------------
148+
-- Version Information
149+
--------------------------------------------------------------------------------
88150

89-
-- | Free string returned by woke_value_as_string
90-
%foreign "C:woke_string_free,libwokelang"
151+
||| Get library version
91152
export
92-
woke_string_free : CString -> PrimIO ()
153+
%foreign "C:wokelang_version, libwokelang"
154+
prim__version : PrimIO Bits64
93155

94-
-- ==== Value Creation ====
156+
||| Get version as string
157+
export
158+
version : IO String
159+
version = do
160+
ptr <- primIO prim__version
161+
pure (prim__getString ptr)
95162

96-
-- | Create integer value
97-
%foreign "C:woke_value_from_int,libwokelang"
163+
||| Get library build info
98164
export
99-
woke_value_from_int : CLongLong -> PrimIO (Ptr ValueHandle)
165+
%foreign "C:wokelang_build_info, libwokelang"
166+
prim__buildInfo : PrimIO Bits64
100167

101-
-- | Create float value
102-
%foreign "C:woke_value_from_float,libwokelang"
168+
||| Get build information
103169
export
104-
woke_value_from_float : CDouble -> PrimIO (Ptr ValueHandle)
170+
buildInfo : IO String
171+
buildInfo = do
172+
ptr <- primIO prim__buildInfo
173+
pure (prim__getString ptr)
174+
175+
--------------------------------------------------------------------------------
176+
-- Callback Support
177+
--------------------------------------------------------------------------------
178+
179+
||| Callback function type (C ABI)
180+
public export
181+
Callback : Type
182+
Callback = Bits64 -> Bits32 -> Bits32
105183

106-
-- | Create boolean value
107-
%foreign "C:woke_value_from_bool,libwokelang"
184+
||| Register a callback
108185
export
109-
woke_value_from_bool : CInt -> PrimIO (Ptr ValueHandle)
186+
%foreign "C:wokelang_register_callback, libwokelang"
187+
prim__registerCallback : Bits64 -> AnyPtr -> PrimIO Bits32
110188

111-
-- | Create string value
112-
%foreign "C:woke_value_from_string,libwokelang"
189+
||| Safe callback registration
113190
export
114-
woke_value_from_string : CString -> PrimIO (Ptr ValueHandle)
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
115202

116-
-- ==== Utility ====
203+
--------------------------------------------------------------------------------
204+
-- Utility Functions
205+
--------------------------------------------------------------------------------
117206

118-
-- | Get version string
119-
%foreign "C:woke_version,libwokelang"
207+
||| Check if library is initialized
120208
export
121-
woke_version : PrimIO CString
209+
%foreign "C:wokelang_is_initialized, libwokelang"
210+
prim__isInitialized : Bits64 -> PrimIO Bits32
122211

123-
-- | Get last error message
124-
%foreign "C:woke_last_error,libwokelang"
212+
||| Check initialization status
125213
export
126-
woke_last_error : PrimIO CString
214+
isInitialized : Handle -> IO Bool
215+
isInitialized h = do
216+
result <- primIO (prim__isInitialized (handlePtr h))
217+
pure (result /= 0)

0 commit comments

Comments
 (0)