You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Real host bindings for JS/TS, PHP, and Go, plus a native Rust API, beyond the current stubs.
Current state: the four non-Python LangImpls are regex-based structural stubs (crates/citry_template_parser/src/lang/{js,php,go,rust}.rs); only Python is fully implemented (via python_safe_eval). packages/ contains only py.
Scope (per language): real expression parsing + variable tracking, code generation, and a host runtime consuming the compiler output.
Binding mechanisms (harvested from old TODO/v2_cross_lang.md)
JS/TS:wasm-bindgen + wasm-pack, with js-sys / web-sys for JS and Web-API types. Build per target: wasm-pack build --target web (browser) versus --target nodejs.
PHP and Go (C ABI): compile with crate-type = ["cdylib", "rlib"] and generate the C header with cbindgen; load via PHP's ext-ffi or Go's cgo.
Python: the existing PyO3 path.
Out of scope for now: becomes relevant once the Python version is established and users ask for other languages.
Calling Rust from Other Languages: PyO3, wasm-bindgen, and FFI
Overview
Yes, you can call Rust compiled binaries from other languages (JavaScript, PHP, Go), similar to how PyO3/maturin works for Python. The approaches vary by language, but they all ultimately use FFI (Foreign Function Interface) mechanisms.
JavaScript/TypeScript: WebAssembly (WASM)
Use WebAssembly with wasm-bindgen and wasm-pack (similar to PyO3 for Python).
Tools:
wasm-bindgen - Generates bindings between Rust and JavaScript
wasm-pack - Builds and packages Rust-generated WebAssembly
js-sys / web-sys - Bindings for JavaScript standard objects and Web APIs
Example:
use wasm_bindgen::prelude::*;#[wasm_bindgen]pubfnparse_template(input:&str) -> String{// Your parsing logicformat!("Parsed: {}", input)}
Build:
wasm-pack build --target web # For browsers
wasm-pack build --target nodejs # For Node.js
// You'd have to write this manually:constwasmModule=awaitWebAssembly.instantiateStreaming(fetch("module.wasm"));constmemory=wasmModule.instance.exports.memory;constparse_template=wasmModule.instance.exports.parse_template;// Manually allocate strings in WASM memory, call function,// read result from memory, free memory...
wasm-bindgen version:
#[wasm_bindgen]pubfnparse_template(input:&str) -> String{// wasm-bindgen handles all the WASM/JS interop automatically}
Comparison Table
Aspect
Manual FFI (C ABI)
PyO3
wasm-bindgen
FFI Layer
C ABI (shared library)
Python C API (C ABI)
WebAssembly ABI
Type Conversion
Manual
Automatic
Automatic
Memory Management
Manual
Automatic (ref counting)
Automatic (WASM memory)
Code Generation
None
Procedural macros
Build-time codegen
Safety
Unsafe (manual)
Safe Rust API
Safe Rust API
Boilerplate
Lots
Minimal
Minimal
The Key Insight
Yes—PyO3 and wasm-bindgen are essentially:
Code generators that create language-specific bindings
Type conversion layers (Rust types ↔ target language types)
Safety abstractions (exposing safe Rust APIs instead of unsafe FFI)
PyO3 flow:
Your Rust Code
↓
PyO3 procedural macros (code generation)
↓
Rust code that calls Python C API (FFI)
↓
Python extension module (.so/.pyd)
↓
Python imports and uses it
wasm-bindgen flow:
Your Rust Code
↓
wasm-bindgen (code generation)
↓
WASM module + JavaScript glue code
↓
JavaScript imports and uses it
Manual FFI flow:
Your Rust Code
↓
You manually write C-compatible functions
↓
Compile to shared library (.so/.dylib/.dll)
↓
Target language loads library via FFI
↓
You manually write bindings in target language
Why Use PyO3/wasm-bindgen Instead of Manual FFI?
Automatic type conversions (no manual CString, PyObject*, etc.)
In short: they are high-level interfaces that generate FFI bindings and handle the low-level details, making it much easier to call Rust from other languages.
Activity
JuroOravec commentedon Jul 1, 2026
Calling Rust from Other Languages: PyO3, wasm-bindgen, and FFI
Overview
Yes, you can call Rust compiled binaries from other languages (JavaScript, PHP, Go), similar to how PyO3/maturin works for Python. The approaches vary by language, but they all ultimately use FFI (Foreign Function Interface) mechanisms.
JavaScript/TypeScript: WebAssembly (WASM)
Use WebAssembly with
wasm-bindgenandwasm-pack(similar to PyO3 for Python).Tools:
wasm-bindgen- Generates bindings between Rust and JavaScriptwasm-pack- Builds and packages Rust-generated WebAssemblyjs-sys/web-sys- Bindings for JavaScript standard objects and Web APIsExample:
Build:
Use in JavaScript:
PHP: FFI Extension
Use PHP's FFI extension to call C-compatible Rust libraries.
Approach:
.so/.dll/.dylib) with C-compatible interfaceext-ffito load and call the libraryRust code:
PHP code:
Go: cgo
Use Go's
cgoto call C-compatible Rust libraries.Rust code:
Go code:
Comparison with PyO3
General Pattern: C ABI
For languages without high-level bindings (PHP, Go, etc.), compile Rust to a C-compatible shared library:
extern "C"and#[no_mangle]in Rustcdylib(shared library)cbindgento generate it)Cargo.toml:
Build:
cargo build --release # Produces: libyour_lib.so (Linux), libyour_lib.dylib (macOS), your_lib.dll (Windows)How PyO3 and wasm-bindgen Differ from Manual FFI
PyO3: High-Level Wrapper Around Python's C API
What PyO3 does:
.so/.pyd) via procedural macrosString↔ Pythonstr, RustVec↔ Pythonlist, etc.)Manual FFI equivalent:
PyO3 version:
wasm-bindgen: High-Level Wrapper Around WebAssembly Interface
What wasm-bindgen does:
String↔ JavaScriptstring, RustVec↔ JavaScriptArray, etc.)Manual WASM equivalent:
wasm-bindgen version:
Comparison Table
The Key Insight
Yes—PyO3 and wasm-bindgen are essentially:
PyO3 flow:
wasm-bindgen flow:
Manual FFI flow:
Why Use PyO3/wasm-bindgen Instead of Manual FFI?
CString,PyObject*, etc.)In short: they are high-level interfaces that generate FFI bindings and handle the low-level details, making it much easier to call Rust from other languages.