53 Commits

Author SHA1 Message Date
Ryan Levick
1151bd0e2a C: Namespace types inside of annoymous types (#608)
* C: Namespace types inside of annoymous types

* Go generator should use c generator's name generation

* Optionaly namespace inner types for all anonymous types

* Test more
2023-07-14 10:06:26 -05:00
Dan Gohman
1a3acf9257 Correct the accounting for results containing empty types. (#610)
* Correct the accounting for results containing empty types.

Empty types are special-cased in the C backend because C doesn't allow
zero-sized types. In the case of a result containing an empty type,
when processing the ok side, increment the return value counter even
if the store is omitted due to the value being empty.

Fixes #609.

* Test all the combinations of non-empty, empty, and absent.
2023-07-12 13:21:53 -07:00
Ryan Levick
d629bc12ba Alias anon types in tiny-go (#606)
* Alias anon types in tiny-go

* Expand codegen test for anon types
2023-07-07 10:57:16 -07:00
Jiaxiao Zhou
ede00f0bf3 fix(go): enable all the codegen tests for go generator (#588)
* update go generator to re-enable codegen-tests

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

* Re-enable runtime tests

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

* enable all the runtime tests

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

* adds variants test

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

---------

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
2023-05-31 09:38:07 -05:00
Alex Crichton
a2935a4fa4 Update wit-bindgen for upcoming WIT changes (#580)
* Update Rust codegen for new WIT changes

* Update the markdown generation for new WIT

* more-rust

* Update the C generator for new WIT

* Get Rust guest tests compiling

* Get rust host tests working

* Get all C tests passing

* Turn off teavm-java and Go tests for now

* Fixup lockfile after rebase

* Bump crate versions

* Update dependency sources

* Update to wasmtime dep
2023-05-26 12:30:24 -05:00
Alex Crichton
0d398ceee8 Fix types used when interfaces are imported and exported (#574)
This fixes an issue where exported freestanding functions are intended
to refer to imported types, not exported types. The order of bindings
generation is altered here to ensure that code generators get access to
types only as they're available, ensuring that functions refer to
imported types rather than exported ones.

Closes #573
2023-05-10 10:57:47 -05:00
Alex Crichton
35cb45f25e Fix duplicate C types being generated (#571)
* Support directory-based codegen tests

Remove the old `*.wit` syntax in the macro as it's no longer necessary.

* Fix duplicate C types being generated

Closes #569

* Fix a Rust compile error
2023-05-10 10:52:05 -05:00
Alex Crichton
7ec986ba6b Relax an assertion in Rust type emission (#552)
I forgot a context where the assertion would trip, so implement the code
necessary to avoid the need for the assertion.

Closes #551
2023-03-29 14:06:26 -05:00
Alex Crichton
58c130aeb3 Update how types are generated in C (#545)
* Update how types are generated in C

Prior to this commit each interface used in C would generate a separate
type per import and per export. For example if a `world` both imported
and exported an interface then distinct, but defined the same way, types
would be generated. Furthermore this same logic was extended to
types-in-worlds which ended up not working well and causing #544.

This commit overhauls how types are generated in C. All used interfaces
now generate one set of types, regardless of whether the interface is
used in an import or an export. Additionally this enables fixing #544 by
generating types for worlds only once at the end instead of
incrementally throughout.

Along the way this updates many names of generated types to be more
appropriate to their scoping constraints. For example types in
interfaces are now namespaced by the name of the interface. Functions
continue to be namespaced by the name of the import/export and disregard
the name of the interface. Ambient types like `record<T, E>` continue to
be prefixed by the name of the world that bindings are generated for.

Closes #544

* Disable go runtime tests

* Disable go codegen tests temporarily
2023-03-28 15:33:17 -07:00
Alex Crichton
cd59db533a Fix bindings generation with unused types in Rust (#530)
* Fix bindings generation with unused types in Rust

This commit fixes an issue where if a type was imported into an
interface but wasn't actually used anywhere then bindings weren't
correctly generated because inference about whether it was owned or
borrowed didn't run. The fix here is to skip generating bindings for
unused types since they're not actually needed anywhere anyway. If
necessary in the future bindings can be forcibly generated but for now
this is hopefully largely "just" fixing an edge case.

* Ignore the new tests for Go
2023-03-06 13:48:10 -06:00
Alex Crichton
5fa5f53863 Fix Rust compilation with only borrowed structs (#519)
Fixes a mistake from #514
2023-02-22 09:32:56 -06:00
Alex Crichton
18eb686bf1 Fix using Rust types in owned and borrowed contexts (#514)
This pulls in type analysis code from Wasmtime which has a more modern
notion of param/result as owned/borrowed and additionally removes the
need for a `default_param_mode` which caused #513 in the first place.

Closes #513
2023-02-21 07:46:31 -06:00
Dan Gohman
751fd66f61 Handle interface names that collide with Rust keywords. (#508)
Use `to_rust_ident` for interface names, and add a test for a world
and interface with names that collide with Rust keywords.

Fixes #507.
2023-02-16 09:18:16 -06:00
Alex Crichton
6add188b83 Fix referring to foreign imported paths in Rust (#504)
This fixes the bindings generator for imports between interfaces to
ensure that matches and lifts/lowers/etc all use the right name as
opposed to assuming the name is already in scope.

Closes #485
2023-02-14 09:54:24 -08:00
Jiaxiao Zhou
5ed959e9a2 fix: enriched the keyword set and fixed Cgen keyword resolution (#483)
* enriched the keyword set and fixed Cgen keyword resolution

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

* added name resolution for java-bindgen

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

* fix keywords.wit test for Java generator

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* run cargo fmt

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

---------

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Joel Dice <joel.dice@fermyon.com>
2023-02-08 08:51:10 -06:00
Jiaxiao Zhou
7534f65c72 uncomment simple-list test (#481)
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
2023-02-03 08:57:30 -06:00
Alex Crichton
5635975a2f Update wit-{parser,component} crate versions (#479)
* Update wit-{parser,component} crate versions

Updates to a version that supports types-in-worlds.

* Fix names for upstream changes
2023-02-02 14:24:48 -06:00
Jiaxiao Zhou
d7b1fdef7a fix: added empty checks to option and result types (#473)
* added empty checks to option and result types

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

* clean up

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

* cargo fmt

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>

---------

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
2023-02-02 09:28:16 -06:00
Alex Crichton
77332eebf3 Fix multiple return areas being generated for imports in one function (#466)
* Fix multiple return areas being generated for imports in one function

This commit fixes the issues brought up in #444 and #454 by moving the
generation of the local variable to outside the `FunctionBindgen`
structure into just after the function has been generated. That way when
two return areas are requested they'll get unified into one storage.

Closes #444
Closes #454

* Exclude the new test on the teavm-java generator
2023-01-30 08:46:37 -06:00
Alex Crichton
808d311980 Fix bindings generation for bare function exports (#459)
* Fix bindings generation for bare function exports

Move around a `use` statement

* Fix post-return from top-level exports
2023-01-24 16:56:14 -06:00
Alex Crichton
d7b870268e Update the wit-{component,parser} crates (#457)
* Update the Rust guest generator

This commit updates the `wit-parser` and `wit-component` dependencies to
the latest 0.4.0 versions on crates.io, namely pulling in support for
`use`. This updates the `*.wit` codegen tests and gets all the codegen
tests working for the `gen-guest-rust` crate. This does not get the
entire repository's tests running.

The major changes here are:

* The `WorldGenerator` trait has been adapted for worlds and the new
  APIs of `wit-parser`.
* The Rust guest generator now supports bare function imports as well as
  bare function exports.
* The syntax in the Rust macro now mirrors the syntax in the Wasmtime
  `bindgen!` macro where `generate!("foo")` will look for the document
  `foo.wit` in `$CARGO_MANIFEST_DIR/wit/foo.wit` and look for a `default
  world` inside there.
* The codegen and runtime macros have been refactored to get separated
  and additionally have conditional compilation working so one generator
  can be tested in isolation without requiring all others working.
* The `rust-macro-shared` crate is now removed since Wasmtime no longer
  resides here and it's merged directly into the `guest-rust-macro`
  crate. Note that many changes happened here since the macro syntax and
  integration with `wit-parser` has changed.

Note that no new tests were added in this commit, and that'll be a
follow-up.

* Add support for using types across interfaces to Rust

* Support renaming interfaces in imports

* Get all codegen tests for JS passing again

* Fix build of wasi_snapshot_preview1 for this repo

* Get unions tests building again

* Get the flavorful test compiling again

* Get the records test compiling again

* Get the many arguments test compiling again

* Get the smoke test compiling again

* Get numbers test compiling again

* Get list test compiling again

* Get variants test compiling again

* Get results test compiling again

* Get invalid test compiling again

* Get all runtime JS tests passing

Change a few bits and pieces of the JS host generator as necessary and
temporarily disable other guest generators.

* Use git repo for wit-{component,parser} patch

* Update C generator for new WIT

* Get the bare bones of teavm-java working

Runtime tests will need some more work to get passing.

* Get CLI and markdown crate building again

* Update build of demo

* Use updated crates.io dep for wit-component

* fix Java runtime test regressions (#1)

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* Update wasm-tools on CI

* Review comments

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Joel Dice <joel.dice@fermyon.com>
2023-01-20 15:41:38 -06:00
Dan Gohman
dde0dc92a3 Use to_rust_ident to generate identifiers in Rust code. (#441)
This handles keywords which collide with identifiers.
2022-11-23 17:50:11 -06:00
Alex Crichton
0220c9593c Migrate all tests to using world-file-syntax (#432)
* Update all codegen tests to world syntax

This enables simplifying codegen tests as well by having each test
exercise imports/exports instead of having all code generators have one
case for imports and one for exports.

* Update codegen tests for the C guest generator

* Update teavm codegen tests for worlds

* Remove the `--name` argument from the CLI

No longer needed as the name is inferred from the `*.wit` world.

* Get all js host generator tests working

Update all `runtime/*` tests with new naming conventions, world files,
etc. Minor updates were made to names as I ended up using different
conventions for the `*.wit` world files than were previously exercised.

This also fixes a few minor issues with the output for the JS generator
in the default ESM mode.

* Get all host-wasmtime tests working with worlds

Various small updates here and there to namings and such.

* Get all Python tests working with worlds

Fix support for exported instances by using correct import paths for
various types/intrinsics/etc. Additionally update the codegen tests to
specify a mypy cache dir to avoid racing between tests.

* Update demo build for worlds

Also fix an issue with the Rust host generator where it used an
interface's name instead of the name of the import for import module names.

* Update demo with world files
2022-11-15 17:16:09 -06:00
Brian
f41c9e0a0d syntax: world + interface (#413)
* syntax: world + interface

Signed-off-by: Brian H <brian.hardock@fermyon.com>

* Get tests passing

Remove all parsing tests related to `use` along the way to get re-added
once `use` is re-implemented for top-level worlds.

* Update wit-parser tests to worlds

Drop usage of the old `Interface::parse_file` function.

* Add some basic tests for world

Assert some assorted failures as well as a basic successful structure.

* Remove existing support for `use`

This will come back shortly with worlds, but for now nothing uses this
in the repository any more and it's not implemented within the resolver
so temporarily remove it.

* Thread through docs on worlds/interfaces

* Remove docs from imports/exports

They're not threaded anywhere right now so remove them. Can be added
back in if needed in the future.

* Implement temporary stopgap of `default export`

* Fix demo build

Signed-off-by: Brian H <brian.hardock@fermyon.com>
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
2022-11-15 09:02:30 -06:00
Alex Crichton
34a4bc7ce0 Reimplement the host Python generator with worlds (#386)
* 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
2022-10-24 09:16:04 -05:00