|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use wasmtime::{ |
| 4 | + component::{bindgen, Resource}, |
| 5 | + Module, |
| 6 | +}; |
| 7 | + |
| 8 | +bindgen!({ |
| 9 | + world: "common", |
| 10 | + path: "../../typescript/common/module/wit" |
| 11 | +}); |
| 12 | + |
| 13 | +pub use common::data::types::{Dictionary, Reference, Value}; |
| 14 | +use wasmtime_wasi::WasiView; |
| 15 | + |
| 16 | +pub trait InputOutput: Send + Sync + Clone + std::fmt::Debug { |
| 17 | + fn read(&self, key: &str) -> Option<Value>; |
| 18 | + fn write(&mut self, key: &str, value: Value); |
| 19 | +} |
| 20 | + |
| 21 | +pub struct ResourceTable<T> { |
| 22 | + next_index: u32, |
| 23 | + resources: BTreeMap<u32, T>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<T> ResourceTable<T> { |
| 27 | + pub fn add(&mut self, resource: T) -> u32 { |
| 28 | + let index = self.next_index; |
| 29 | + |
| 30 | + self.next_index = self.next_index + 1; |
| 31 | + self.resources.insert(index, resource); |
| 32 | + |
| 33 | + index |
| 34 | + } |
| 35 | + |
| 36 | + pub fn lookup(&self, index: u32) -> Option<&T> { |
| 37 | + self.resources.get(&index) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn remove(&mut self, index: u32) { |
| 41 | + self.resources.remove(&index); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<T> Default for ResourceTable<T> { |
| 46 | + fn default() -> Self { |
| 47 | + Self { |
| 48 | + next_index: Default::default(), |
| 49 | + resources: BTreeMap::new(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[repr(transparent)] |
| 55 | +pub struct HostReference(String); |
| 56 | + |
| 57 | +pub struct ModuleEnvironment<Io> |
| 58 | +where |
| 59 | + Io: InputOutput, |
| 60 | +{ |
| 61 | + io: Io, |
| 62 | + references: ResourceTable<HostReference>, |
| 63 | + |
| 64 | + wasi_resources: wasmtime_wasi::ResourceTable, |
| 65 | + wasi_ctx: wasmtime_wasi::WasiCtx, |
| 66 | +} |
| 67 | + |
| 68 | +impl<Io> ModuleEnvironment<Io> |
| 69 | +where |
| 70 | + Io: InputOutput, |
| 71 | +{ |
| 72 | + pub fn new(io: Io) -> Self { |
| 73 | + ModuleEnvironment { |
| 74 | + io, |
| 75 | + references: ResourceTable::default(), |
| 76 | + |
| 77 | + wasi_resources: wasmtime_wasi::ResourceTable::new(), |
| 78 | + wasi_ctx: wasmtime_wasi::WasiCtx::builder().build(), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + pub fn take_io(self) -> Io { |
| 83 | + self.io |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<Io> common::io::state::Host for ModuleEnvironment<Io> |
| 88 | +where |
| 89 | + Io: InputOutput, |
| 90 | +{ |
| 91 | + fn read(&mut self, name: String) -> Option<Resource<Reference>> { |
| 92 | + warn!("Reading input: {name}"); |
| 93 | + if self.io.read(&name).is_none() { |
| 94 | + return None; |
| 95 | + } |
| 96 | + |
| 97 | + let reference = HostReference(name); |
| 98 | + let index = self.references.add(reference); |
| 99 | + |
| 100 | + Some(Resource::new_own(index)) |
| 101 | + } |
| 102 | + |
| 103 | + fn write(&mut self, name: String, value: Value) -> () { |
| 104 | + warn!("Writing output: {name}"); |
| 105 | + self.io.write(&name, value); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<Io> common::data::types::HostDictionary for ModuleEnvironment<Io> |
| 110 | +where |
| 111 | + Io: InputOutput, |
| 112 | +{ |
| 113 | + fn get( |
| 114 | + &mut self, |
| 115 | + _resource: Resource<Dictionary>, |
| 116 | + _key: String, |
| 117 | + ) -> Option<wasmtime::component::Resource<Reference>> { |
| 118 | + unimplemented!("Dictionary not supported yet saaawiii"); |
| 119 | + } |
| 120 | + |
| 121 | + fn drop(&mut self, _rep: Resource<Dictionary>) -> wasmtime::Result<()> { |
| 122 | + unimplemented!("Dictionary not supported yet saaawiii"); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<Io> common::data::types::HostReference for ModuleEnvironment<Io> |
| 127 | +where |
| 128 | + Io: InputOutput, |
| 129 | +{ |
| 130 | + /// Dereference a reference to a value |
| 131 | + /// This call is fallible (for example, if the dereference is not allowed) |
| 132 | + /// The value may be none (for example, if it is strictly opaque) |
| 133 | + fn deref(&mut self, resource: Resource<Reference>) -> Result<Option<Value>, String> { |
| 134 | + let HostReference(key) = self |
| 135 | + .references |
| 136 | + .lookup(resource.rep()) |
| 137 | + .ok_or_else(|| String::from("Attempted to deref an untracked Reference"))?; |
| 138 | + |
| 139 | + Ok(self.io.read(key)) |
| 140 | + } |
| 141 | + |
| 142 | + fn drop(&mut self, rep: Resource<Reference>) -> wasmtime::Result<()> { |
| 143 | + Ok(self.references.remove(rep.rep())) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<Io> common::data::types::Host for ModuleEnvironment<Io> where Io: InputOutput {} |
| 148 | + |
| 149 | +impl<Io> WasiView for ModuleEnvironment<Io> |
| 150 | +where |
| 151 | + Io: InputOutput, |
| 152 | +{ |
| 153 | + fn table(&mut self) -> &mut wasmtime_wasi::ResourceTable { |
| 154 | + &mut self.wasi_resources |
| 155 | + } |
| 156 | + |
| 157 | + fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx { |
| 158 | + &mut self.wasi_ctx |
| 159 | + } |
| 160 | +} |
0 commit comments