c

FFI Binding to C

moon add tonyfettes/c@0.7.8
Download zip
Version
0.7.8
License
Apache-2.0
Last updated
2 months ago
Downloads
10K

Dependencies

README

#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.

#
BytesLike

trait BytesLike

impl BytesLike for Bytes

#
Decref

trait Decref

impl Decref for FixedArray[T]
impl Decref for Bytes

#
IsNullable

pub(open) trait IsNullable {
null() -> Self
is_null(self : Self) -> Bool
}

#
Load

pub(open) trait Load {
load(Pointer[Self]) -> Self
}

impl Load for Byte
impl Load for Int
impl Load for Int16
impl Load for Int64
impl Load for UInt
impl Load for UInt16
impl Load for UInt64
impl Load for Float
impl Load for Double

#
Sizeof

pub(open) trait Sizeof {
sizeof() -> UInt64
}

impl Sizeof for Byte
impl Sizeof for Int
impl Sizeof for Int16
impl Sizeof for Int64
impl Sizeof for UInt
impl Sizeof for UInt16
impl Sizeof for UInt64
impl Sizeof for Float
impl Sizeof for Double

#
Store

pub(open) trait Store {
store(Pointer[Self], Self) -> Unit
}

impl Store for Byte
impl Store for Int
impl Store for Int16
impl Store for Int64
impl Store for UInt
impl Store for UInt16
impl Store for UInt64
impl Store for Float
impl Store for Double

#
Null

#external
pub type Null

#
Nullable

#deprecated("Nullable[T] can leak memory if T is a managed type. Define a null value for type `T` yourself instead.")
#external
pub type Nullable[_]

#
Nullable::is_null

fn[T] Nullable::is_null(self : Nullable[T]) -> Bool

#
Nullable::none

fn[T] Nullable::none() -> Nullable[T]

#
Nullable::of

fn[T] Nullable::of(option : T?) -> Nullable[T]

#
Nullable::some

fn[T] Nullable::some(value : T) -> Nullable[T]

#
Nullable::to

fn[T] Nullable::to(self : Nullable[T]) -> T?

#
Pointer

#alias(Ptr)
#external
pub type Pointer[_]

impl Compare for Pointer[T]
impl Eq for Pointer[T]
impl Load for Pointer[T]
impl Sizeof for Pointer[T]
impl Store for Pointer[T]

#
Pointer::add

fn[T : Sizeof] Pointer::add(self : Pointer[T], offset : UInt64) -> Pointer[T]

Adds an offset to a pointer and returns a new pointer.

Parameters:

  • pointer: The original pointer.
  • offset: The offset to add, in number of elements of type T.

Returns a new pointer that is offset from the original pointer by the specified number of elements.

Note: The offset is subject to overflow.

#
Pointer::cast

fn[T, U] Pointer::cast(self : Pointer[T]) -> Pointer[U]

#
Pointer::is_not_null

fn[T] Pointer::is_not_null(self : Pointer[T]) -> Bool

#
Pointer::is_null

fn[T] Pointer::is_null(self : Pointer[T]) -> Bool

#
Pointer::load

fn[T : Load] Pointer::load(self : Pointer[T]) -> T

#
Pointer::null

fn[T] Pointer::null() -> Pointer[T]

#
Pointer::offset

fn[T : Sizeof] Pointer::offset(self : Pointer[T], offset : Int64) -> Pointer[T]

Adds an offset to a pointer and returns a new pointer.

Parameters:

  • pointer: The original pointer.
  • offset: The offset to add, in number of elements of type T.

Returns a new pointer that is offset from the original pointer by the specified number of elements.

Note: The offset is of type Int64 and might be insufficient for very large offsets on some platforms.

#
Pointer::offset_from

fn[T : Sizeof] Pointer::offset_from(self : Pointer[T], other : Pointer[T]) -> Int64

#
Pointer::op_get

fn[T : Load + Sizeof] Pointer::op_get(self : Pointer[T], index : UInt64) -> T

#
Pointer::op_set

fn[T : Store + Sizeof] Pointer::op_set(self : Pointer[T], index : UInt64, value : T) -> Unit

#
Pointer::store

fn[T : Store] Pointer::store(self : Pointer[T], value : T) -> Unit

#
Pointer::sub

fn[T : Sizeof] Pointer::sub(self : Pointer[T], offset : UInt64) -> Pointer[T]

Subtracts an offset from a pointer and returns a new pointer.

Parameters:

  • pointer: The original pointer.
  • offset: The offset to subtract, in number of elements of type T.

Returns a new pointer that is offset from the original pointer by the specified number of elements.

Note: The offset is subject to overflow.

#
Pointer::to_uint64

fn[T] Pointer::to_uint64(self : Pointer[T]) -> UInt64

#
borrow_array

fn[T] borrow_array(array : FixedArray[T]) -> Pointer[T]

#
borrow_bytes

fn[T : BytesLike, R] borrow_bytes(byte_pointer : T, f : (Pointer[Byte]) -> R) -> R

#
borrow_bytes_2

fn[A : BytesLike, B : BytesLike, R] borrow_bytes_2(a : A, b : B, f : (Pointer[Byte], Pointer[Byte]) -> R) -> R

#
borrow_bytes_n

fn[A : BytesLike, R] borrow_bytes_n(array : FixedArray[A], f : (FixedArray[Pointer[Byte]]) -> R) -> R

#
borrow_ref

fn[T] borrow_ref(r : Ref[T]) -> Pointer[T]

#
decref

fn[T : Decref] decref(value : T) -> Unit

#
none

fn[T] none() -> Nullable[T]

#
null

let null : Null

#
return_array

fn[T] return_array(pointer : Pointer[T]) -> FixedArray[T]

#
return_ref

fn[T] return_ref(ptr : Pointer[T]) -> Ref[T]

#
sizeof

fn[T : Sizeof] sizeof(_ : (T) -> Unit) -> UInt64

#
unsafe_borrow

fn[T] unsafe_borrow(r : T) -> Pointer[Unit]

#
unsafe_return

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

#
with_array_borrowed

fn[T, R] with_array_borrowed(array : FixedArray[T], f : (Pointer[T]) -> R) -> R

#
with_ref_borrowed

fn[T, R] with_ref_borrowed(r : Ref[T], f : (Pointer[T]) -> R) -> R

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io