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.
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 | Engine | Status |
|---|---|---|
js!{} | Boa | ✅ Working |
ts!{} | SWC + Boa | ✅ Working |
py!{} | Rhai | ✅ Working |
cuda!{} | - | 🚧 Reserved |
sql!{} | - | 🚧 Reserved |
#[poly_bridge] | - | ✅ Designed |
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.
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
For comprehensive guidance, refer to the following:
To explore Poly, run:
cargo run --example poly_runtime_demo
Compile the project with:
cargo build -p polyglot-macros -p polyglot-runtime
Poly adheres to several core principles:
let x: i32 = js!{ 1 + 2 };#[poly_bridge(javascript)].No comments yet.
Sign in to be the first to comment.