Skip to content

Commit 6b7a6e2

Browse files
authored
Implement ReadAs and WriteAs for Ref types (#1687)
implement ReadAs and WriteAs for Ref types
1 parent b5e33e5 commit 6b7a6e2

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

crates/wasmi/src/reftype.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
collections::arena::ArenaIndex,
3-
core::UntypedVal,
3+
core::{ReadAs, UntypedVal, WriteAs},
44
store::Stored,
55
AsContextMut,
66
Func,
@@ -174,6 +174,39 @@ fn funcref_null_to_zero() {
174174
macro_rules! impl_conversions {
175175
( $( $reftype:ty ),* $(,)? ) => {
176176
$(
177+
impl ReadAs<$reftype> for UntypedVal {
178+
fn read_as(&self) -> $reftype {
179+
let bits = u64::from(*self);
180+
unsafe { mem::transmute::<u64, $reftype>(bits) }
181+
}
182+
}
183+
184+
impl ReadAs<Ref<$reftype>> for UntypedVal {
185+
fn read_as(&self) -> Ref<$reftype> {
186+
let bits = u64::from(*self);
187+
if bits == 0 {
188+
return <Ref<$reftype>>::Null;
189+
}
190+
<Ref<$reftype>>::Val(<Self as ReadAs<$reftype>>::read_as(self))
191+
}
192+
}
193+
194+
impl WriteAs<$reftype> for UntypedVal {
195+
fn write_as(&mut self, value: $reftype) {
196+
let bits = unsafe { mem::transmute::<$reftype, u64>(value) };
197+
self.write_as(bits)
198+
}
199+
}
200+
201+
impl WriteAs<Ref<$reftype>> for UntypedVal {
202+
fn write_as(&mut self, value: Ref<$reftype>) {
203+
match value {
204+
Ref::Null => self.write_as(0_u64),
205+
Ref::Val(value) => self.write_as(value),
206+
}
207+
}
208+
}
209+
177210
impl From<UntypedVal> for Ref<$reftype> {
178211
fn from(untyped: UntypedVal) -> Self {
179212
if u64::from(untyped) == 0 {

0 commit comments

Comments
 (0)