Sign in

    c

    FFI Binding to C

    Download zip
    Version
    0.7.8
    License
    Apache-2.0
    Last updated
    4 months ago
    Downloads
    10K

    #tonyfettes/c

    FFI Binding to C.

    #Usage

    1. Add this library using moon add:

      moon add tonyfettes/c

    2. Import the library in your moon.pkg.json:

      { "import": [ "tonyfettes/c" ] }

    3. Insert the following code somewhere in your MoonBit code:

      fn init {
      ignore(@c.moonbit_ffi_make_closure)
      }

      This prevents the closure help function from being eliminated by the MoonBit compiler.

    4. Use the library in your code.

    Pointer

    A generic pointer type that wraps the underlying platform pointer type.

    Pointer[T] represents a pointer to a value of type T. This type is used for low-level memory operations and interoperability with C code.

    Ptr

    using @tonyfettes/c/pointer { type Pointer as Ptr }

    A generic pointer type that wraps the underlying platform pointer type.

    Pointer[T] represents a pointer to a value of type T. This type is used for low-level memory operations and interoperability with C code.

    Ptrdiff

    Size

    using @tonyfettes/c/stddef { type Size }

    atexit

    fn atexit(func : FuncRef[() -> Unit]) -> Int

    borrow

    fn[T :
    Borrow
    , R] borrow(value : T, f : (
    Pointer
    [Unit]) -> R raise?) -> R raise?

    Borrows a pointer from a value that implements the Borrow trait.

    The value is temporarily converted to a pointer, passed to the function, and then restored. This ensures the value remains valid after the operation.

    Parameters:

    • value: The value to borrow from (must implement Borrow)
    • f: The function to call with the borrowed pointer

    Returns the result of calling f.

    borrow_array

    fn[T, R] borrow_array(array : FixedArray[T], f : (
    Pointer
    [T]) -> R raise?) -> R raise?

    Borrows a pointer from a fixed array for the duration of a function call.

    The array is temporarily converted to a pointer, passed to the function, and then restored. This ensures the array remains valid after the operation.

    Parameters:

    • array: The fixed array to borrow from
    • f: The function to call with the borrowed pointer

    Returns the result of calling f.

    borrow_bytes

    fn[R] borrow_bytes(bytes : Bytes, f : (
    Pointer
    [Byte]) -> R raise?) -> R raise?

    Borrows a pointer from a bytes buffer for the duration of a function call.

    The bytes buffer is temporarily converted to a pointer, passed to the function, and then restored. This ensures the buffer remains valid after the operation.

    Parameters:

    • bytes: The bytes buffer to borrow from
    • f: The function to call with the borrowed pointer

    Returns the result of calling f.

    borrow_ref

    fn[T, R] borrow_ref(ref_ :
    Ref
    [T], f : (
    Pointer
    [T]) -> R raise?) -> R raise?

    Borrows a pointer from a reference for the duration of a function call.

    The reference is temporarily converted to a pointer, passed to the function, and then restored. This ensures the reference remains valid after the operation.

    Parameters:

    • ref_: The reference to borrow from
    • f: The function to call with the borrowed pointer

    Returns the result of calling f.

    calloc

    Allocates and zeroes memory on the heap.

    This function is a wrapper around the C calloc function. It allocates memory for an array of n elements, each of size bytes, and initializes all bytes to zero.

    Parameters:
    • n: The number of elements to allocate
    • size: The size of each element in bytes

    Returns a pointer to the allocated and zeroed memory, or a null pointer if allocation fails.

    Note: The caller is responsible for freeing the allocated memory using free.

    exit

    fn[T] exit(status : Int) -> T

    exit_failure

    let exit_failure : Int

    exit_success

    let exit_success : Int

    free

    fn[T] free(ptr :
    Pointer
    [T]) -> Unit

    Frees previously allocated memory.

    This function is a wrapper around the C free function. It deallocates the memory block pointed to by ptr, which must have been previously allocated by malloc, calloc, or realloc.

    Parameters:
    • ptr: A pointer to the memory block to free

    Note: After calling free, the pointer becomes invalid and should not be used. Freeing a null pointer is safe and does nothing. Freeing the same pointer twice or freeing a pointer not returned by allocation functions leads to undefined behavior.

    malloc

    Allocates memory on the heap.

    This function is a wrapper around the C malloc function. It allocates a block of memory of the specified size and returns a pointer to it. The memory is not initialized.

    Parameters:
    • size: The number of bytes to allocate

    Returns a pointer to the allocated memory, or a null pointer if allocation fails.

    Note: The caller is responsible for freeing the allocated memory using free.

    memcpy

    Copies memory from source to destination.

    This function is a wrapper around the C memcpy function. It copies len bytes from the memory area src to the memory area dst. The memory areas must not overlap (use memmove for overlapping regions).

    Parameters:
    • dst: The destination memory location
    • src: The source memory location
    • len: The number of bytes to copy

    Note: If the memory regions overlap, the behavior is undefined. Both dst and src must be valid memory locations with at least len bytes accessible.

    memset

    fn[Dst] memset(dst : Dst, val : Byte, len :
    Size
    ) -> Unit

    Fills a memory block with a constant byte value.

    This function is a wrapper around the C memset function. It fills the first len bytes of the memory area pointed to by dst with the constant byte val.

    Parameters:
    • dst: The destination memory location to fill
    • val: The byte value to fill with
    • len: The number of bytes to fill

    Note: The destination memory must have at least len bytes accessible.

    null

    Creates a null pointer of type T.

    Returns a null pointer that points to no valid memory location.

    rand

    fn rand() -> Int

    rand_max

    let rand_max : Int

    realloc

    Reallocates a previously allocated memory block.

    This function is a wrapper around the C realloc function. It changes the size of the memory block pointed to by ptr to size bytes. The contents of the memory block are preserved up to the minimum of the old and new sizes.

    Parameters:
    • ptr: A pointer to previously allocated memory (or null)
    • size: The new size in bytes

    Returns a pointer to the reallocated memory, which may be different from ptr, or a null pointer if reallocation fails (in which case the original block is unchanged).

    Note: If ptr is null, this behaves like malloc. If size is 0, this may behave like free (platform-dependent).

    srand

    fn srand(seed : UInt) -> Unit

    strcmp

    fn[Str1 :
    Borrow
    , Str2 :
    Borrow
    ] strcmp(s1 : Str1, s2 : Str2) -> Int

    system

    fn[T :
    Borrow
    ] system(command : T) -> Int

    unsafe_borrow

    fn[T, R] unsafe_borrow(value : T, f : (
    Pointer
    [Unit]) -> R raise?) -> R raise?

    Unsafely borrows a pointer from any value for the duration of a function call.

    Warning: This is an unsafe operation. The value is temporarily converted to a pointer and restored after the function call, but this may lead to undefined behavior if not used carefully.

    Parameters:

    • value: The value to borrow from
    • f: The function to call with the borrowed pointer

    Returns the result of calling f.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io