Skip to content

Commit

Permalink
js: Take a component as input, not interfaces
Browse files Browse the repository at this point in the history
This commit is the implementation of bytecodealliance#314 for the JS host generator.
The commit here covers basically everything in that issue for JS except
it generates a slightly different structure of the JS output. Otherwise
the main highlights are:

* The JS host generator no longer implements the core `Generator` trait
  since it doesn't really fit in the component-as-input world. For now I
  created an `InterfaceGenerator` trait since similar functionality is
  used just not precisely the same. I expect that this will get iterated
  on over time as worlds and other host generators take shape.

* The `wasmtime-environ` crate from Wasmtime itself, typically a "private"
  dependency of Wasmtime, is used to parse the input component and
  generate an `instantiate` function in JS. Wasmtime does all the heavy
  lifting of creating a linear list of initializers for the component
  and this empowers the generator to simply generate JS that is the list
  of initializers.

* The `wit-component` crate is used to "extract" the `Interface`
  descriptions from the input component. This is used to generate type
  information for TypeScript as well as types for lifting/lowering.
  Internally a correlation is done from a lowering/lifting to an
  `Interface` function to connect the dots when instantiating a component.

Lots of pieces needed updating here such as the runtime tests, the
`wit-bindgen` CLI tool, and the demo web page. These are all updated for
the new structure of the JS host generator.

Overall this surprisingly went much more smoothly than I expected. With
`wit-bindgen` being able to extract `Interface` representations from a
component most of the prior JS host code generation was able to be
reused. Along the way I also felt that the addition of "worlds" would
have relatively obvious insertion points and would be relatively easily
handled in all the places.

The demo and runtime tests are proof enough to me at least that this all
works internally, and this feels like a solid foundation to iterate from
with the addition of worlds and continued support for JS hosts.
  • Loading branch information
alexcrichton committed Oct 13, 2022
1 parent ac6beeb commit 5aa153a
Show file tree
Hide file tree
Showing 35 changed files with 1,891 additions and 1,652 deletions.
72 changes: 42 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ pulldown-cmark = { version = "0.8", default-features = false }
clap = { version = "4.0.9", features = ["derive"] }
env_logger = "0.9.1"

wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", branch = "main" , features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", branch = "main" }
wasmtime = { git = "https://github.com/alexcrichton/wasmtime", branch = "wit-bindgen-pieces" , features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/alexcrichton/wasmtime", branch = "wit-bindgen-pieces" }
wasmtime-environ = { git = "https://github.com/alexcrichton/wasmtime", branch = "wit-bindgen-pieces" }
wasmprinter = "0.2.41"
wasmparser = "0.92.0"
wasm-encoder = "0.18.0"
Expand Down Expand Up @@ -62,3 +63,4 @@ wit-bindgen-gen-host-js = { path = 'crates/gen-host-js', features = ['clap'] }
wit-bindgen-gen-guest-c = { path = 'crates/gen-guest-c', features = ['clap'] }
wit-bindgen-gen-markdown = { path = 'crates/gen-markdown', features = ['clap'] }
wit-bindgen-gen-guest-teavm-java = { path = 'crates/gen-guest-teavm-java', features = ['clap'] }
wat = { workspace = true }
Loading

0 comments on commit 5aa153a

Please sign in to comment.