Skip to content

Commit 72fac28

Browse files
committed
Add Moonwave annotation partially (#243)
1 parent 5002088 commit 72fac28

File tree

2 files changed

+135
-10
lines changed

2 files changed

+135
-10
lines changed

crates/lune-std-ffi/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ Implememt type-casting for all CTypes
8585

8686
**Trait `FfiData`:** Provide common data handle, including methods below
8787

88-
- **Method `check_boundary`:** check boundary with offset and size
89-
- **Method `get_pointer`:** returns raw pointer `*mut ()`
88+
- **Method `check_inner_boundary`:** check boundary with offset and size
89+
- **Method `get_inner_pointer`:** returns raw pointer `*mut ()`
9090
- **Method `is_writable`**
9191
- **Method `is_readable`**
9292

@@ -102,4 +102,4 @@ Implememt type-casting for all CTypes
102102
- [**Mod `libffi_helper.rs`:**](./src/ffi/libffi_helper.rs)
103103
- **Const `FFI_STATUS_NAMES`:** Used for ffi_status stringify
104104
- **Function `get_ensured_size`:** Returns ensured ffi_type size
105-
- **Const `SIEE_OF_POINTER`:** Platform specific pointer size (Compile time known)
105+
- **Const `SIZE_OF_POINTER`:** Platform specific pointer size (Compile time known)

types/ffi.luau

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export type CPtrInfo<T> = {
2222
inner: T,
2323

2424
-- subtype
25-
-- FIXME: recursive types; 'any' should be CPtrInfo
25+
-- FIXME: recursive types; result 'any' should be CArrInfo<CPtrInfo<T>>
2626
arr: (self: CPtrInfo<T>, len: number) -> any,
27+
-- FIXME: recursive types; result 'any' should be CPtrInfo<CPtrInfo<T>>
2728
ptr: (self: CPtrInfo<T>) -> any,
2829

2930
readRef: (self: CPtrInfo<T>, target: (Ref|Box), offset: number?) -> Ref,
@@ -36,12 +37,12 @@ export type CArrInfo<T, R> = {
3637
inner: T,
3738

3839
-- subtype
39-
ptr: (self: CArrInfo<T, R>) -> CPtrInfo<T>,
40+
ptr: (self: CArrInfo<T, R>) -> CPtrInfo<CArrInfo<T, R>>,
4041

4142
-- realize
4243
box: (self: CArrInfo<T, R>, table: { T }) -> Box,
4344
readData: (self: CArrInfo<T, R>, target: (Ref|Box), offset: number?) -> { T },
44-
writeData: (self: CArrInfo<T, R>, target: (Ref|Box), value: { T }, offset: number?) -> (),
45+
writeData: (self: CArrInfo<T, R>, target: (Ref|Box), value: { R }, target_offset: number?) -> (),
4546
copyData: (self: CArrInfo<T, R>, dst: (Ref|Box), src: (Ref|Box), dst_offset: number?, src_offset: number?) -> (),
4647

4748
offset: (self: CArrInfo<T, R>, index: number) -> number,
@@ -145,20 +146,82 @@ export type Ref = {
145146
isNull: (self: Ref) -> boolean,
146147
}
147148

149+
--[=[
150+
@class Box
151+
152+
A user manageable heap memory
153+
]=]
148154
export type Box = {
155+
--[=[
156+
@within Box
157+
@tag Field
158+
@field size
159+
160+
Size of the box.
161+
]=]
149162
size: number,
150163

164+
--[=[
165+
@within Box
166+
@tag Method
167+
@method zero
168+
169+
Fill the box with zero.
170+
171+
@return `Box` itself for convenience
172+
]=]
151173
zero: (self: Box) -> Box,
174+
--[=[
175+
@within Box
176+
@tag Method
177+
@method leak
178+
179+
Create a reference of the box after leaking it.
180+
181+
GC doesn't manage destruction after this action. You must free it later
182+
183+
@return A reference of the box
184+
]=]
152185
leak: (self: Box, offset: number?) -> Ref,
186+
--[=[
187+
@within Box
188+
@tag Method
189+
@method ref
190+
191+
Create a reference of the box.
192+
193+
@return A reference of the box
194+
]=]
153195
ref: (self: Box, offset: number?) -> Ref,
154196
}
155197

198+
--[=[
199+
@class Lib
200+
201+
A dynamic opened library handle
202+
]=]
156203
export type Lib = {
204+
--[=[
205+
@within Lib
206+
@tag Method
207+
@method find
208+
209+
Find a symbol from the dynamic library.
210+
211+
@param sym The name of the symbol
212+
@return A `Ref` of the found symbol
213+
]=]
157214
find: (self: Lib, sym: string) -> Ref,
158215
}
159216

160217
-- export type AppliedCallable = ()->()
161218

219+
--[=[
220+
@class Callable
221+
@tag unsafe
222+
223+
A callable external function
224+
]=]
162225
export type Callable = (ret: (Ref|Box)?, ...Ref)->() & {
163226
-- apply: (self: Callable, args: Args)->AppliedCallable,
164227
}
@@ -167,6 +230,12 @@ export type Closure = {
167230
ref: (self: Closure)->Ref,
168231
}
169232

233+
--[=[
234+
@class C
235+
@within FFI
236+
237+
Namespace for compile time sized c types.
238+
]=]
170239
local c = {}
171240

172241
c.char = {} :: char
@@ -185,14 +254,41 @@ c.ulonglong = {} :: ulonglong
185254

186255
c.void = {} :: CVoidInfo
187256

257+
--[=[
258+
@within C
259+
260+
Create a function signature type information.
261+
262+
@param args An array of CTypes represents the arguments of the function
263+
@param ret The return type of the function
264+
@return A function signature type information
265+
]=]
188266
function c.fn(args: { CTypes }, ret: CTypes): CFnInfo
189267
return nil :: any
190268
end
191269

192-
function c.struct(inner: { CTypes }): CStructInfo
270+
--[=[
271+
@within C
272+
273+
Create a struct type information.
274+
275+
@param fields An array of CTypes represents the fields of the struct
276+
@return A struct type information
277+
]=]
278+
function c.struct(fields: { CTypes }): CStructInfo
193279
return nil :: any
194280
end
195281

282+
--[=[
283+
@class FFI
284+
285+
Built-in library for foreign function interface
286+
287+
### Example usage
288+
289+
```lua
290+
```
291+
]=]
196292
local ffi = {}
197293

198294
ffi.c = c
@@ -212,22 +308,51 @@ ffi.f64 = {} :: f64
212308
ffi.usize = {} :: usize
213309
ffi.isize = {} :: isize
214310

311+
--[=[
312+
@within FFI
313+
314+
Create a `Ref` with address 0.
315+
316+
Can be used for receive a pointer from external function or pass it as an argument.
317+
318+
@return A zero initialized Ref
319+
]=]
215320
function ffi.nullRef(): Ref
216321
return nil :: any
217322
end
218323

324+
--[=[
325+
@within FFI
326+
327+
Create a `Box` with specific size.
328+
329+
@param size The size of the new box
330+
@return A allocated box
331+
]=]
219332
function ffi.box(size: number): Box
220333
return nil :: any
221334
end
222335

336+
--[=[
337+
@within FFI
338+
339+
Open a dynamic library.
340+
341+
@param name The name of the target library
342+
@return A dynamic library handle
343+
]=]
223344
function ffi.open(name: string): Lib
224345
return nil :: any
225346
end
226347

227-
function ffi.uninitRef(): Ref
228-
return nil :: any
229-
end
348+
--[=[
349+
@within FFI
350+
351+
Return `true` if the second argument is an integer (i32)
230352
353+
@param val A lua value to check
354+
@return Whether val is an integer or not
355+
]=]
231356
function ffi.isInteger<T>(val: T): boolean
232357
return nil :: any
233358
end

0 commit comments

Comments
 (0)