|
| 1 | +use std::process; |
| 2 | +extern crate wit_bindgen; |
| 3 | + |
| 4 | +wit_bindgen::generate!({ |
| 5 | + inline: r" |
| 6 | + package test:test; |
| 7 | +
|
| 8 | + world test { |
| 9 | + include wasi:filesystem/[email protected]; |
| 10 | + include wasi:cli/[email protected]; |
| 11 | + } |
| 12 | +", |
| 13 | + additional_derives: [PartialEq, Eq, Hash, Clone], |
| 14 | + // Work around https://github.com/bytecodealliance/wasm-tools/issues/2285. |
| 15 | + features:["clocks-timezone"], |
| 16 | + generate_all |
| 17 | +}); |
| 18 | + |
| 19 | +use wasi::filesystem::types::Descriptor; |
| 20 | +use wasi::filesystem::types::{DescriptorFlags, OpenFlags, PathFlags}; |
| 21 | + |
| 22 | +async fn test_is_same_object(dir: &Descriptor) { |
| 23 | + let afd = dir |
| 24 | + .open_at( |
| 25 | + PathFlags::empty(), |
| 26 | + "a.txt".to_string(), |
| 27 | + OpenFlags::empty(), |
| 28 | + DescriptorFlags::READ, |
| 29 | + ) |
| 30 | + .await |
| 31 | + .unwrap(); |
| 32 | + let bfd = dir |
| 33 | + .open_at( |
| 34 | + PathFlags::empty(), |
| 35 | + "b.txt".to_string(), |
| 36 | + OpenFlags::empty(), |
| 37 | + DescriptorFlags::READ, |
| 38 | + ) |
| 39 | + .await |
| 40 | + .unwrap(); |
| 41 | + |
| 42 | + // is-same-object: async func(other: borrow<descriptor>) -> bool; |
| 43 | + assert!(dir.is_same_object(dir).await); |
| 44 | + { |
| 45 | + let other = dir |
| 46 | + .open_at( |
| 47 | + PathFlags::empty(), |
| 48 | + ".".to_string(), |
| 49 | + OpenFlags::empty(), |
| 50 | + DescriptorFlags::READ, |
| 51 | + ) |
| 52 | + .await |
| 53 | + .unwrap(); |
| 54 | + assert!(dir.is_same_object(&other).await); |
| 55 | + } |
| 56 | + assert!(!dir.is_same_object(&afd).await); |
| 57 | + assert!(afd.is_same_object(&afd).await); |
| 58 | + assert!(!afd.is_same_object(&bfd).await); |
| 59 | + assert!(bfd.is_same_object(&bfd).await); |
| 60 | +} |
| 61 | + |
| 62 | +struct Component; |
| 63 | +export!(Component); |
| 64 | +impl exports::wasi::cli::run::Guest for Component { |
| 65 | + async fn run() -> Result<(), ()> { |
| 66 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 67 | + [(dir, dirname)] if dirname == "fs-tests.dir" => { |
| 68 | + test_is_same_object(dir).await; |
| 69 | + } |
| 70 | + [..] => { |
| 71 | + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); |
| 72 | + process::exit(1) |
| 73 | + } |
| 74 | + }; |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn main() { |
| 80 | + unreachable!("main is a stub"); |
| 81 | +} |
0 commit comments