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

feat(wasi-types) Extract types from wasi to a new wasi-types crate #2411

Merged
merged 5 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C
## 2.0.0-rc2 - 2020/06/03

### Added
- [#2411](https://github.com/wasmerio/wasmer/pull/2411) Extract types from `wasi` to a new `wasi-types` crate.
- [#2390](https://github.com/wasmerio/wasmer/pull/2390) Make `wasmer-vm` to compile on Windows 32bits.
- [#2402](https://github.com/wasmerio/wasmer/pull/2402) Add more examples and more doctests for `wasmer-middlewares`.

Expand Down
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ members = [
"lib/object",
"lib/vm",
"lib/wasi",
"lib/wasi-types",
"lib/wasi-experimental-io-devices",
"lib/types",
"tests/lib/wast",
Expand Down
17 changes: 17 additions & 0 deletions lib/wasi-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "wasmer-wasi-types"
version = "2.0.0-rc2"
description = "WASI types for Wasmer WebAssembly runtime"
categories = ["wasm", "os"]
keywords = ["wasm", "webassembly", "wasi", "sandbox", "ABI"]
authors = ["Wasmer Engineering Team <[email protected]>"]
repository = "https://github.com/wasmerio/wasmer"
license = "MIT"
readme = "README.md"
edition = "2018"

[dependencies]
wasmer-types = { path = "../types", version = "2.0.0-rc2" }
serde = { version = "1.0", features = ["derive"] }
byteorder = "1.3"
time = "0.1"
3 changes: 3 additions & 0 deletions lib/wasi-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `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!
7 changes: 7 additions & 0 deletions lib/wasi-types/src/advice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub type __wasi_advice_t = u8;
pub const __WASI_ADVICE_NORMAL: u8 = 0;
pub const __WASI_ADVICE_SEQUENTIAL: u8 = 1;
pub const __WASI_ADVICE_RANDOM: u8 = 2;
pub const __WASI_ADVICE_WILLNEED: u8 = 3;
pub const __WASI_ADVICE_DONTNEED: u8 = 4;
pub const __WASI_ADVICE_NOREUSE: u8 = 5;
41 changes: 41 additions & 0 deletions lib/wasi-types/src/directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::*;
use std::mem;
use wasmer_types::ValueType;

pub type __wasi_dircookie_t = u64;
pub const __WASI_DIRCOOKIE_START: u64 = 0;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct __wasi_dirent_t {
pub d_next: __wasi_dircookie_t,
pub d_ino: __wasi_inode_t,
pub d_namlen: u32,
pub d_type: __wasi_filetype_t,
}

unsafe impl ValueType for __wasi_dirent_t {}

pub fn dirent_to_le_bytes(ent: &__wasi_dirent_t) -> Vec<u8> {
use mem::transmute;

let mut out = Vec::with_capacity(mem::size_of::<__wasi_dirent_t>());
let bytes: [u8; 8] = unsafe { transmute(ent.d_next.to_le()) };
for &b in &bytes {
out.push(b);
}
let bytes: [u8; 8] = unsafe { transmute(ent.d_ino.to_le()) };
for &b in &bytes {
out.push(b);
}
let bytes: [u8; 4] = unsafe { transmute(ent.d_namlen.to_le()) };
for &b in &bytes {
out.push(b);
}
out.push(ent.d_type);
out.push(0);
out.push(0);
out.push(0);
assert_eq!(out.len(), mem::size_of::<__wasi_dirent_t>());
out
}
78 changes: 78 additions & 0 deletions lib/wasi-types/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pub type __wasi_errno_t = u16;
pub const __WASI_ESUCCESS: u16 = 0;
pub const __WASI_E2BIG: u16 = 1;
pub const __WASI_EACCES: u16 = 2;
pub const __WASI_EADDRINUSE: u16 = 3;
pub const __WASI_EADDRNOTAVAIL: u16 = 4;
pub const __WASI_EAFNOSUPPORT: u16 = 5;
pub const __WASI_EAGAIN: u16 = 6;
pub const __WASI_EALREADY: u16 = 7;
pub const __WASI_EBADF: u16 = 8;
pub const __WASI_EBADMSG: u16 = 9;
pub const __WASI_EBUSY: u16 = 10;
pub const __WASI_ECANCELED: u16 = 11;
pub const __WASI_ECHILD: u16 = 12;
pub const __WASI_ECONNABORTED: u16 = 13;
pub const __WASI_ECONNREFUSED: u16 = 14;
pub const __WASI_ECONNRESET: u16 = 15;
pub const __WASI_EDEADLK: u16 = 16;
pub const __WASI_EDESTADDRREQ: u16 = 17;
pub const __WASI_EDOM: u16 = 18;
pub const __WASI_EDQUOT: u16 = 19;
pub const __WASI_EEXIST: u16 = 20;
pub const __WASI_EFAULT: u16 = 21;
pub const __WASI_EFBIG: u16 = 22;
pub const __WASI_EHOSTUNREACH: u16 = 23;
pub const __WASI_EIDRM: u16 = 24;
pub const __WASI_EILSEQ: u16 = 25;
pub const __WASI_EINPROGRESS: u16 = 26;
pub const __WASI_EINTR: u16 = 27;
pub const __WASI_EINVAL: u16 = 28;
pub const __WASI_EIO: u16 = 29;
pub const __WASI_EISCONN: u16 = 30;
pub const __WASI_EISDIR: u16 = 31;
pub const __WASI_ELOOP: u16 = 32;
pub const __WASI_EMFILE: u16 = 33;
pub const __WASI_EMLINK: u16 = 34;
pub const __WASI_EMSGSIZE: u16 = 35;
pub const __WASI_EMULTIHOP: u16 = 36;
pub const __WASI_ENAMETOOLONG: u16 = 37;
pub const __WASI_ENETDOWN: u16 = 38;
pub const __WASI_ENETRESET: u16 = 39;
pub const __WASI_ENETUNREACH: u16 = 40;
pub const __WASI_ENFILE: u16 = 41;
pub const __WASI_ENOBUFS: u16 = 42;
pub const __WASI_ENODEV: u16 = 43;
pub const __WASI_ENOENT: u16 = 44;
pub const __WASI_ENOEXEC: u16 = 45;
pub const __WASI_ENOLCK: u16 = 46;
pub const __WASI_ENOLINK: u16 = 47;
pub const __WASI_ENOMEM: u16 = 48;
pub const __WASI_ENOMSG: u16 = 49;
pub const __WASI_ENOPROTOOPT: u16 = 50;
pub const __WASI_ENOSPC: u16 = 51;
pub const __WASI_ENOSYS: u16 = 52;
pub const __WASI_ENOTCONN: u16 = 53;
pub const __WASI_ENOTDIR: u16 = 54;
pub const __WASI_ENOTEMPTY: u16 = 55;
pub const __WASI_ENOTRECOVERABLE: u16 = 56;
pub const __WASI_ENOTSOCK: u16 = 57;
pub const __WASI_ENOTSUP: u16 = 58;
pub const __WASI_ENOTTY: u16 = 59;
pub const __WASI_ENXIO: u16 = 60;
pub const __WASI_EOVERFLOW: u16 = 61;
pub const __WASI_EOWNERDEAD: u16 = 62;
pub const __WASI_EPERM: u16 = 63;
pub const __WASI_EPIPE: u16 = 64;
pub const __WASI_EPROTO: u16 = 65;
pub const __WASI_EPROTONOSUPPORT: u16 = 66;
pub const __WASI_EPROTOTYPE: u16 = 67;
pub const __WASI_ERANGE: u16 = 68;
pub const __WASI_EROFS: u16 = 69;
pub const __WASI_ESPIPE: u16 = 70;
pub const __WASI_ESRCH: u16 = 71;
pub const __WASI_ESTALE: u16 = 72;
pub const __WASI_ETIMEDOUT: u16 = 73;
pub const __WASI_ETXTBSY: u16 = 74;
pub const __WASI_EXDEV: u16 = 75;
pub const __WASI_ENOTCAPABLE: u16 = 76;
83 changes: 83 additions & 0 deletions lib/wasi-types/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::*;
use std::fmt;
use wasmer_types::ValueType;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct __wasi_event_fd_readwrite_t {
pub nbytes: __wasi_filesize_t,
pub flags: __wasi_eventrwflags_t,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub union __wasi_event_u {
pub fd_readwrite: __wasi_event_fd_readwrite_t,
}

// TODO: remove this implementation of Debug when `__wasi_event_u` gets more than 1 variant
impl fmt::Debug for __wasi_event_u {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("__wasi_event_u")
.field("fd_readwrite", unsafe { &self.fd_readwrite })
.finish()
}
}

#[derive(Debug, Copy, Clone)]
pub enum EventEnum {
FdReadWrite {
nbytes: __wasi_filesize_t,
flags: __wasi_eventrwflags_t,
},
}

impl EventEnum {
pub fn untagged(self) -> __wasi_event_u {
match self {
EventEnum::FdReadWrite { nbytes, flags } => __wasi_event_u {
fd_readwrite: __wasi_event_fd_readwrite_t { nbytes, flags },
},
}
}
}

#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct __wasi_event_t {
pub userdata: __wasi_userdata_t,
pub error: __wasi_errno_t,
pub type_: __wasi_eventtype_t,
pub u: __wasi_event_u,
}

impl __wasi_event_t {
pub fn tagged(&self) -> Option<EventEnum> {
match self.type_ {
__WASI_EVENTTYPE_FD_READ | __WASI_EVENTTYPE_FD_WRITE => Some(EventEnum::FdReadWrite {
nbytes: unsafe { self.u.fd_readwrite.nbytes },
flags: unsafe { self.u.fd_readwrite.flags },
}),
_ => None,
}
}
}

unsafe impl ValueType for __wasi_event_t {}

pub type __wasi_eventrwflags_t = u16;
pub const __WASI_EVENT_FD_READWRITE_HANGUP: u16 = 1 << 0;

pub type __wasi_eventtype_t = u8;
pub const __WASI_EVENTTYPE_CLOCK: u8 = 0;
pub const __WASI_EVENTTYPE_FD_READ: u8 = 1;
pub const __WASI_EVENTTYPE_FD_WRITE: u8 = 2;

pub fn eventtype_to_str(event_type: __wasi_eventtype_t) -> &'static str {
match event_type {
__WASI_EVENTTYPE_CLOCK => "__WASI_EVENTTYPE_CLOCK",
__WASI_EVENTTYPE_FD_READ => "__WASI_EVENTTYPE_FD_READ",
__WASI_EVENTTYPE_FD_WRITE => "__WASI_EVENTTYPE_FD_WRITE",
_ => "INVALID EVENTTYPE",
}
}
Loading