moonbit-c-binding

Create MoonBit bindings for C libraries using native FFI workflows.

40|3|Updated May 28, 2025
One-click install
npx skills add https://github.com/justjavac/moonbit-webview --skill moonbit-c-binding-justjavac
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: moonbit-c-binding
Source: https://github.com/justjavac/moonbit-webview/tree/main/.agents/skills/moonbit-c-binding
Command: npx skills add https://github.com/justjavac/moonbit-webview --skill moonbit-c-binding-justjavac

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill provides a comprehensive guide to writing MoonBit bindings for C libraries, enabling seamless interoperability between MoonBit and native C code.

Core Features & Use Cases

  • FFI Declarations: Learn how to declare extern "c" functions and map C types to MoonBit types.
  • C Stub Implementation: Understand how to write C stub files (.c) that interface with MoonBit.
  • Configuration: Configure moon.pkg for native builds, including native-stub and link.native.
  • Memory Management: Master ownership annotations (#borrow, #owned) and external object patterns for safe memory handling.
  • Error Handling & Validation: Implement callback trampolines, wrap C pointers, convert strings, and validate bindings with AddressSanitizer.
  • Use Case: Integrate a complex C library (e.g., a graphics rendering engine or a scientific computing library) into your MoonBit application by creating robust and safe FFI bindings.

Quick Start

Use the moonbit-c-binding skill to create MoonBit bindings for a C library by following the FFI Layer workflow.

Frequently Asked Questions about moonbit-c-binding

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write MoonBit C bindings for a native library?

MoonBit C bindings are created by declaring `extern "c"` functions, implementing C stubs with `moonbit.h`, and configuring `moon.pkg` for native builds. This FFI layer workflow maps C types to MoonBit types and handles the native interoperability safely.

How does memory management work for C FFI in MoonBit?

Memory management in MoonBit C FFI uses ownership annotations like `#borrow` and `#owned` to handle external objects safely. These annotations define whether MoonBit or the C library manages the lifecycle of allocated resources across the FFI boundary.

How do I configure moon.pkg for native C stubs?

Configuring `moon.pkg` for native C stubs involves specifying the `native-stub` field to point to your `.c` files and using `link.native` to link the required C libraries. This setup ensures the MoonBit compiler includes your C code during the native build process.

Can I use callbacks from C libraries in MoonBit?

Yes, you can use callbacks from C libraries in MoonBit by implementing callback trampolines. These trampolines bridge C function pointers back into MoonBit, allowing native C libraries to execute MoonBit functions asynchronously or during synchronous calls.

What is the best way to validate MoonBit C FFI bindings?

The best way to validate MoonBit C FFI bindings is by using AddressSanitizer. It detects memory leaks, buffer overflows, and use-after-free errors across the FFI boundary, ensuring your ownership annotations and pointer wrapping are correctly implemented.

What are common pitfalls when mapping C types to MoonBit?

Common pitfalls when mapping C types to MoonBit include mishandling string conversions, failing to wrap raw C pointers securely, and incorrectly assigning ownership annotations. The FFI workflow addresses these by using external object patterns and dedicated conversion routines.