167 Commits

Author SHA1 Message Date
Ryan Levick
7db951ba9f Consistently add generated preamble (#621)
* Consistently add generated preamble

* Fix tests
2023-07-17 15:40:18 -05:00
Peter Huene
4e2dcd4431 Rust: add option for path to wit-bindgen runtime. (#619)
* Rust: add option for path to wit-bindgen runtime.

This change adds an optional path to the `wit-bindgen` runtime to use for Rust
code generation.

This will allow a different crate from `wit-bindgen` to generate bindings while
also re-exporting `wit-bindgen::rt` at a known path for the generated code to
use.

* Rust: add `runtime_path` macro option and a test.
2023-07-14 16:35:15 -05:00
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
8ceb28d320 Disable flaky variants go-bindgen runtime test (#601)
Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
2023-06-29 12:08:38 -05:00
Joel Dice
c28cdd6c1c provide more control over type ownership (#598)
* provide more control over type ownership

This is the guest version of https://github.com/bytecodealliance/wasmtime/pull/6648.

This replaces the duplicate_if_necessary parameter to the Rust binding generator
with a new ownership parameter which provides finer-grained control over whether
and how generated types own their fields.

The default is `Ownership::Owning`, which means types own their fields
regardless of how they are used in functions. These types are passed by
reference when used as parameters to guest-exported functions. Note that this
also affects how unnamed types (e.g. `list<list<string>>`) are passed: using a
reference only at the top level (e.g. `&[Vec<String>]` instead of `&[&[&str]]`,
which is more difficult to construct when using non-'static data).

The other option is `Ownership::Borrowing`, which includes a
`duplicate_if_necessary` field, providing the same code generation strategy as
was used prior to this change.

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

* fix tests

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

* fix tests, part 2

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

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-06-28 10:54:30 -05: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
Joel Dice
081a4bb11c update teavm-java generator and re-enable tests (#584)
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-05-26 15:25:57 -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
dab3644b0e Update to Wasmtime 9.0.0 (#582) 2023-05-24 16:27:57 -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
Ryan Levick
e6b6324963 Update wasm-tools dependencies (#562)
* Update wasm-tools dependencies

* Fix non-compiling tests
2023-04-27 14:31:04 -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
1be6d2e4d6 Don't generate *Param and *Result types in Rust by default (#547)
Previously when a single WIT type was used in a "borrowed" position, or
a parameter to an import, and an "owned" position then the type would
have two copies of its definition generated if it internally contained a
list. This was intended to signify that for import parameters ownership
wasn't necessary so borrowed values could be passed in.

In practice though I don't think this was the right default to have.
This surprisingly generates two types when one might expect one and
otherwise if a type if received from an export and expected to be
passed to an import then it's not easy to do so since it must be
converted to the borrowed type.

This commit moves the preexisting behavior of generating duplicate types
behind a new `duplicate_if_necessary` option. The default behavior is to
no longer generate two types, but instead just one. Imports now generate
their types with a `&` in front if it's otherwise owned internally to
signify that ownership isn't required.

Closes #535
2023-03-28 13:04:46 -07:00
Alex Crichton
483f92e12c Update to Wasmtime 7 (#543)
* Update to Wasmtime 7

* Fix trait names for 7.0.0
2023-03-21 10:53:36 -05: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
Jiaxiao Zhou
cd9d253d11 Refactor: Renamed folders and crates (#522)
* renamed folders and crates

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

* renmae for markdown crate

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

* fixed a CI error

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

* changed gen-guest-teavm-java to teavm-java

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

---------

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
2023-02-27 09:10:32 -06:00
Joel Dice
eeba5da107 remove use of Maven in Java tests (#520)
Per #495, Maven has proven quite unreliable for the past few weeks, so
now we download and execute the relevant TeaVM jars directly, which
should be more reliable.

This also fixes the lift-lower-foreign.wit codegen test for Java.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-02-23 09:24:23 -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
Jiaxiao Zhou
16994da9d5 feat: add guest binding generator for TinyGo/Go (#471)
* created wit-bindgen-gen-guest-go crate and added to the clap CLI

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

* rewrote import and export using a more generic conversion method

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

* milestone: records working

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

* simply code

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

* milestone: tuple works

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

* milestone: list works

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

* milestone: string works

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

* milestone: option works

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

* implemented result type

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

* milestone: result works

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

* milestone: result option work

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

* added codegen for go bindgen. Further work needs to pass all the tests

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

* get more tests passed, including conventions

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

* now flags and alias are working

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

* milestone: implemented multi-return

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

cargo clippy

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

* milestone: implemented union, enum and variantss

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

* rewrite lower's Result for imports

* avoid Go keywords conflict with wit's variable names

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

* milestone: all codegen tests passed.

The issue with "fragment is larger than or outside of variable" is
a known issue and has been resolved in tinygo v0.26.

However, tinygo v0.26 introduces a regression for wasm/wasi target
where the compiler panics when size 0 passed to malloc. See
https://github.com/tinygo-org/tinygo/pull/3303 for details

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

* refactor: add gen-guest-c dependency to gen-guest-go.

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

* cargo lock file

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

* milestone: passed new tests associated cross-interface feature

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

* formatted code

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

* gh action: installed tinygo

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

* install go 1.19

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

* generate C files together with Go files

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

* added option-result test

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

* fix a typo in wit-bindgen-cli doc

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

* updated README to include gen-guest-go

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

* removed option-result.wit

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

* fix the broken CLI

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

* added go to runtime test

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

* free C parameters and variables

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

* removed memory free for return variables in export and invoke arguments in import

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

* implemented lists

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

* fixed a small bug around lowering nested option types

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

* improve readability of code

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

* changed the variant's package name to the world's name.

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

* milestone: re-implement results

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

* Fixed a small reference issue for lowering result

* re-implement variants and unions

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

* fixed a few small bugs around variants and union types. Added variant runtime tests

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

* turn off gc-leaking in tinygo 0.26

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

* simplify code and add multi-return to lowering

* Added runtime tests for list, number and variant

* Added flavorful runtime test

* Added tests for many_aguments, records and smoke

* Added strings and unions runtime tests

* removed http from runtime test

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

* added go bindgen build step to ci

* updated tinygo to 0.27. All tests should pass now

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

* fixed go version to 1.20 instead of 1.2

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

* disable string test because unicode hasn't been handled properly

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

* deleted wasm abi flag

* merged main

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

* Move go's files to it's own subfolder

* move C gen files to its own folder

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

* update go from 1.19 to 1.20

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

* changed tinygo crate version to 0.1.0

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

* diable variants test for go

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

* Fixed a missing argument to encode

* Fix referring to foreign imported paths in go

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

* disable lift-foreign tests for now

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

* run rustfmt

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

---------

Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
2023-02-16 12:38:39 -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