Skip to content

Commit

Permalink
Merge pull request #3177 from wasmerio/wasi-types-generation-2
Browse files Browse the repository at this point in the history
Auto-generate wasi-types from .wit files
  • Loading branch information
syrusakbary authored Oct 10, 2022
2 parents 07c2add + 7492a61 commit 19e7e7a
Show file tree
Hide file tree
Showing 56 changed files with 14,156 additions and 4,454 deletions.
496 changes: 317 additions & 179 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"lib/vm",
"lib/wasi",
"lib/wasi-types",
"lib/wasi-types/wasi-types-generator-extra",
"lib/wasi-experimental-io-devices",
"lib/wasi-local-networking",
"lib/c-api/tests/wasmer-c-api-test-runner",
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.2", 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/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wit-bindgen/
12 changes: 12 additions & 0 deletions lib/wasi-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ license = "MIT"
readme = "README.md"
edition = "2018"

# 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" }
wasmer-types = { path = "../types", version = "=3.0.0-beta.2" }
wasmer-derive = { path = "../derive", version = "=3.0.0-beta.2" }
serde = { version = "1.0", features = ["derive"], optional = true }
byteorder = "1.3"
time = "0.2"

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

[features]
enable-serde = ["serde", "wasmer-types/serde"]
js = ["wasmer/js"]
sys = ["wasmer/sys"]
8 changes: 8 additions & 0 deletions lib/wasi-types/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# `wasmer-wasi-types` [![Build Status](https://github.com/wasmerio/wasmer/workflows/build/badge.svg?style=flat-square)](https://github.com/wasmerio/wasmer/actions?query=workflow%3Abuild) [![Join Wasmer Slack](https://img.shields.io/static/v1?label=Slack&message=join%20chat&color=brighgreen&style=flat-square)](https://slack.wasmer.io) [![MIT License](https://img.shields.io/github/license/wasmerio/wasmer.svg?style=flat-square)](https://github.com/wasmerio/wasmer/blob/master/LICENSE) [![crates.io](https://img.shields.io/crates/v/wasmer-wasi-types.svg)](https://crates.io/crates/wasmer-wasi-types)

This crate contains the WASI types necessary for `wasmer-wasi`. Please check this crate to learn more!

---

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/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
7 changes: 0 additions & 7 deletions lib/wasi-types/src/advice.rs

This file was deleted.

113 changes: 0 additions & 113 deletions lib/wasi-types/src/bus.rs

This file was deleted.

60 changes: 0 additions & 60 deletions lib/wasi-types/src/directory.rs

This file was deleted.

Loading

0 comments on commit 19e7e7a

Please sign in to comment.