Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ffi_util package for FFI related util & raw function type #1577

Closed
wants to merge 1 commit into from

Conversation

Guest0x0
Copy link
Contributor

When binding FFI in wasm/native backend, it is often necessary to pass a callback function to FFI, and sometimes the FFI function must be capture free, for example Unix signal handler.

This PR adds a new package @ffi_util and a new type @ffi_util.RawFunction[T]. @ffi_util.RawFunction[T] represents capture-free toplevel function of type T. Note that the definition in core is merely a placeholder, the raw function type need special treatment from the compiler. So it is not usable until the compiler part is released.

Copy link

‼️ This code review is generated by a bot. Please verify the content before trusting it.

⚠️ [Conflicting Type Definitions for RawFunction]
  • Category: Correctness
  • Code Snippet:
    ffi_util.mbti (line 4): pub type RawFunction
    raw_function.mbt (line 4): pub type RawFunction[T]
  • Recommendation: Remove the non-generic RawFunction declaration from ffi_util.mbti and keep only the generic RawFunction[T] in raw_function.mbt
  • Reasoning: The compiler will treat these as conflicting type declarations since they share the same name but have different type parameters. This would prevent the package from compiling correctly.
💡 [Incomplete Documentation for Specialized Type]
  • Category: Maintainability
  • Code Snippet: raw_function.mbt documentation comment
  • Recommendation: Expand documentation to include:
    /// Example: `let handler: RawFunction[(Int) -> Unit] = fn(...) {...}`
    /// Compiler will verify the function has no captured variables
    /// Only works with toplevel function definitions
    
  • Reasoning: The current documentation doesn't clarify usage patterns or enforcement mechanism for capture-free requirement, which is critical for a compiler-magic type.
⚠️ [Potential Package Structure Issue]
  • Category: Maintainability
  • Code Snippet: moon.pkg.json being empty
  • Recommendation: Add minimal package metadata like {"name": "ffi_util"}
  • Reasoning: While MoonBit might tolerate empty package files, explicit metadata improves discoverability and prevents potential tooling issues.

@bobzhang
Copy link
Contributor

bobzhang commented Feb 8, 2025

per offline discussion, let's make it builtin type

@bobzhang bobzhang closed this Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants