MoonBit Foreign Function Interface.
Dependencies
moon update
moon add justjavac/ffi// Convert Bytes → String
let data = b"Hello, world!\x00"
let s = @ffi.from_cstr(data)
assert_eq!(s, "Hello, world!")
// Convert String → Bytes
let cstr = @ffi.to_cstr("Hello, world!")
assert_eq!(cstr, data)moon build
moon test --docfn from_cstr(b : Bytes) -> Stringlet s = from_cstr(b"Hello, world!\x00")
assert_eq!(s, "Hello, world!")fn from_wstr_lossy(b : Bytes) -> Stringlet s = from_wstr_lossy(b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x00\x00")
assert_eq!(s, "Hello")fn to_cstr(s : String) -> Byteslet cstr = to_cstr("Hello, world!")
assert_eq!(cstr, b"Hello, world!\x00")fn to_wstr(s : String) -> Byteslet wstr = to_wstr("Hello")
assert_eq!(wstr, b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x00\x00")MoonBit Foreign Function Interface.
Dependencies