Skip to content

Reimplement the host Python generator with worlds #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2022

Conversation

alexcrichton
Copy link
Member

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".

This commit is the equivalent of bytecodealliance#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".
@alexcrichton alexcrichton requested a review from pchickey October 20, 2022 17:13
Copy link
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! I won't pretend to understand very much of the code generator, but the way the tests got simplified is really great

@alexcrichton alexcrichton merged commit 34a4bc7 into bytecodealliance:main Oct 24, 2022
@alexcrichton alexcrichton deleted the wasmtime-py-world branch October 24, 2022 14:16
alexcrichton added a commit to alexcrichton/wit-bindgen that referenced this pull request Oct 25, 2022
This commit removes the `Generator` trait impl for the C generator and
adds the `WorldGenerator` trait impl to replace it. This is the
equivalent of bytecodealliance#386 for the guest C generator.

The generated C code is largely the same, except that the entire world
is represented in one header file instead of per-interface header files
as before. Additionally a tiny amount of "type sharing" is now done
where all interfaces share the same string type (namespaced by the
world name). More type sharing should come with a more first-class
implementation of worlds.

There was quite a lot of code movement within the generator as I got it
working again, but at the surface level very little has changed and it
should largely be the same as before.
alexcrichton added a commit to alexcrichton/wit-bindgen that referenced this pull request Oct 25, 2022
This commit removes the `Generator` trait impl for the C generator and
adds the `WorldGenerator` trait impl to replace it. This is the
equivalent of bytecodealliance#386 for the guest C generator.

The generated C code is largely the same, except that the entire world
is represented in one header file instead of per-interface header files
as before. Additionally a tiny amount of "type sharing" is now done
where all interfaces share the same string type (namespaced by the
world name). More type sharing should come with a more first-class
implementation of worlds.

There was quite a lot of code movement within the generator as I got it
working again, but at the surface level very little has changed and it
should largely be the same as before.
alexcrichton added a commit that referenced this pull request Oct 25, 2022
This commit removes the `Generator` trait impl for the C generator and
adds the `WorldGenerator` trait impl to replace it. This is the
equivalent of #386 for the guest C generator.

The generated C code is largely the same, except that the entire world
is represented in one header file instead of per-interface header files
as before. Additionally a tiny amount of "type sharing" is now done
where all interfaces share the same string type (namespaced by the
world name). More type sharing should come with a more first-class
implementation of worlds.

There was quite a lot of code movement within the generator as I got it
working again, but at the surface level very little has changed and it
should largely be the same as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants