This is an experimental project exploring interoperability between MoonBit and modern systems programming languages.
This project started as an effort to implement an HTTP client in MoonBit. Instead of relying on traditional libcurl
-based solutions, this project takes a more exploratory approach with the following goals:
- Achieve complete independence from
libcurl
. - Implement the HTTP client using Zig's standard library.
- Explore integration patterns between MoonBit and C/Zig.
Zig was chosen over C for the underlying implementation due to several advantages:
- Dependency Management
- The C ecosystem often involves complex dependency management.
- This typically requires manual configuration of compilers (like GCC), library paths, and linking.
- In contrast, Zig offers a modern build system and a comprehensive standard library.
- Standard Library Capabilities
- Zig's standard library includes a built-in HTTP client implementation.
- This eliminates the need for external dependencies for core HTTP functionality.
- Interoperability
- Interoperability is achieved via the C ABI (Phase 1).
- Direct MoonBit - Zig interoperability
(Phase 2)(Implemented).
The key components are:
- MoonBit: The primary language for application development.
- Zig: Implements the underlying HTTP client logic.
C: Acts as the intermediate bridge (in Phase 1).
MoonBit -> C ABI -> Zig
MoonBit -> Zig
fn main {
// GET request
println(@http.curl_get("https://api.example.com"))
// POST request
println(@http.curl_post("https://api.example.com", "{'data': 'test'}"))
}
> ./zig-out/bin/moonbit_zig
No request url.
Usage:
moonbit_zig <url>
> ./zig-out/bin/moonbit_zig https://jsonplaceholder.typicode.com/todos/1
It Works! You've requested: https://jsonplaceholder.typicode.com/todos/1
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
The project uses Zig build system, controlled by moon
command:
moon build --target native
- Requires Zig 0.11.0 or higher
- Ensure MoonBit runtime environment is properly set up
- Currently supports macOS/aarch64 platform only
The current implementation has a significant limitation: the exception handling mechanism is incomplete. While direct function calls from MoonBit to Zig have been implemented, there are issues when handling exceptions thrown from the Zig side. Specifically, Zig's errors cannot currently be gracefully converted into MoonBit's exception handling mechanism.