Poly is a polyglot macro system designed for type-safe cross-language programming in Rust. With embedded interpreters for JavaScript, TypeScript, and Python, it enables developers to write multi-language source files effortlessly. Experience the power of zero external dependencies while working with familiar syntax across different runtimes.
Poly is an innovative polyglot macro system designed to facilitate type-safe cross-language programming in Rust without any external dependencies. With Poly, developers can leverage multiple programming languages while maintaining the performance and safety characteristics inherent to Rust.
Key Features
- Cross-Language Programming: Write code in JavaScript, TypeScript, and Python seamlessly within Rust using embedded interpreters.
- Zero External Dependencies: No need for external runtimes like Python or Node.js—everything runs in pure Rust.
- Type Safety: Enjoy compile-time checking across different languages, ensuring greater reliability in code integration and execution.
Quick Start
Here's how you can use Poly to perform simple operations across multiple languages:
use polyglot_macros::{js, py, ts};
fn main() {
// JavaScript (using Boa engine, purely in Rust)
let sum: i32 = js!{ [1,2,3].reduce((a,b) => a+b, 0) };
// TypeScript (using SWC + Boa, all in Rust)
let typed: i32 = ts!{ const x: number = 5; x * 2 };
// Python scripting (using Rhai, also in Rust)
let result: i32 = py!{ let x = 10; x * 2 };
println!("{sum}, {typed}, {result}"); // Output: 6, 10, 20
}
Macro and Engine Status
| Macro | Engine | Status |
|---|---|---|
js!{} | Boa | ✅ Working |
ts!{} | SWC + Boa | ✅ Working |
py!{} | Rhai | ✅ Working |
cuda!{} | - | 🚧 Reserved |
sql!{} | - | 🚧 Reserved |
#[poly_bridge] | - | ✅ Designed |
Type-Safe FFI Bridge
Develop type-safe interfaces for external languages:
use polyglot_macros::poly_bridge;
#[poly_bridge(javascript)]
trait Calculator {
fn add(&self, a: i32, b: i32) -> i32;
}
// Automatically generates JsCalculator with type-checked methods.
The .poly Format
Poly allows for multi-language source files using the .poly extension:
// main.poly
import numpy as np // Python imports
rust {
let data = vec![1, 2, 3, 4, 5];
let doubled = py!{ (np.array(data) * 2).tolist() };
}
python {
def process(data):
return [x * 2 for x in data]
}
javascript {
const render = (data) => console.log(data);
}
Compile with:
polyglot compile main.poly --target wasm
polyglot watch main.poly # Enables hot reloading
Documentation
For comprehensive guidance, refer to the following:
Running Examples
To explore Poly, run:
cargo run --example poly_runtime_demo
Building the Project
Compile the project with:
cargo build -p polyglot-macros -p polyglot-runtime
Philosophy
Poly adheres to several core principles:
- Keep it Simple: Example of simplicity:
let x: i32 = js!{ 1 + 2 }; - Facilitate Complexity: Support for complex use cases via
#[poly_bridge(javascript)]. - Dependability: All interpreters operate within Rust, ensuring performance and reducing integration complexity.
- Ensuring Safety: Comprehensive compile-time checks across language interfaces.
No comments yet.
Sign in to be the first to comment.