Last active
October 15, 2019 21:27
-
-
Save yurydelendik/2c1bd0fa76642712203f9ece3680a104 to your computer and use it in GitHub Desktop.
helloS.expanded.rs
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
#![feature(prelude_import)] | |
#![no_std] | |
#![doc = " Translation of hello example"] | |
#[prelude_import] | |
use ::std::prelude::v1::*; | |
#[macro_use] | |
extern crate std as std; | |
use failure::{bail, format_err, Error}; | |
use std::cell::Ref; | |
use std::fs::read; | |
use wasmtime_api::*; | |
#[macro_use] | |
extern crate wasmtime_bindings_macro; | |
use wasmtime_bindings_common::*; | |
pub extern "C" fn callback(vmctx: *mut VMContext) { | |
let _closure = || { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling back...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["> Hello World!\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
}; | |
let _res = _closure(); | |
} | |
mod callback_mod { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
use ::wasmtime_bindings_common::get_host_call_conv; | |
use ::wasmtime_bindings_common::{InstanceHandle, InstanceHandleExport, VMContext}; | |
pub fn signature() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
pub struct Wrapper { | |
vmctx: *mut VMContext, | |
export: InstanceHandleExport, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle, export: InstanceHandleExport) -> Self { | |
Wrapper { | |
vmctx: instance.vmctx_mut_ptr(), | |
export, | |
} | |
} | |
pub fn call(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.export); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub fn metadata() -> ::wasmtime_bindings_common::FnMetadata { | |
::wasmtime_bindings_common::FnMetadata { | |
name: "callback", | |
signature: signature(), | |
address: super::callback as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
} | |
} | |
} | |
pub extern "C" fn hello(vmctx: *mut VMContext) { | |
let _closure = || { | |
{ | |
::std::rt::begin_panic( | |
"not yet implemented", | |
&("wasmtime-api/examples/hello2.rs", 21u32, 5u32), | |
) | |
}; | |
}; | |
let _res = _closure(); | |
} | |
mod hello_mod { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
use ::wasmtime_bindings_common::get_host_call_conv; | |
use ::wasmtime_bindings_common::{InstanceHandle, InstanceHandleExport, VMContext}; | |
pub fn signature() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
pub struct Wrapper { | |
vmctx: *mut VMContext, | |
export: InstanceHandleExport, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle, export: InstanceHandleExport) -> Self { | |
Wrapper { | |
vmctx: instance.vmctx_mut_ptr(), | |
export, | |
} | |
} | |
pub fn call(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.export); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub fn metadata() -> ::wasmtime_bindings_common::FnMetadata { | |
::wasmtime_bindings_common::FnMetadata { | |
name: "hello", | |
signature: signature(), | |
address: super::hello as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
} | |
} | |
} | |
fn main() -> Result<(), Error> { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Initializing...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let engine = HostRef::new(Engine::new(Config::default())); | |
let store = HostRef::new(Store::new(engine)); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Loading binary...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let binary = read("examples/hello.wasm")?; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Compiling module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let module = HostRef::new(Module::new(store.clone(), &binary).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error compiling module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Creating callback...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let hello_func = HostRef::new({ | |
use callback_mod as m; | |
let f = m::metadata(); | |
::wasmtime_api::Func::from_raw(store.clone(), f.address, f.signature) | |
}); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Instantiating module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let imports = <[_]>::into_vec(box [hello_func.into()]); | |
let instance = HostRef::new( | |
Instance::new(store.clone(), module, imports.as_slice()).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error instantiating module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?, | |
); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Extracting export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let exports = Ref::map(instance.borrow(), |instance| instance.exports()); | |
if exports.len() == 0 { | |
return Err(::failure::err_msg("> Error accessing exports!")); | |
} | |
let run_func = exports[0].func().ok_or_else(|| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error accessing exports!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let f = { | |
use hello_mod as m; | |
let (i, e) = run_func.borrow().raw_parts(); | |
m::Wrapper::new(i, e) | |
}; | |
f.call(); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Shutting down...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
drop(store); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Done.\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
Ok(()) | |
} |
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
#![feature(prelude_import)] | |
#![no_std] | |
#![doc = " Translation of hello example"] | |
#[prelude_import] | |
use ::std::prelude::v1::*; | |
#[macro_use] | |
extern crate std as std; | |
use failure::{format_err, Error}; | |
use std::fs::read; | |
use wasmtime_api::*; | |
#[macro_use] | |
extern crate wasmtime_bindings_macro; | |
trait Callback { | |
fn callback(&self); | |
} | |
mod callback_mod { | |
use super::*; | |
use ::std::boxed::Box; | |
use ::std::cell::{Ref, RefCell, RefMut}; | |
use ::wasmtime_bindings_common::{ | |
AbiPrimitive, InstanceHandle, InstanceHandleExport, VMContext, WasmMem, | |
}; | |
type Subject = dyn super::Callback; | |
pub struct State { | |
pub(super) subject: RefCell<Box<dyn super::Callback + 'static>>, | |
} | |
impl State { | |
fn from<'a>(vmctx: *mut VMContext) -> &'a mut Self { | |
unsafe { &mut *(&mut *vmctx).host_state().downcast_mut::<Self>().unwrap() } | |
} | |
} | |
fn get_self(vmctx: *mut VMContext) -> Ref<'static, Subject> { | |
use ::core::ops::Deref; | |
Ref::map(State::from(vmctx).subject.borrow(), |b| b.deref()) | |
} | |
fn get_self_mut(vmctx: *mut VMContext) -> RefMut<'static, Subject> { | |
use ::core::ops::DerefMut; | |
RefMut::map(State::from(vmctx).subject.borrow_mut(), |b| b.deref_mut()) | |
} | |
pub extern "C" fn callback(vmctx: *mut VMContext) { | |
let _self = get_self(vmctx); | |
let _res = _self.callback(); | |
} | |
pub struct Wrapper { | |
callback: InstanceHandleExport, | |
vmctx: *mut VMContext, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle) -> Self { | |
Wrapper { | |
callback: instance.lookup("callback").unwrap(), | |
vmctx: instance.vmctx_mut_ptr(), | |
} | |
} | |
pub fn callback(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.callback); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub mod signatures { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
pub fn callback() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
} | |
pub fn metadata() -> Vec<::wasmtime_bindings_common::FnMetadata> { | |
<[_]>::into_vec(box [::wasmtime_bindings_common::FnMetadata { | |
name: "callback", | |
signature: signatures::callback(), | |
address: callback as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
}]) | |
} | |
} | |
struct CallbackImpl; | |
impl Callback for CallbackImpl { | |
fn callback(&self) { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling back...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["> Hello World!\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
} | |
} | |
trait Hello { | |
fn run(&self); | |
} | |
mod hello_mod { | |
use super::*; | |
use ::std::boxed::Box; | |
use ::std::cell::{Ref, RefCell, RefMut}; | |
use ::wasmtime_bindings_common::{ | |
AbiPrimitive, InstanceHandle, InstanceHandleExport, VMContext, WasmMem, | |
}; | |
type Subject = dyn super::Hello; | |
pub struct State { | |
pub(super) subject: RefCell<Box<dyn super::Hello + 'static>>, | |
} | |
impl State { | |
fn from<'a>(vmctx: *mut VMContext) -> &'a mut Self { | |
unsafe { &mut *(&mut *vmctx).host_state().downcast_mut::<Self>().unwrap() } | |
} | |
} | |
fn get_self(vmctx: *mut VMContext) -> Ref<'static, Subject> { | |
use ::core::ops::Deref; | |
Ref::map(State::from(vmctx).subject.borrow(), |b| b.deref()) | |
} | |
fn get_self_mut(vmctx: *mut VMContext) -> RefMut<'static, Subject> { | |
use ::core::ops::DerefMut; | |
RefMut::map(State::from(vmctx).subject.borrow_mut(), |b| b.deref_mut()) | |
} | |
pub extern "C" fn run(vmctx: *mut VMContext) { | |
let _self = get_self(vmctx); | |
let _res = _self.run(); | |
} | |
pub struct Wrapper { | |
run: InstanceHandleExport, | |
vmctx: *mut VMContext, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle) -> Self { | |
Wrapper { | |
run: instance.lookup("run").unwrap(), | |
vmctx: instance.vmctx_mut_ptr(), | |
} | |
} | |
pub fn run(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.run); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub mod signatures { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
pub fn run() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
} | |
pub fn metadata() -> Vec<::wasmtime_bindings_common::FnMetadata> { | |
<[_]>::into_vec(box [::wasmtime_bindings_common::FnMetadata { | |
name: "run", | |
signature: signatures::run(), | |
address: run as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
}]) | |
} | |
} | |
fn main() -> Result<(), Error> { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Initializing...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let engine = HostRef::new(Engine::new(Config::default())); | |
let store = HostRef::new(Store::new(engine)); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Loading binary...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let binary = read("examples/hello.wasm")?; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Compiling module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let module = HostRef::new(Module::new(store.clone(), &binary).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error compiling module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Creating callback...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let callback_mod = HostRef::new( | |
{ | |
use callback_mod as m; | |
struct T; | |
impl ::wasmtime_api::HandleStateBuilder for T { | |
fn build_state( | |
&self, | |
imports: &[::wasmtime_api::Extern], | |
) -> Box<dyn std::any::Any> { | |
let imp = |_imports: &[::wasmtime_api::Extern]| CallbackImpl; | |
let state = m::State { | |
subject: ::std::cell::RefCell::new(::std::boxed::Box::new(imp(imports))), | |
}; | |
::std::boxed::Box::new(state) | |
} | |
} | |
let exports = m::metadata() | |
.into_iter() | |
.map(|f| (String::from(f.name), f.signature, f.address)) | |
.collect::<Vec<_>>(); | |
::wasmtime_api::Module::from_raw_parts(store.clone(), &exports, ::std::rc::Rc::new(T)) | |
} | |
.map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error compiling callback module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?, | |
); | |
let callback_instance = Instance::new(store.clone(), callback_mod, &[]).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error instantiating callback module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?; | |
let hello_func = &callback_instance.exports()[0]; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Instantiating module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let imports = <[_]>::into_vec(box [hello_func.clone()]); | |
let instance = HostRef::new( | |
Instance::new(store.clone(), module, imports.as_slice()).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error instantiating module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?, | |
); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Extracting export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let hello = { | |
use hello_mod as m; | |
let handle = instance.borrow().handle().clone(); | |
m::Wrapper::new(handle) | |
}; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
hello.run(); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Shutting down...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
drop(store); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Done.\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
Ok(()) | |
} |
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
#![feature(prelude_import)] | |
#![no_std] | |
#![doc = " Translation of hello example"] | |
#[prelude_import] | |
use ::std::prelude::v1::*; | |
#[macro_use] | |
extern crate std as std; | |
use failure::{format_err, Error}; | |
use std::fs::read; | |
use wasmtime_api::*; | |
#[macro_use] | |
extern crate wasmtime_bindings_macro; | |
struct Callback; | |
impl Callback { | |
fn callback(&self) { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling back...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["> Hello World!\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
} | |
} | |
mod callback_mod { | |
use super::*; | |
use ::std::boxed::Box; | |
use ::std::cell::{Ref, RefCell, RefMut}; | |
use ::wasmtime_bindings_common::{ | |
AbiPrimitive, InstanceHandle, InstanceHandleExport, VMContext, WasmMem, | |
}; | |
type Subject = super::Callback; | |
pub struct State { | |
pub(super) subject: RefCell<Box<super::Callback>>, | |
} | |
impl State { | |
fn from<'a>(vmctx: *mut VMContext) -> &'a mut Self { | |
unsafe { &mut *(&mut *vmctx).host_state().downcast_mut::<Self>().unwrap() } | |
} | |
} | |
fn get_self(vmctx: *mut VMContext) -> Ref<'static, Subject> { | |
use ::core::ops::Deref; | |
Ref::map(State::from(vmctx).subject.borrow(), |b| b.deref()) | |
} | |
fn get_self_mut(vmctx: *mut VMContext) -> RefMut<'static, Subject> { | |
use ::core::ops::DerefMut; | |
RefMut::map(State::from(vmctx).subject.borrow_mut(), |b| b.deref_mut()) | |
} | |
pub extern "C" fn callback(vmctx: *mut VMContext) { | |
let _self = get_self(vmctx); | |
let _res = _self.callback(); | |
} | |
pub struct Wrapper { | |
callback: InstanceHandleExport, | |
vmctx: *mut VMContext, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle) -> Self { | |
Wrapper { | |
callback: instance.lookup("callback").unwrap(), | |
vmctx: instance.vmctx_mut_ptr(), | |
} | |
} | |
pub fn callback(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.callback); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub mod signatures { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
pub fn callback() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
} | |
pub fn metadata() -> Vec<::wasmtime_bindings_common::FnMetadata> { | |
<[_]>::into_vec(box [::wasmtime_bindings_common::FnMetadata { | |
name: "callback", | |
signature: signatures::callback(), | |
address: callback as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
}]) | |
} | |
} | |
trait Hello { | |
fn run(&self); | |
} | |
mod hello_mod { | |
use super::*; | |
use ::std::boxed::Box; | |
use ::std::cell::{Ref, RefCell, RefMut}; | |
use ::wasmtime_bindings_common::{ | |
AbiPrimitive, InstanceHandle, InstanceHandleExport, VMContext, WasmMem, | |
}; | |
type Subject = dyn super::Hello; | |
pub struct State { | |
pub(super) subject: RefCell<Box<dyn super::Hello + 'static>>, | |
} | |
impl State { | |
fn from<'a>(vmctx: *mut VMContext) -> &'a mut Self { | |
unsafe { &mut *(&mut *vmctx).host_state().downcast_mut::<Self>().unwrap() } | |
} | |
} | |
fn get_self(vmctx: *mut VMContext) -> Ref<'static, Subject> { | |
use ::core::ops::Deref; | |
Ref::map(State::from(vmctx).subject.borrow(), |b| b.deref()) | |
} | |
fn get_self_mut(vmctx: *mut VMContext) -> RefMut<'static, Subject> { | |
use ::core::ops::DerefMut; | |
RefMut::map(State::from(vmctx).subject.borrow_mut(), |b| b.deref_mut()) | |
} | |
pub extern "C" fn run(vmctx: *mut VMContext) { | |
let _self = get_self(vmctx); | |
let _res = _self.run(); | |
} | |
pub struct Wrapper { | |
run: InstanceHandleExport, | |
vmctx: *mut VMContext, | |
} | |
impl Wrapper { | |
pub fn new(mut instance: InstanceHandle) -> Self { | |
Wrapper { | |
run: instance.lookup("run").unwrap(), | |
vmctx: instance.vmctx_mut_ptr(), | |
} | |
} | |
pub fn run(&self) { | |
type F = extern "C" fn(vmctx: *mut VMContext); | |
let (_f, vmctx) = ::wasmtime_bindings_common::get_body(&self.run); | |
let _f: F = unsafe { std::mem::transmute(_f) }; | |
let _res = _f(vmctx); | |
} | |
} | |
pub mod signatures { | |
use super::*; | |
use ::wasmtime_bindings_common::codegen::{ir, isa}; | |
pub fn run() -> ir::Signature { | |
let call_conv = ::wasmtime_bindings_common::get_host_call_conv(); | |
let mut sig = ir::Signature::new(call_conv); | |
sig.params.push(ir::AbiParam::special( | |
ir::types::I64, | |
ir::ArgumentPurpose::VMContext, | |
)); | |
sig | |
} | |
} | |
pub fn metadata() -> Vec<::wasmtime_bindings_common::FnMetadata> { | |
<[_]>::into_vec(box [::wasmtime_bindings_common::FnMetadata { | |
name: "run", | |
signature: signatures::run(), | |
address: run as extern "C" fn(vmctx: *mut VMContext) as *const _, | |
}]) | |
} | |
} | |
fn main() -> Result<(), Error> { | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Initializing...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let engine = HostRef::new(Engine::new(Config::default())); | |
let store = HostRef::new(Store::new(engine)); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Loading binary...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let binary = read("examples/hello.wasm")?; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Compiling module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let module = HostRef::new(Module::new(store.clone(), &binary).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error compiling module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Creating callback...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let callback_mod = HostRef::new( | |
{ | |
use callback_mod as m; | |
struct T; | |
impl ::wasmtime_api::HandleStateBuilder for T { | |
fn build_state( | |
&self, | |
imports: &[::wasmtime_api::Extern], | |
) -> Box<dyn std::any::Any> { | |
let imp = |_imports: &[::wasmtime_api::Extern]| Callback; | |
let state = m::State { | |
subject: ::std::cell::RefCell::new(::std::boxed::Box::new(imp(imports))), | |
}; | |
::std::boxed::Box::new(state) | |
} | |
} | |
let exports = m::metadata() | |
.into_iter() | |
.map(|f| (String::from(f.name), f.signature, f.address)) | |
.collect::<Vec<_>>(); | |
::wasmtime_api::Module::from_raw_parts(store.clone(), &exports, ::std::rc::Rc::new(T)) | |
} | |
.map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error compiling callback module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?, | |
); | |
let callback_instance = Instance::new(store.clone(), callback_mod, &[]).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error instantiating callback module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?; | |
let hello_func = &callback_instance.exports()[0]; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Instantiating module...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let imports = <[_]>::into_vec(box [hello_func.clone()]); | |
let instance = HostRef::new( | |
Instance::new(store.clone(), module, imports.as_slice()).map_err(|_| { | |
::failure::err_msg(::alloc::fmt::format(::core::fmt::Arguments::new_v1( | |
&["> Error instantiating module!"], | |
&match () { | |
() => [], | |
}, | |
))) | |
})?, | |
); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Extracting export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
let hello = { | |
use hello_mod as m; | |
let handle = instance.borrow().handle().clone(); | |
m::Wrapper::new(handle) | |
}; | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Calling export...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
hello.run(); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Shutting down...\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
drop(store); | |
{ | |
::std::io::_print(::core::fmt::Arguments::new_v1( | |
&["Done.\n"], | |
&match () { | |
() => [], | |
}, | |
)); | |
}; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment