Skip to content

WebAssembly Component Model Support for MoonBit Integration #304

@avrabe

Description

@avrabe

WebAssembly Component Model Support for MoonBit Integration

Issue Type

  • Feature Request
  • Bug Report
  • Documentation Request
  • Performance Issue
  • Security Concern

Summary

Add MoonBit-specific WebAssembly Component Model support to rules_wasm_component to enable seamless integration with rules_moonbit.

Context

  • rules_moonbit version: 0.1.0 (current development)
  • rules_wasm_component version: [target version]
  • MoonBit version: 0.6.33+
  • Target platforms: wasm, js, c, native

Feature Description

Overview

rules_moonbit needs to integrate with rules_wasm_component to provide WebAssembly Component Model support for MoonBit users. This requires MoonBit-specific enhancements to rules_wasm_component's Component Model infrastructure.

Required Features

1. MoonBit-Specific Component Creation

Requirement: Support MoonBit-generated WebAssembly modules in component creation

Details:

  • Accept MoonBit-compiled Wasm modules as input
  • Handle MoonBit-specific metadata and optimization flags
  • Provide MoonBit-optimized component creation

Example Usage:

wasm_component(
    name = "moonbit_component",
    srcs = [":moonbit_wasm"],  # MoonBit-generated Wasm
    wit_deps = [":math_wit"],
    moonbit_specific = True,  # Enable MoonBit optimizations
)

2. MoonBit WIT Integration

Requirement: MoonBit-compatible WIT file processing

Details:

  • Support MoonBit-specific WIT patterns
  • Generate MoonBit-compatible bindings
  • Validate MoonBit interface definitions

Example:

wasm_wit(
    name = "moonbit_wit",
    srcs = ["math.wit"],
    moonbit_compatible = True,  # MoonBit-specific validation
)

3. MoonBit Toolchain Integration

Requirement: MoonBit compiler detection and integration

Details:

  • Detect MoonBit compiler version
  • Support MoonBit-specific component flags
  • Provide MoonBit optimization profiles
  • Handle MoonBit metadata in components

Toolchain Requirements:

  • MoonBit compiler version detection
  • MoonBit-specific component creation flags
  • MoonBit optimization profiles (size, speed, aggressive)
  • MoonBit metadata handling in components

4. MoonBit Component Testing

Requirement: MoonBit-specific component testing support

Details:

  • MoonBit component validation patterns
  • MoonBit integration testing
  • MoonBit performance benchmarks

Example:

wasm_component_test(
    name = "moonbit_test",
    component = ":moonbit_component",
    wit_deps = [":math_wit"],
    moonbit_specific = True,  # MoonBit test patterns
)

Integration Requirements

rules_moonbit Integration Points

  1. Component Creation Delegation

    • rules_moonbit will delegate component creation to rules_wasm_component
    • Provide MoonBit-specific convenience rules
    • Maintain seamless user experience
  2. WIT File Support

    • rules_moonbit will use rules_wasm_component WIT processing
    • Add MoonBit-specific WIT validation
    • Provide MoonBit WIT examples
  3. Toolchain Integration

    • rules_moonbit will integrate with rules_wasm_component toolchain
    • Support MoonBit-specific component flags
    • Provide MoonBit optimization profiles
  4. Testing Integration

    • rules_moonbit will use rules_wasm_component testing infrastructure
    • Add MoonBit-specific test patterns
    • Provide MoonBit test examples

Expected Interfaces

Component Creation Interface:

def create_moonbit_component(
    ctx,
    wasm_src,      # MoonBit-generated Wasm module
    wit_deps,      # WIT interface dependencies
    moonbit_flags, # MoonBit-specific flags
):
    # Returns: Component output

WIT Processing Interface:

def process_moonbit_wit(
    ctx,
    wit_srcs,     # WIT source files
    moonbit_flags, # MoonBit-specific flags
):
    # Returns: Processed WIT interfaces

Toolchain Interface:

def get_moonbit_toolchain(
    ctx,
    version,      # MoonBit version
    platform,     # Target platform
):
    # Returns: MoonBit toolchain info

Technical Requirements

Toolchain Requirements

Required Tools:

  • wit-bindgen with MoonBit support
  • wasm-tools with MoonBit compatibility
  • MoonBit compiler version detection
  • MoonBit-specific component flags

Toolchain Configuration:

moonbit_toolchain = struct(
    wit_bindgen = "@wit_bindgen//:wit-bindgen",
    wasm_tools = "@wasm_tools//:wasm-tools",
    moonbit_compiler = "@moonbit//:moon",
    version = "0.6.33",
    supports_components = True,
)

Rule Requirements

Component Rule Enhancements:

wasm_component(
    name = "component",
    srcs = [":wasm"],
    wit_deps = [":wit"],
    moonbit_specific = True,  # NEW: MoonBit optimizations
    moonbit_flags = ["--optimize"],  # NEW: MoonBit flags
)

WIT Rule Enhancements:

wasm_wit(
    name = "wit",
    srcs = ["math.wit"],
    moonbit_compatible = True,  # NEW: MoonBit validation
    moonbit_flags = [],  # NEW: MoonBit flags
)

Testing Requirements

Test Rule Enhancements:

wasm_component_test(
    name = "test",
    component = ":component",
    wit_deps = [":wit"],
    moonbit_specific = True,  # NEW: MoonBit test patterns
)

Implementation Strategy

Phase 1: Research and Design (2 weeks)

Tasks:

  • Research MoonBit Component Model requirements
  • Design MoonBit-specific interfaces
  • Create integration architecture
  • Develop test plan

Deliverables:

  • MoonBit Component Model design document
  • Integration architecture specification
  • Test plan and requirements

Phase 2: Core Implementation (4 weeks)

Tasks:

  • Add MoonBit-specific component creation
  • Implement MoonBit WIT processing
  • Add MoonBit toolchain support
  • Create MoonBit test patterns

Deliverables:

  • MoonBit component creation rules
  • MoonBit WIT processing rules
  • MoonBit toolchain integration
  • MoonBit test infrastructure

Phase 3: Integration and Testing (2 weeks)

Tasks:

  • Integrate with rules_moonbit
  • Create joint test suite
  • Develop integration examples
  • Performance optimization

Deliverables:

  • Working integration with rules_moonbit
  • Comprehensive test suite
  • Integration examples
  • Performance benchmarks

Phase 4: Documentation and Release (2 weeks)

Tasks:

  • Create MoonBit Component Model guide
  • Develop integration documentation
  • Write examples and tutorials
  • Prepare release notes

Deliverables:

  • MoonBit Component Model documentation
  • Integration guide with rules_moonbit
  • Complete examples and tutorials
  • Release documentation

Success Criteria

Technical Success

  • MoonBit components can be created using rules_wasm_component
  • rules_moonbit integrates seamlessly with rules_wasm_component
  • Component creation works across all supported platforms
  • Performance meets expectations (<20% overhead vs core Wasm)
  • All tests pass with good coverage

User Success

  • Clear documentation for MoonBit Component Model
  • Smooth integration with rules_moonbit
  • Good developer experience with clear error messages
  • Active community adoption
  • Positive feedback from users

Ecosystem Success

  • rules_wasm_component becomes standard for Component Model
  • rules_moonbit provides excellent MoonBit integration
  • WebAssembly Component Model ecosystem grows
  • Cross-language Component Model support improves

Example Implementation

MoonBit Component Creation

WIT Interface (math.wit):

package example:math;

interface math {
    add: func(a: u32, b: u32) -> u32;
    subtract: func(a: u32, b: u32) -> u32;
}

world calculator {
    export math;
}

rules_moonbit BUILD.bazel:

load("//moonbit:defs.bzl", "moonbit_wasm")

moonbit_wasm(
    name = "math_wasm",
    srcs = ["math.mbt"],
)

rules_wasm_component BUILD.bazel:

load("@rules_wasm_component//wasm:defs.bzl", "wasm_component", "wasm_wit")

wasm_wit(
    name = "math_wit",
    srcs = ["math.wit"],
    moonbit_compatible = True,
)

wasm_component(
    name = "math_component",
    srcs = [":math_wasm"],
    wit_deps = [":math_wit"],
    moonbit_specific = True,
)

Integrated rules_moonbit BUILD.bazel:

load("//moonbit:defs.bzl", "moonbit_component")

moonbit_component(
    name = "math_component",
    srcs = ["math.mbt"],
    wit_deps = [":math_wit"],
)

Testing Requirements

Test Coverage Needed

  1. Component Creation Tests

    • MoonBit Wasm to Component conversion
    • MoonBit-specific optimization validation
    • Component validation and metadata
  2. WIT Processing Tests

    • MoonBit WIT file validation
    • MoonBit binding generation
    • MoonBit interface compatibility
  3. Toolchain Tests

    • MoonBit compiler detection
    • MoonBit-specific flags handling
    • MoonBit optimization profiles
  4. Integration Tests

    • rules_moonbit to rules_wasm_component integration
    • Component creation workflow
    • Error handling and diagnostics

Example Test

wasm_component_test(
    name = "math_component_test",
    component = ":math_component",
    wit_deps = [":math_wit"],
    moonbit_specific = True,
)

Documentation Requirements

rules_wasm_component Documentation

Required Documents:

  1. MoonBit Component Model Guide

    • Overview of MoonBit Component Model support
    • MoonBit-specific features and optimizations
    • Integration with rules_moonbit
  2. MoonBit WIT Processing

    • MoonBit WIT file patterns
    • MoonBit binding generation
    • MoonBit interface validation
  3. MoonBit Toolchain Configuration

    • MoonBit compiler setup
    • MoonBit-specific flags
    • MoonBit optimization profiles
  4. MoonBit Testing Guide

    • MoonBit component testing patterns
    • MoonBit integration testing
    • MoonBit performance benchmarks

Joint Documentation

Required Documents:

  1. Integration Guide

    • How rules_moonbit integrates with rules_wasm_component
    • Component creation workflow
    • Troubleshooting and FAQ
  2. Migration Guide

    • Moving from core Wasm to Component Model
    • Upgrading existing MoonBit projects
    • Best practices for Component Model
  3. Examples and Tutorials

    • Complete working examples
    • Step-by-step tutorials
    • Advanced usage patterns

Performance Considerations

Build Performance

  • Expected Overhead: 15-25% vs core Wasm compilation
  • Mitigation Strategies:
    • Aggressive caching of component creation
    • Incremental component updates
    • Parallel component processing

Runtime Performance

  • Expected Overhead: 5-10% vs core Wasm execution
  • Mitigation Strategies:
    • MoonBit-specific optimization profiles
    • Component-specific optimization flags
    • Runtime-aware component creation

Benchmarking Requirements

  • Component creation time
  • Component size
  • Runtime performance
  • Memory usage
  • Startup time

Security Considerations

Security Requirements

  1. Checksum Verification: All component tools must be checksum-verified
  2. Toolchain Security: Secure handling of MoonBit toolchain
  3. Component Validation: Validate all MoonBit components
  4. WIT Security: Secure processing of WIT files
  5. Binding Safety: Safe generation of MoonBit bindings

Security Implementation

  • Checksum verification for all downloads
  • Secure toolchain management
  • Component validation and signing
  • WIT file validation and sanitization
  • Safe binding generation patterns

Risks and Mitigations

Technical Risks

  1. MoonBit Component Support Maturity

    • Mitigation: Work with MoonBit team, implement compatibility layers
  2. Integration Complexity

    • Mitigation: Start with simple integration, add complexity gradually
  3. Performance Overhead

    • Mitigation: Implement caching, optimize build process

Operational Risks

  1. Cross-Repo Coordination

    • Mitigation: Regular sync meetings, clear interfaces
  2. Testing Complexity

    • Mitigation: Focus on integration testing, use CI/CD
  3. User Adoption

    • Mitigation: Provide clear documentation, migration guides

Next Steps

For rules_wasm_component Team

  1. Review and prioritize this feature request
  2. Assign resources for implementation
  3. Create detailed implementation plan
  4. Implement MoonBit-specific features
  5. Develop testing infrastructure

For rules_moonbit Team

  1. Prepare integration layer
  2. Create convenience rules
  3. Develop documentation and examples
  4. Test integration thoroughly
  5. Gather community feedback

Joint Activities

  1. Regular coordination meetings
  2. Joint testing and validation
  3. Documentation review
  4. Community outreach
  5. Performance optimization

Additional Information

References

  • MoonBit Component Model Documentation: [link]
  • rules_wasm_component Documentation: [link]
  • WebAssembly Component Model Specification: [link]
  • wit-bindgen Documentation: [link]

Related Issues

  • rules_moonbit#123: Multi-target compilation support
  • rules_wasm_component#456: General Component Model enhancements
  • rules_wasm_component#789: Toolchain management improvements

Priority

  • High: This feature is critical for MoonBit WebAssembly Component Model support
  • Impact: Enables MoonBit users to leverage modern WebAssembly features
  • Dependencies: Requires coordination between rules_moonbit and rules_wasm_component teams

Conclusion

This feature request outlines a comprehensive plan for adding MoonBit-specific WebAssembly Component Model support to rules_wasm_component. By implementing these features, rules_wasm_component will enable rules_moonbit to provide excellent Component Model support to MoonBit users while maintaining a clean separation of concerns and avoiding duplication.

The proposed approach leverages rules_wasm_component's expertise in Component Model tooling while allowing rules_moonbit to focus on MoonBit language-specific features. This collaboration will result in a robust, user-friendly solution that positions both rulesets as leaders in their respective domains.

Expected Benefits:

  • MoonBit users get excellent Component Model support
  • rules_wasm_component becomes the standard for Component Model tooling
  • rules_moonbit provides seamless MoonBit integration
  • WebAssembly Component Model ecosystem grows and matures

Next Steps:

  1. Review and prioritize this feature request
  2. Assign resources for implementation
  3. Create detailed implementation plan
  4. Begin implementation with regular coordination
  5. Develop comprehensive testing and documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions