This version of the module is deprecated: This package is deprecated. moonbit-community/ffi should be preferred.
MoonBit Foreign Function Interface.
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) -> String// Basic conversion
let c_str = b"Hello, world!\x00"
let moonbit_str = @ffi.from_cstr(c_str)
// moonbit_str equals "Hello, world!"
// UTF-8 text conversion
let utf8_c_str = b"Caf\xc3\xa9\x00" // "Café" in UTF-8 with null terminator
let utf8_str = @ffi.from_cstr(utf8_c_str)
// utf8_str equals "Café"
// Empty string conversion
let empty_c_str = b"\x00"
let empty_str = @ffi.from_cstr(empty_c_str)
// empty_str equals ""fn from_wstr_lossy(b : Bytes) -> String// Basic ASCII conversion
let wide_hello = b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x00\x00"
let str_hello = @ffi.from_wstr_lossy(wide_hello)
// str_hello equals "Hello"
// Unicode characters (Chinese text)
let wide_chinese = b"\x16\x4e\x96\x75\x00\x00" // "世界" in UTF-16LE
let str_chinese = @ffi.from_wstr_lossy(wide_chinese)
// str_chinese equals "世界"
// Empty string
let empty_wide = b"\x00\x00"
let empty_str = @ffi.from_wstr_lossy(empty_wide)
// empty_str equals ""
// Mixed content
let mixed = b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x2c\x00\x20\x00\x16\x4e\x96\x75\x21\x00\x00\x00"
let mixed_str = @ffi.from_wstr_lossy(mixed)
// mixed_str equals "Hello, 世界!"fn to_cstr(s : String) -> Bytes// Basic conversion
let moonbit_str = "Hello, world!"
let c_str = @ffi.to_cstr(moonbit_str)
// c_str equals b"Hello, world!\x00"
// Empty string conversion
let empty_str = ""
let empty_c_str = @ffi.to_cstr(empty_str)
// empty_c_str equals b"\x00"
// UTF-8 text conversion
let utf8_str = "Café"
let utf8_c_str = @ffi.to_cstr(utf8_str)
// utf8_c_str contains UTF-8 bytes: b"Caf\xc3\xa9\x00"
// Using with FFI functions
// extern "C" fn printf(format : Bytes, ...) -> Int = "printf"
// let message = "Hello from MoonBit!"
// let result = printf(@ffi.to_cstr("%s\n"), @ffi.to_cstr(message))fn to_wstr(s : String) -> Bytes// Basic ASCII conversion
let hello_str = "Hello"
let hello_wide = @ffi.to_wstr(hello_str)
// hello_wide contains: b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x00\x00"
// Unicode characters (Chinese text)
let chinese_str = "世界"
let chinese_wide = @ffi.to_wstr(chinese_str)
// chinese_wide contains UTF-16 encoded bytes for "世界" with null termination
// Empty string
let empty_str = ""
let empty_wide = @ffi.to_wstr(empty_str)
// empty_wide contains: b"\x00\x00"
// Mixed content with emojis
let mixed_str = "Hello, 世界! 🌍"
let mixed_wide = @ffi.to_wstr(mixed_str)
// mixed_wide contains UTF-16 encoded bytes with null termination
// Using with Windows API
// extern "C" fn CreateFileW(
// lpFileName : Bytes,
// dwDesiredAccess : Int,
// dwShareMode : Int,
// lpSecurityAttributes : Int64,
// dwCreationDisposition : Int,
// dwFlagsAndAttributes : Int,
// hTemplateFile : Int64
// ) -> Int64 = "CreateFileW"
//
// let file_handle = CreateFileW(
// @ffi.to_wstr("C:\\path\\to\\file.txt"),
// 0x80000000, // GENERIC_READ
// 1, // FILE_SHARE_READ
// 0, // No security attributes
// 3, // OPEN_EXISTING
// 0x80, // FILE_ATTRIBUTE_NORMAL
// 0 // No template file
// )