1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#![allow(missing_docs)] use crate::{ast, types::InterfaceType, values::InterfaceValue}; use std::{cell::Cell, ops::Deref}; pub trait TypedIndex: Copy + Clone { fn new(index: usize) -> Self; fn index(&self) -> usize; } macro_rules! typed_index { ($type:ident) => { #[derive(Copy, Clone)] pub struct $type(usize); impl TypedIndex for $type { fn new(index: usize) -> Self { Self(index) } fn index(&self) -> usize { self.0 } } }; } typed_index!(FunctionIndex); typed_index!(LocalFunctionIndex); typed_index!(ImportFunctionIndex); pub trait LocalImportIndex { type Local: TypedIndex; type Import: TypedIndex; } impl LocalImportIndex for FunctionIndex { type Local = LocalFunctionIndex; type Import = ImportFunctionIndex; } pub trait Export { fn inputs_cardinality(&self) -> usize; fn outputs_cardinality(&self) -> usize; fn inputs(&self) -> &[InterfaceType]; fn outputs(&self) -> &[InterfaceType]; fn call(&self, arguments: &[InterfaceValue]) -> Result<Vec<InterfaceValue>, ()>; } pub trait LocalImport { fn inputs_cardinality(&self) -> usize; fn outputs_cardinality(&self) -> usize; fn inputs(&self) -> &[InterfaceType]; fn outputs(&self) -> &[InterfaceType]; fn call(&self, arguments: &[InterfaceValue]) -> Result<Vec<InterfaceValue>, ()>; } pub trait MemoryView: Deref<Target = [Cell<u8>]> {} pub trait Memory<View> where View: MemoryView, { fn view(&self) -> View; } pub trait Instance<E, LI, M, MV> where E: Export, LI: LocalImport, M: Memory<MV>, MV: MemoryView, { fn export(&self, export_name: &str) -> Option<&E>; fn local_or_import<I: TypedIndex + LocalImportIndex>(&mut self, index: I) -> Option<&LI>; fn memory(&self, index: usize) -> Option<&M>; fn wit_type(&self, index: u32) -> Option<&ast::Type>; } impl Export for () { fn inputs_cardinality(&self) -> usize { 0 } fn outputs_cardinality(&self) -> usize { 0 } fn inputs(&self) -> &[InterfaceType] { &[] } fn outputs(&self) -> &[InterfaceType] { &[] } fn call(&self, _arguments: &[InterfaceValue]) -> Result<Vec<InterfaceValue>, ()> { Err(()) } } impl LocalImport for () { fn inputs_cardinality(&self) -> usize { 0 } fn outputs_cardinality(&self) -> usize { 0 } fn inputs(&self) -> &[InterfaceType] { &[] } fn outputs(&self) -> &[InterfaceType] { &[] } fn call(&self, _arguments: &[InterfaceValue]) -> Result<Vec<InterfaceValue>, ()> { Err(()) } } pub(crate) struct EmptyMemoryView; impl MemoryView for EmptyMemoryView {} impl Deref for EmptyMemoryView { type Target = [Cell<u8>]; fn deref(&self) -> &Self::Target { &[] } } impl Memory<EmptyMemoryView> for () { fn view(&self) -> EmptyMemoryView { EmptyMemoryView } } impl<E, LI, M, MV> Instance<E, LI, M, MV> for () where E: Export, LI: LocalImport, M: Memory<MV>, MV: MemoryView, { fn export(&self, _export_name: &str) -> Option<&E> { None } fn memory(&self, _: usize) -> Option<&M> { None } fn local_or_import<I: TypedIndex + LocalImportIndex>(&mut self, _index: I) -> Option<&LI> { None } fn wit_type(&self, _index: u32) -> Option<&ast::Type> { None } }