ruchy-wasm
WebAssembly bindings for the Ruchy programming language.
Features
- 🌐 Browser-based compilation: Compile Ruchy to Rust directly in the browser
- ⚡ Fast validation: Real-time syntax checking for interactive editors
- 📚 Educational tools: Perfect for documentation and learning platforms
- 🔧 AST inspection: Parse and analyze Ruchy code structure
Installation
NPM
Building from Source
# Install wasm-pack
|
# Build the WASM package
# For bundler environments (webpack, vite, etc.)
Usage
ES Modules (Vite, Modern Browsers)
import init from 'ruchy-wasm';
// Initialize the WASM module
await ;
// Create a compiler instance
const compiler = ;
// Compile Ruchy to Rust
const ruchyCode = `
struct Person {
name: &str,
age: i32
}
fn main() {
let alice = Person { name: "Alice", age: 30 }
println(alice.name)
}
`;
try catch
Bundler (Webpack, Rollup)
import init from 'ruchy-wasm';
Syntax Validation
import from 'ruchy-wasm';
const compiler = ;
// Validate syntax
const isValid = compiler.;
console.log; // true
const isInvalid = compiler.;
console.log; // false
AST Inspection
import from 'ruchy-wasm';
const compiler = ;
// Get AST as JSON
const ast = compiler.;
console.log;
API Reference
RuchyCompiler
Constructor
const compiler = ;
Creates a new Ruchy compiler instance.
Methods
compile(source: string): string
Compiles Ruchy source code to Rust.
- Parameters:
source- Ruchy source code as a string - Returns: Transpiled Rust code as a string
- Throws: Parse or transpile errors
validate(source: string): boolean
Validates Ruchy syntax without compilation.
- Parameters:
source- Ruchy source code to validate - Returns:
trueif syntax is valid,falseotherwise
parse_to_json(source: string): string
Parses Ruchy code and returns AST as JSON.
- Parameters:
source- Ruchy source code to parse - Returns: AST representation as JSON string
- Throws: Parse errors
version: string
Returns the Ruchy compiler version.
Examples
Interactive Code Playground
Ruchy Playground
Ruchy to Rust Compiler
fn fibonacci(n) {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
Compile
Real-time Syntax Validation
import from 'ruchy-wasm';
const compiler = ;
const editor = document.;
editor.;
Building for Interactive Books
Perfect for embedding in documentation sites like the Ruchy Book:
// In your documentation build system
import from 'ruchy-wasm';
const compiler = ;
// Add "Try it" buttons to code blocks
document..;
Size Optimization
The WASM binary is optimized for size:
- opt-level = "s": Optimizes for binary size
- LTO enabled: Link-time optimization
- Stripped symbols: Removes debug information
- Typical size: ~500KB gzipped
Browser Compatibility
- ✅ Chrome/Edge 57+
- ✅ Firefox 52+
- ✅ Safari 11+
- ✅ All modern browsers with WASM support
License
Licensed under:
- MIT license (LICENSE-MIT)
Links
- Ruchy - Main repository
- Ruchy Book - Learn Ruchy
- crates.io - Rust package