webidl-bindgen.mbt
v0.3.3
Published
WebIDL to MoonBit binding generator
Readme
webidl-bindgen.mbt
WebIDL to MoonBit binding generator.
Features
- Parse WebIDL files and generate MoonBit FFI bindings
- Fetch WebIDL directly from W3C specs via
@webref/idl - Generate type-safe bindings for interfaces, dictionaries, enums, and typedefs
- Support for all 300+ W3C/WHATWG specifications
Installation
npm install -g webidl-bindgen.mbt
# or
bun add -g webidl-bindgen.mbtUsage
Generate bindings for all W3C specs
webidl-bindgen.mbt --all --per-spec -o ./src/Single spec
webidl-bindgen.mbt --spec html -o ./src/Multiple specs
webidl-bindgen.mbt --specs html,dom,cssom,fetch -o ./src/From local WebIDL file
webidl-bindgen.mbt --input canvas2d.webidl -o ./src/Options
-i, --input <file> Input WebIDL file
-s, --spec <name> Single spec from @webref/idl (e.g., html)
--specs <list> Comma-separated list of specs
-a, --all Load all specs from @webref/idl
--per-spec Output each spec to separate file (with --all or --specs)
-o, --output <dir> Output directory (default: ./)
-h, --help Show this help messageNote: --input, --spec, --specs, and --all are mutually exclusive.
Generated Code
The generator produces MoonBit code with:
pub typedeclarations for interfaces and callback interfacespub extern "js" fnfor methods and attributespub enumfor WebIDL enums withto_string/from_stringhelperspub structfor dictionaries withdefault()andto_js()methodspub typealiases for typedefs
Example Output
/// HTMLElement interface
pub type HTMLElement
/// Get the inner text
pub extern "js" fn HTMLElement::inner_text(self : HTMLElement) -> String =
#| (self) => self.innerText
/// Set the inner text
pub extern "js" fn HTMLElement::set_inner_text(self : HTMLElement, value : String) -> Unit =
#| (self, value) => { self.innerText = value }Dependencies
- webidl2 - WebIDL parser
- @webref/idl - W3C spec IDL collection
Known Limitations
Currently not supported (planned):
- Constructor generation (
new Blob()etc.) - Inheritance (parent interface methods)
- Static methods (
URL.createObjectURL()) - Special getter/setter/deleter (indexed access)
License
MIT
