Skip to content
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

Auto-generate wasi-types from .wit files #3177

Merged
merged 95 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
e33496b
WIP
silwol Aug 10, 2022
f2a4b87
Generate bindings directly for now
silwol Aug 16, 2022
1a48fab
WIP
silwol Aug 16, 2022
8d36b49
WIP: something that builds with Errno from wasi-types-generated
silwol Aug 16, 2022
8fdbde4
WIP
silwol Aug 17, 2022
d1bc30d
WIP
silwol Aug 17, 2022
a54005f
WIP
silwol Aug 17, 2022
c6e2eb0
Eventtype
silwol Aug 17, 2022
6d53f23
Advice
silwol Aug 17, 2022
293c67f
WIP: Rights
silwol Aug 17, 2022
75be1d3
WIP: Eventrwflags
silwol Aug 18, 2022
40b5b53
WIP: Subcodeflags
silwol Aug 18, 2022
24f88fd
WIP: Clockid
silwol Aug 18, 2022
5207cc1
WIP: Dirent struct
silwol Aug 18, 2022
a3deb19
WIP: Fdflags
silwol Aug 18, 2022
7e8b835
WIP: Preopentype
silwol Aug 18, 2022
39fad86
WIP: Fdstat
silwol Aug 18, 2022
188d069
WIP: Fd
silwol Aug 18, 2022
06ced06
WIP
silwol Aug 19, 2022
005b05d
Sauhaufen!
silwol Aug 19, 2022
d471def
Builds again
silwol Aug 23, 2022
26f4207
Timestamp
silwol Aug 23, 2022
8f5a1a6
BusErrno
silwol Aug 23, 2022
60cbd02
Socktype
silwol Aug 24, 2022
0b86b61
Sockstatus
silwol Aug 24, 2022
7c70dcb
Sockoption
silwol Aug 24, 2022
4bb761f
Streamsecurity
silwol Aug 24, 2022
4abe73c
Addressfamily
silwol Aug 24, 2022
721ece0
Filesize
silwol Aug 24, 2022
fdb7850
Inode
silwol Aug 24, 2022
ef8021a
Device
silwol Aug 24, 2022
14f1f42
Linkcount
silwol Aug 24, 2022
9fefd22
snapshot0 Filestat
silwol Aug 25, 2022
19e979c
Filestat
silwol Aug 25, 2022
7f2bd8f
snapshot0 Whence
silwol Aug 25, 2022
a9290a5
Whence
silwol Aug 25, 2022
7c6164d
Fstflags
silwol Aug 25, 2022
aa8d6fa
Tty
silwol Aug 25, 2022
7aacc7a
Tid + Pid
silwol Aug 25, 2022
5ba7615
WIP, Temporary: reference wit-bindgen silwol/wasi-types-generation gi…
silwol Aug 26, 2022
04bddcb
Merge branch 'master' into silwol/wasi-types-generation
fschutt Aug 31, 2022
73137f5
Add wit-clean files
fschutt Sep 5, 2022
a4e7e89
Update Cargo.lock
fschutt Sep 5, 2022
00503ad
Move "wasmer" dependency out of wasi-types-generated crate
fschutt Sep 5, 2022
69e3114
Port BusDataFormat
fschutt Sep 7, 2022
8983637
Use awk instead of sed to resolve build issues in regenerate.sh
fschutt Sep 7, 2022
b485e45
Add BusEventType
fschutt Sep 7, 2022
0a9595b
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 7, 2022
e7e8e9d
Fix errors in BusDataFormat
fschutt Sep 7, 2022
c1e8897
cargo fmt
fschutt Sep 7, 2022
dec63a4
Create build.rs script in wasi-types-generated to auto-generate Rust …
fschutt Sep 7, 2022
ca17477
Finish conversion to autogenerated bindings
fschutt Sep 7, 2022
2c72e6e
cargo fmt
fschutt Sep 7, 2022
cb86205
Port bus.rs to wasi-types-generated
fschutt Sep 7, 2022
d09a2ee
Serialize all wasi-types into one lib.rs file, adjust Oflags
fschutt Sep 8, 2022
1f35d34
cargo fmt
fschutt Sep 8, 2022
e318877
Start porting "net" to wit
fschutt Sep 8, 2022
4e35335
Port signal::Signal
fschutt Sep 8, 2022
daa20bf
Port "net" module to .wit (finished porting wasi-types)
fschutt Sep 8, 2022
914cbee
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 8, 2022
e4d4e86
Fix make test-js
fschutt Sep 8, 2022
639dc05
License wasi-types-generated MIT, comment out build.rs/main
fschutt Sep 8, 2022
64cab33
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 12, 2022
fbdec22
Added custom bindings test for wit-bindgen
fschutt Sep 12, 2022
921fac3
Remove dummy-func (which prevented proper linking) from .wit
fschutt Sep 12, 2022
17431df
Fix "make lint"
fschutt Sep 12, 2022
d2edcd7
Fix "make lint" again
fschutt Sep 12, 2022
8ce1597
Fix wasi-types-generated unit test
fschutt Sep 12, 2022
4e96969
Remove wasi_snapshot0 in favor of wasi
fschutt Sep 12, 2022
b164ea1
Remove unused imports
fschutt Sep 12, 2022
bdd5395
Fix "make lint" again
fschutt Sep 12, 2022
b98b691
Fix compilation issues on Windows
fschutt Sep 12, 2022
280b5ac
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 14, 2022
063c1e7
Update wit-bindgen
fschutt Sep 14, 2022
7549676
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 15, 2022
d9752a9
Update wit-bindgen again (with rev instead of branch)
fschutt Sep 15, 2022
a4e1d06
Update wit-bindgen
fschutt Sep 15, 2022
132e735
Fix dependency issue with wit-bindgen
fschutt Sep 15, 2022
7e3ca70
Use wit-bindgen dependency from crates.io
fschutt Sep 15, 2022
7343449
Update wit-bindgen version
fschutt Sep 15, 2022
9f9a0cd
Regenerate Cargo.lock
fschutt Sep 15, 2022
ac1e980
wit_bindgen_gen_rust_wasm -> wit_bindgen_rust_wasm
fschutt Sep 15, 2022
259e26c
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 22, 2022
ba2606c
Remove unused code, port subscription module
fschutt Sep 22, 2022
4217cf8
Move wasi-types-generated into wasi-types
fschutt Sep 22, 2022
36db57c
cargo fmt
fschutt Sep 22, 2022
3c0386d
Fix "make lint"
fschutt Sep 23, 2022
336c4d9
Correct bitflags: u8 -> bitflags: u16 / u32 / u64
fschutt Sep 26, 2022
c445f1e
Add regex crate
fschutt Sep 26, 2022
43e9d8f
Update wasi-types generator to replace repr(u16) types
fschutt Sep 27, 2022
8340a4b
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 27, 2022
c618db4
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 27, 2022
1397d2f
Merge branch 'master' into wasi-types-generation-2
fschutt Sep 29, 2022
5e049b9
Add doc comment to explain wasi-generator-extra
fschutt Sep 29, 2022
7492a61
Merge branch 'master' into wasi-types-generation-2
fschutt Oct 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
626 changes: 386 additions & 240 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ members = [
"lib/vm",
"lib/wasi",
"lib/wasi-types",
"lib/wasi-types-generated",
"lib/wasi-types-generated/wasi-types-generator-extra",
"lib/wasi-experimental-io-devices",
"lib/wasi-local-networking",
"lib/types",
Expand Down
29 changes: 29 additions & 0 deletions lib/api/src/js/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ impl<'a> MemoryView<'a> {
self.size
}

// TODO: do we want a proper implementation here instead?
/// Retrieve a slice of the memory contents.
///
/// # Safety
///
/// Until the returned slice is dropped, it is undefined behaviour to
/// modify the memory contents in any way including by calling a wasm
/// function that writes to the memory or by resizing the memory.
#[doc(hidden)]
pub unsafe fn data_unchecked(&self) -> &[u8] {
unimplemented!("direct data pointer access is not possible in JavaScript");
}

// TODO: do we want a proper implementation here instead?
/// Retrieve a mutable slice of the memory contents.
///
/// # Safety
///
/// This method provides interior mutability without an UnsafeCell. Until
/// the returned value is dropped, it is undefined behaviour to read or
/// write to the pointed-to memory in any way except through this slice,
/// including by calling a wasm function that reads the memory contents or
/// by resizing this Memory.
#[allow(clippy::mut_from_ref)]
#[doc(hidden)]
pub unsafe fn data_unchecked_mut(&self) -> &mut [u8] {
unimplemented!("direct data pointer access is not possible in JavaScript");
}

/// Returns the size (in [`Pages`]) of the `Memory`.
///
/// # Example
Expand Down
8 changes: 6 additions & 2 deletions lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ impl Module {
}

/// Serializes a module into a binary representation that the `Engine`
/// can later process via [`Module::deserialize`].
/// can later process via
#[cfg_attr(feature = "compiler", doc = "[`Module::deserialize`].")]
#[cfg_attr(not(feature = "compiler"), doc = "`Module::deserialize`.")]
///
/// # Usage
///
Expand All @@ -252,7 +254,9 @@ impl Module {
}

/// Serializes a module into a file that the `Engine`
/// can later process via [`Module::deserialize_from_file`].
/// can later process via
#[cfg_attr(feature = "compiler", doc = "[`Module::deserialize_from_file`].")]
#[cfg_attr(not(feature = "compiler"), doc = "`Module::deserialize_from_file`.")]
///
/// # Usage
///
Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/sys/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub(crate) struct StoreInner {
///
/// The `Store` holds the engine (that is —amongst many things— used to compile
/// the Wasm bytes into a valid module artifact), in addition to the
/// [`Tunables`] (that are used to create the memories, tables and globals).
#[cfg_attr(feature = "compiler", doc = "[`Tunables`]")]
#[cfg_attr(not(feature = "compiler"), doc = "`Tunables`")]
/// (that are used to create the memories, tables and globals).
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#store>
pub struct Store {
Expand Down
1 change: 1 addition & 0 deletions lib/types/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ macro_rules! primitives {
)*)
}
primitives! {
bool
i8 u8
i16 u16
i32 u32
Expand Down
4 changes: 2 additions & 2 deletions lib/wasi-experimental-io-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ maintenance = { status = "experimental" }
[dependencies]
wasmer-wasi = { version = "=3.0.0-beta", path = "../wasi", default-features=false }
tracing = "0.1"
minifb = { version = "0.19", optional = true }
nix = "0.20.2"
minifb = { version = "0.23", optional = true }
nix = "0.25.0"
ref_thread_local = "0.1"
serde = "1"
typetag = "0.1"
Expand Down
7 changes: 5 additions & 2 deletions lib/wasi-experimental-io-devices/src/link-ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::collections::{BTreeSet, VecDeque};
use std::convert::TryInto;
use std::io::{Read, Seek, SeekFrom, Write};
use tracing::debug;
use wasmer_wasi::{types::*, WasiInodes};
use wasmer_wasi::{
types::{wasi::Filesize, *},
WasiInodes,
};
use wasmer_wasi::{Fd, VirtualFile, WasiFs, WasiFsError, ALL_RIGHTS, VIRTUAL_ROOT_FD};

use minifb::{Key, KeyRepeat, MouseButton, Scale, Window, WindowOptions};
Expand Down Expand Up @@ -417,7 +420,7 @@ impl VirtualFile for FrameBuffer {
fn size(&self) -> u64 {
0
}
fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError> {
fn set_len(&mut self, _new_size: Filesize) -> Result<(), WasiFsError> {
Ok(())
}
fn unlink(&mut self) -> Result<(), WasiFsError> {
Expand Down
1 change: 1 addition & 0 deletions lib/wasi-types-generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wit-bindgen/
22 changes: 22 additions & 0 deletions lib/wasi-types-generated/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "wasmer-wasi-types-generated"
version = "3.0.0-beta"
edition = "2018"
license = "MIT"
description = "Autogenerated wasi types, generated from .wit files"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasmer = { default-features = false, path = "../api", version = "3.0.0-beta" }
wit-bindgen-rust = { package = "wasmer-wit-bindgen-rust", version = "0.1.1" }
wit-bindgen-rust-wasm = { package = "wasmer-wit-bindgen-gen-rust-wasm", version = "0.1.1" }
wit-bindgen-core = { package = "wasmer-wit-bindgen-gen-core", version = "0.1.1" }
wit-parser = { package = "wasmer-wit-parser", version = "0.1.1" }

[dev-dependencies.pretty_assertions]
version = "1.3.0"

[features]
js = ["wasmer/js"]
sys = ["wasmer/sys"]
5 changes: 5 additions & 0 deletions lib/wasi-types-generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Run `regenerate.sh` to regenerate the wasi-types from
the `wasi-clean/typenames.wit` into the final Rust bindings.

The `wasi-types-generator-extra` generates some extra code
that wit-bindgen currently can't provide.
33 changes: 33 additions & 0 deletions lib/wasi-types-generated/regenerate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

BASEDIR=$(dirname "$0")

rm -f \
"$BASEDIR"/src/bindings.rs \
"$BASEDIR"/src/*/bindings.rs

cat "$BASEDIR"/wit-clean/typenames.wit "$BASEDIR"/wit-clean/wasi_unstable.wit > "$BASEDIR"/wit-clean/output.wit

git clone https://github.com/wasmerio/wit-bindgen --branch force-generate-structs --single-branch
git pull origin force-generate-structs
cd wit-bindgen
cargo build
cd ..

./wit-bindgen/target/debug/wit-bindgen rust-wasm \
--import "$BASEDIR"/wit-clean/output.wit \
--force-generate-structs \
--out-dir "$BASEDIR"/src/wasi \

awk '{sub(/mod output/,"pub mod output")}1' src/wasi/bindings.rs > src/wasi/bindings2.rs
cargo fmt --all
cp src/wasi/bindings2.rs src/wasi/bindings.rs
rm src/wasi/bindings2.rs

cd ./wasi-types-generator-extra
cargo build
pwd
../../../target/debug/wasi-types-generator-extra
cd ..

cargo fmt --all
42 changes: 42 additions & 0 deletions lib/wasi-types-generated/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub mod wasi;

// Prevent the CI from passing if the wasi/bindings.rs is not
// up to date with the output.wit file
#[test]
#[cfg(feature = "sys")]
fn fail_if_wit_files_arent_up_to_date() {
use wit_bindgen_core::Generator;

let output_wit = concat!(env!("CARGO_MANIFEST_DIR"), "/wit-clean/output.wit");
let bindings_target =
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/wasi/bindings.rs"));
let mut generator = wit_bindgen_rust_wasm::Opts {
..wit_bindgen_rust_wasm::Opts::default()
}
.build();
let output_wit_parsed = wit_parser::Interface::parse_file(output_wit).unwrap();
let imports = vec![output_wit_parsed];
let exports = vec![];
let mut files = Default::default();
generator.generate_all(
&imports, &exports, &mut files, /* generate_structs */ true,
);
let generated = files
.iter()
.filter_map(|(k, v)| if k == "bindings.rs" { Some(v) } else { None })
.next()
.unwrap();
let generated_str = String::from_utf8_lossy(generated);
let generated_str = generated_str
.lines()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join("\r\n");
let generated_str = generated_str.replace("mod output {", "pub mod output {");
let bindings_target = bindings_target
.lines()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join("\r\n");
pretty_assertions::assert_eq!(generated_str, bindings_target); // output.wit out of date? regenerate bindings.rs
}
Loading