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