A web component for loading for webidl wasm applications
npm install webidl-loader.webidl files using a generator tool inside this project.helloworld.wasm and call main:helloworld.html:
``html`
Here's a web assembly example to log to console using a Web IDL generated function console_log.
`c_cpp
extern void console_log(int msg);
int main() {
char *greeting = "Hello world!";
console_log(greeting);
return 0;
}
`
This is written using Poetry
helloworld.poem:`python
export_memory "memory"
import "env" "console_log" log 1 0
export "main" main
log (address_of "hello world")
`
Here's a rust version:
helloworld.rs:`rust
#![no_main]
use std::ffi::CString;
use std::os::raw::c_char;
extern "C" {
fn console_log(start: *mut c_char);
}
pub fn log(msg: &str) {
unsafe {
console_log(CString::new(msg).unwrap().into_raw());
}
}
#[no_mangle]
pub fn main() -> () {
log("hello world!");
}
`
All documented web IDL functions can be found in api docs
You can configure your web assembly module by using different attributes on your HTML tag. For instance if instead of main you have a function named start you want called on module load.
`html`
Attributes - the first function to be called on loading
Host Bindings
This project hopes to emulate how host bindings work in real web assembly as closely as possible. I'll have to make some assumptions, but if anyone knows better details I'd love to talk. Where I make assumptions, I'll try to be consistent.
For those who don't know what host bindings are, basically the future plan is to expose Web IDL in some way to web assembly in a standard way. But it's not been implemented yet. Maybe this can turn into a prototype.
https://webassembly.org/docs/future-features/
Creating Specialized Builds
This project was meant to expose all of web IDL as possible, but if you need a specialized build with only the pieces you need to reduce load time, you can make your own by only specifying the web IDL you want to generate or even hacking down some webIDL of the unnecessary
Here's an example with the minimal needed to draw canvas for instance:
`terminal
node tools/generate_webidl.js Console.webidl Window.webidl Document.webidl HTMLCanvasElement.webidl CanvasRenderingContext2D.webidl
``