Skip to content

Commit

Permalink
remove clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Jun 30, 2024
1 parent ee6b95d commit 6a09113
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 96 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ jobs:
with:
components: rust-src
- uses: mkroening/rust-toolchain-toml@main
- run: rustup component add llvm-tools
- run: |
rustup component add llvm-tools
rustup target add wasi32-wasm
- name: Check docs
run: cargo doc --no-deps --document-private-items

Expand Down
180 changes: 85 additions & 95 deletions examples/wasmtime/src/preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
match clock_id {
0 => match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => {
if let Some(mem) = caller.get_export("memory") {
if let Extern::Memory(mem) = mem {
let nanos = n.as_secs() * 1000000000 + n.subsec_nanos() as u64;
let _ = mem.write(
caller.as_context_mut(),
timestamp_ptr.try_into().unwrap(),
nanos.as_bytes(),
);

return 0;
}
if let Some(Extern::Memory(mem)) = caller.get_export("memory") {
let nanos = n.as_secs() * 1000000000 + n.subsec_nanos() as u64;
let _ = mem.write(
caller.as_context_mut(),
timestamp_ptr.try_into().unwrap(),
nanos.as_bytes(),
);

return 0;
}

hermit_abi::EINVAL
Expand All @@ -40,19 +38,17 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
1 => {
static NOW: OnceCell<Instant> = OnceCell::new();

if let Some(mem) = caller.get_export("memory") {
if let Extern::Memory(mem) = mem {
let elapsed = NOW.get_or_init(|| Instant::now()).elapsed();
let nanos: u64 =
elapsed.as_secs() * 1000000000 + elapsed.subsec_nanos() as u64;
let _ = mem.write(
caller.as_context_mut(),
timestamp_ptr.try_into().unwrap(),
nanos.as_bytes(),
);
if let Some(Extern::Memory(mem)) = caller.get_export("memory") {
let elapsed = NOW.get_or_init(|| Instant::now()).elapsed();
let nanos: u64 =
elapsed.as_secs() * 1000000000 + elapsed.subsec_nanos() as u64;
let _ = mem.write(
caller.as_context_mut(),
timestamp_ptr.try_into().unwrap(),
nanos.as_bytes(),
);

return 0;
}
return 0;
}

hermit_abi::EINVAL
Expand All @@ -71,49 +67,47 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
iovs_ptr: i32,
iovs_len: i32,
nwritten_ptr: i32| {
if let Some(mem) = caller.get_export("memory") {
if let Extern::Memory(mem) = mem {
let mut iovs = vec![0i32; (2 * iovs_len).try_into().unwrap()];
if let Some(Extern::Memory(mem)) = caller.get_export("memory") {
let mut iovs = vec![0i32; (2 * iovs_len).try_into().unwrap()];
let _ = mem.read(
caller.as_context(),
iovs_ptr.try_into().unwrap(),
iovs.as_bytes_mut(),
);

let mut nwritten_bytes: i32 = 0;
let mut i = 0;
while i < iovs.len() {
let len = iovs[i + 1];
let mut data = vec![0u8; len.try_into().unwrap()];

let _ = mem.read(
caller.as_context(),
iovs_ptr.try_into().unwrap(),
iovs.as_bytes_mut(),
iovs[i].try_into().unwrap(),
&mut data[..],
);

let mut nwritten_bytes: i32 = 0;
let mut i = 0;
while i < iovs.len() {
let len = iovs[i + 1];
let mut data = vec![0u8; len.try_into().unwrap()];

let _ = mem.read(
caller.as_context(),
iovs[i].try_into().unwrap(),
&mut data[..],
);
let result = unsafe {
hermit_abi::write(fd, data.as_ptr(), len.try_into().unwrap())
};
if result > 0 {
nwritten_bytes += result as i32;
if result < len.try_into().unwrap() {
break;
}
} else {
return (-result).try_into().unwrap();
let result = unsafe {
hermit_abi::write(fd, data.as_ptr(), len.try_into().unwrap())
};
if result > 0 {
nwritten_bytes += result as i32;
if result < len.try_into().unwrap() {
break;
}

i += 2;
} else {
return (-result).try_into().unwrap();
}

let _ = mem.write(
caller.as_context_mut(),
nwritten_ptr.try_into().unwrap(),
nwritten_bytes.as_bytes(),
);

return 0;
i += 2;
}

let _ = mem.write(
caller.as_context_mut(),
nwritten_ptr.try_into().unwrap(),
nwritten_bytes.as_bytes(),
);

return 0;
}

hermit_abi::EINVAL
Expand All @@ -126,24 +120,22 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
"args_sizes_get",
|mut caller: Caller<'_, _>, number_args_ptr: i32, args_size_ptr: i32| {
// Currently, we ignore the arguments
if let Some(mem) = caller.get_export("memory") {
if let Extern::Memory(mem) = mem {
// Currently, we ignore the environment
let zereo: u32 = 0;

let _ = mem.write(
caller.as_context_mut(),
number_args_ptr.try_into().unwrap(),
zereo.as_bytes(),
);
let _ = mem.write(
caller.as_context_mut(),
args_size_ptr.try_into().unwrap(),
zereo.as_bytes(),
);

return 0;
}
if let Some(Extern::Memory(mem)) = caller.get_export("memory") {
// Currently, we ignore the environment
let zereo: u32 = 0;

let _ = mem.write(
caller.as_context_mut(),
number_args_ptr.try_into().unwrap(),
zereo.as_bytes(),
);
let _ = mem.write(
caller.as_context_mut(),
args_size_ptr.try_into().unwrap(),
zereo.as_bytes(),
);

return 0;
}

hermit_abi::EINVAL
Expand All @@ -162,24 +154,22 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
"wasi_snapshot_preview1",
"environ_sizes_get",
|mut caller: Caller<'_, _>, number_env_variables_ptr: i32, env_buffer_size_ptr: i32| {
if let Some(mem) = caller.get_export("memory") {
if let Extern::Memory(mem) = mem {
// Currently, we ignore the environment
let zereo: u32 = 0;

let _ = mem.write(
caller.as_context_mut(),
number_env_variables_ptr.try_into().unwrap(),
zereo.as_bytes(),
);
let _ = mem.write(
caller.as_context_mut(),
env_buffer_size_ptr.try_into().unwrap(),
zereo.as_bytes(),
);

return 0;
}
if let Some(Extern::Memory(mem)) = caller.get_export("memory") {
// Currently, we ignore the environment
let zereo: u32 = 0;

let _ = mem.write(
caller.as_context_mut(),
number_env_variables_ptr.try_into().unwrap(),
zereo.as_bytes(),
);
let _ = mem.write(
caller.as_context_mut(),
env_buffer_size_ptr.try_into().unwrap(),
zereo.as_bytes(),
);

return 0;
}

hermit_abi::EINVAL
Expand Down

0 comments on commit 6a09113

Please sign in to comment.