The project is very ALPHA right now, but it is generating some very basic bindings for Rust and C right now
Generate WebAssembly bindings to JavaSCript via js-wasm
for various languages:
cargo install js-bindgen
This project is able to take JavaScript API descriptions in yaml like the one below:
```yaml
```
And turn them into code.
js-bindgen --lang rust console.yaml
```rust
pub mod console { use js::*;
pub fn clear(){
let func = js!(r###"function(){
console.clear();
}"###);
func.invoke_0();
}
pub fn log(msg: &str){
let a0 = msg.as_ptr() as u32;
let a1 = msg.len() as u32;
let func = js!(r###"function(msgPtr,msgLen){
console.log(this.readUtf8FromMemory(msgPtr,msgLen));
}"###);
func.invoke_2(a0, a1);
}
... ```
js-bindgen --lang c console.yaml
```C
void consoleclear(){ static int fn; char *fncode = "function(){ console.clear(); }"; if(fn == 0){ fn = jsregisterfunction(fncode,jsstrlen(fncode)); } jsinvokefunction0(fn); }
void consolelog(char * msg){ static int fn; unsigned int a0 = (unsigned int)msg; unsigned int a1 = jsstrlen(msg); char *fncode = "function(msgPtr,msgLen){ console.log(this.readUtf8FromMemory(msgPtr,msgLen)); }"; if(fn == 0){ fn = jsregisterfunction(fncode,jsstrlen(fncode)); } jsinvokefunction_2(fn, a0, a1); }
... ```
Sometimes you may want to create a binding to code that doesn't exist and still have the power to generate libraries for many targets
yaml
- namespace: unicorn
functions:
makeUnicorns:
code: |
function() {
console.log("🦄🦄🦄")
}