Created
February 25, 2026 01:21
-
-
Save alexcrichton/ce02ac93762ccf74215b3fccc3469ee0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[allow(unused_variables, dead_code)] | |
| mod my_fs { | |
| use wasi::filesystem::preopens; | |
| use wasi::filesystem::types; | |
| use wasmtime::component::{HasData, Linker, Resource, ResourceTable}; | |
| wasmtime::component::bindgen!({ | |
| with: { | |
| "wasi:clocks": wasmtime_wasi::p2::bindings::clocks, | |
| "wasi:io": wasmtime_wasi::p2::bindings::io, | |
| "wasi:filesystem/types.descriptor": MyDescriptor, | |
| }, | |
| require_store_data_send: true, | |
| }); | |
| pub struct MyFsCtx { | |
| // .. | |
| } | |
| pub struct MyFsCtxView<'a> { | |
| pub cx: &'a mut MyFsCtx, | |
| pub table: &'a mut ResourceTable, | |
| } | |
| pub struct MyDescriptor { | |
| // .. | |
| } | |
| impl types::Host for MyFsCtxView<'_> { | |
| fn filesystem_error_code( | |
| &mut self, | |
| err: Resource<types::Error>, | |
| ) -> Option<types::ErrorCode> { | |
| todo!() | |
| } | |
| } | |
| impl types::HostDescriptor for MyFsCtxView<'_> { | |
| fn read_via_stream( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| offset: types::Filesize, | |
| ) -> Result<Resource<types::InputStream>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn write_via_stream( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| offset: types::Filesize, | |
| ) -> Result<Resource<types::OutputStream>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn append_via_stream( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<Resource<types::OutputStream>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn advise( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| offset: types::Filesize, | |
| length: types::Filesize, | |
| advice: types::Advice, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn sync_data( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn get_flags( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<types::DescriptorFlags, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn get_type( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<types::DescriptorType, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn set_size( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| size: types::Filesize, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn set_times( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| data_access_timestamp: types::NewTimestamp, | |
| data_modification_timestamp: types::NewTimestamp, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn read( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| length: types::Filesize, | |
| offset: types::Filesize, | |
| ) -> Result<(Vec<u8>, bool), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn write( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| buffer: Vec<u8>, | |
| offset: types::Filesize, | |
| ) -> Result<types::Filesize, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn read_directory( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<Resource<types::DirectoryEntryStream>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn sync(&mut self, self_: Resource<types::Descriptor>) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn create_directory_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn stat( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<types::DescriptorStat, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn stat_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path_flags: types::PathFlags, | |
| path: String, | |
| ) -> Result<types::DescriptorStat, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn set_times_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path_flags: types::PathFlags, | |
| path: String, | |
| data_access_timestamp: types::NewTimestamp, | |
| data_modification_timestamp: types::NewTimestamp, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn link_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| old_path_flags: types::PathFlags, | |
| old_path: String, | |
| new_descriptor: Resource<types::Descriptor>, | |
| new_path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn open_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path_flags: types::PathFlags, | |
| path: String, | |
| open_flags: types::OpenFlags, | |
| flags: types::DescriptorFlags, | |
| ) -> Result<Resource<types::Descriptor>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn readlink_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path: String, | |
| ) -> Result<String, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn remove_directory_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn rename_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| old_path: String, | |
| new_descriptor: Resource<types::Descriptor>, | |
| new_path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn symlink_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| old_path: String, | |
| new_path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn unlink_file_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path: String, | |
| ) -> Result<(), types::ErrorCode> { | |
| todo!() | |
| } | |
| fn is_same_object( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| other: Resource<types::Descriptor>, | |
| ) -> bool { | |
| todo!() | |
| } | |
| fn metadata_hash( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| ) -> Result<types::MetadataHashValue, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn metadata_hash_at( | |
| &mut self, | |
| self_: Resource<types::Descriptor>, | |
| path_flags: types::PathFlags, | |
| path: String, | |
| ) -> Result<types::MetadataHashValue, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn drop(&mut self, rep: Resource<types::Descriptor>) -> wasmtime::Result<()> { | |
| todo!() | |
| } | |
| } | |
| impl types::HostDirectoryEntryStream for MyFsCtxView<'_> { | |
| fn read_directory_entry( | |
| &mut self, | |
| self_: Resource<types::DirectoryEntryStream>, | |
| ) -> Result<Option<types::DirectoryEntry>, types::ErrorCode> { | |
| todo!() | |
| } | |
| fn drop(&mut self, rep: Resource<types::DirectoryEntryStream>) -> wasmtime::Result<()> { | |
| todo!() | |
| } | |
| } | |
| impl preopens::Host for MyFsCtxView<'_> { | |
| fn get_directories(&mut self) -> Vec<(Resource<types::Descriptor>, String)> { | |
| todo!() | |
| } | |
| } | |
| pub fn add_to_linker<T>( | |
| linker: &mut Linker<T>, | |
| get: fn(&mut T) -> MyFsCtxView<'_>, | |
| ) -> wasmtime::Result<()> { | |
| struct MyData; | |
| impl HasData for MyData { | |
| type Data<'a> = MyFsCtxView<'a>; | |
| } | |
| types::add_to_linker::<_, MyData>(linker, get)?; | |
| preopens::add_to_linker::<_, MyData>(linker, get)?; | |
| Ok(()) | |
| } | |
| } | |
| use wasmtime::component::{HasData, Linker, ResourceTable}; | |
| use wasmtime_wasi::clocks::{WasiClocks, WasiClocksCtx, WasiClocksCtxView}; | |
| use wasmtime_wasi::random::{WasiRandom, WasiRandomCtx}; | |
| pub struct MyStoreData { | |
| fs: my_fs::MyFsCtx, | |
| clocks: WasiClocksCtx, | |
| random: WasiRandomCtx, | |
| table: ResourceTable, | |
| } | |
| pub fn add_to_linker(linker: &mut Linker<MyStoreData>) -> wasmtime::Result<()> { | |
| use wasmtime_wasi::p2::bindings::{clocks, io, random}; | |
| // custom `wasi:filesystem` interfaces | |
| my_fs::add_to_linker(linker, |data| my_fs::MyFsCtxView { | |
| cx: &mut data.fs, | |
| table: &mut data.table, | |
| })?; | |
| // wasi:clocks interfaces | |
| clocks::monotonic_clock::add_to_linker::<_, WasiClocks>(linker, |state| WasiClocksCtxView { | |
| table: &mut state.table, | |
| ctx: &mut state.clocks, | |
| })?; | |
| clocks::wall_clock::add_to_linker::<_, WasiClocks>(linker, |state| WasiClocksCtxView { | |
| table: &mut state.table, | |
| ctx: &mut state.clocks, | |
| })?; | |
| // wasi:random interfaces | |
| random::random::add_to_linker::<_, WasiRandom>(linker, |state| &mut state.random)?; | |
| random::insecure::add_to_linker::<_, WasiRandom>(linker, |state| &mut state.random)?; | |
| random::insecure_seed::add_to_linker::<_, WasiRandom>(linker, |state| &mut state.random)?; | |
| // wasi:io interfaces | |
| io::poll::add_to_linker::<_, WasiIo>(linker, |state| &mut state.table)?; | |
| io::error::add_to_linker::<_, WasiIo>(linker, |state| &mut state.table)?; | |
| io::streams::add_to_linker::<_, WasiIo>(linker, |state| &mut state.table)?; | |
| return Ok(()); | |
| struct WasiIo; | |
| impl HasData for WasiIo { | |
| type Data<'a> = &'a mut ResourceTable; | |
| } | |
| } | |
| fn main() { | |
| // .. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment