Skip to content

Commit

Permalink
Reimplement the host Python generator with worlds (#386)
Browse files Browse the repository at this point in the history
* Reimplement the host Python generator with worlds

This commit is the equivalent of #381 but for Wasmtime Python host
bindings. This is a large-scale refactor of the internals of the Python
host generator in which I took quite a few liberties in internal
restructuring as well as output restructuring. The tests are probably
the best to review and better reflect what changed, but there are some
aspects of worlds that the tests are not currently exercising which
we'll want to add with the introduction of worlds in the future.

This means that all host generators are now working with worlds as input
rather than individual `*.wit` files, and the only two remaining
generators are the C and Java generators for guests (which I hope are
easier).

The high level summary of the new output is:

    out_dir/
      __init__.py             # top-level component exports/definitions
      types.py                # shared type information, currently just `Result`
      imports/
        __init__.py           # only here if something is imported
        foo.py                # type and protocol definition per interface
      exports/
        __init__.py           # only here if an instance is exported
        bar.py                # one per exported instance

"Default exports" will show up on the generated structure in
`out_dir/__init__.py` so all runtime tests, for example, do not generate
`exports` at this time. Lots of fiddly stuff went into structuring this
all right to get past `mypy` and additionally try to avoid name
conflicts. It's still somewhat easy to have name conflicts but ideally
they're of the more esoteric category rather than "really easy to run
into".

* Try to fix windows
  • Loading branch information
alexcrichton authored Oct 24, 2022
1 parent 5be459e commit 34a4bc7
Show file tree
Hide file tree
Showing 27 changed files with 1,962 additions and 1,943 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/gen-host-js/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-ignore
import { readFile } from 'node:fs/promises';
// @ts-ignore
import { argv, stdout } from 'node:process';
import { argv, stdout, stderr } from 'node:process';

// This is a helper function used from `host.ts` test in the `tests/runtime/*`
// directory to pass as the `instantiateCore` argument to the `instantiate`
Expand All @@ -20,4 +20,7 @@ export const testwasi = {
log(bytes: Uint8Array) {
stdout.write(bytes);
},
logErr(bytes: Uint8Array) {
stderr.write(bytes);
},
};
5 changes: 4 additions & 1 deletion crates/gen-host-wasmtime-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ version.workspace = true
edition.workspace = true

[dependencies]
wit-bindgen-core = { workspace = true }
wit-bindgen-core = { workspace = true, features = ['component-generator'] }
heck = { workspace = true }
clap = { workspace = true, optional = true }
wit-component = { workspace = true }
indexmap = "1.0"
wasmtime-environ = { workspace = true, features = ['component-model'] }

[dev-dependencies]
test-helpers = { path = '../test-helpers' }
Loading

0 comments on commit 34a4bc7

Please sign in to comment.