forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasmtime: Add support for
func.ref
and table.grow
with funcref
s
Part of bytecodealliance#929
- Loading branch information
Showing
12 changed files
with
195 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::{VMCallerCheckedAnyfunc, VMExternRef}; | ||
use std::any::Any; | ||
use std::convert::TryFrom; | ||
use std::fmt; | ||
|
||
/// A `VMFuncRef` is a `funcref` as compiled Wasm code sees it. | ||
/// | ||
/// Internally, a `VMFuncRef` is a `VMExternRef` that always points to a | ||
/// `VMCallerCheckedAnyfunc`. This means that it is managed by the same GC | ||
/// discipline that `VMExternRef` is managed by. It also means that it is | ||
/// represented with a single word (either an `r32` or an `r64` depending on the | ||
/// target pointer width) and is therefore suitable for Wasm code to manipulate | ||
/// on-stack and pass into libcalls (since Cranelift does not support compound | ||
/// structures). | ||
#[derive(Clone)] | ||
#[repr(transparent)] | ||
pub struct VMFuncRef { | ||
inner: VMExternRef, | ||
} | ||
|
||
impl VMFuncRef { | ||
/// Create a new `funcref` from the given `VMCallerCheckedAnyfunc`. | ||
pub fn new(func: VMCallerCheckedAnyfunc) -> Self { | ||
VMFuncRef { | ||
inner: VMExternRef::new(func), | ||
} | ||
} | ||
|
||
/// Get this `funcref`'s inner `VMCallerCheckedAnyfunc`. | ||
pub fn as_func(&self) -> &VMCallerCheckedAnyfunc { | ||
// We always control the inner `VMExternRef`, so we know it is always a | ||
// `VMCallerCheckedAnyfunc`, and we can do an unsafe cast here. | ||
debug_assert!(self.inner.is::<VMCallerCheckedAnyfunc>()); | ||
let p: *const dyn Any = &*self.inner as _; | ||
let p = p as *const VMCallerCheckedAnyfunc; | ||
unsafe { &*p } | ||
} | ||
|
||
/// Get this `funcref`'s inner `VMExternRef`. | ||
pub fn as_externref(&self) -> &VMExternRef { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl fmt::Debug for VMFuncRef { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let func = self.as_func(); | ||
f.debug_struct("VMFuncRef").field("func", &func).finish() | ||
} | ||
} | ||
|
||
impl From<VMFuncRef> for VMExternRef { | ||
fn from(f: VMFuncRef) -> VMExternRef { | ||
f.inner | ||
} | ||
} | ||
|
||
impl TryFrom<VMExternRef> for VMFuncRef { | ||
type Error = VMExternRef; | ||
|
||
fn try_from(r: VMExternRef) -> Result<Self, Self::Error> { | ||
if r.is::<VMCallerCheckedAnyfunc>() { | ||
Ok(VMFuncRef { inner: r }) | ||
} else { | ||
Err(r) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.