MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / serialize_and_call

Function serialize_and_call

tests/all/module_serialize.rs:96–125  ·  view source on GitHub ↗
(wat: &str)

Source from the content-addressed store, hash-verified

94 return Ok(());
95
96 fn serialize_and_call(wat: &str) -> Result<()> {
97 let mut store = Store::<()>::default();
98 let td = tempfile::TempDir::new()?;
99 let buffer = serialize(store.engine(), wat)?;
100
101 let path = td.path().join("module.bin");
102 fs::write(&path, &buffer)?;
103 let module = unsafe { Module::deserialize_file(store.engine(), &path)? };
104 let instance = Instance::new(&mut store, &module, &[])?;
105 let func = instance.get_typed_func::<(), i32>(&mut store, "run")?;
106 assert_eq!(func.call(&mut store, ())?, 42);
107
108 // Try an already opened file as well.
109 let mut open_options = OpenOptions::new();
110 open_options.read(true);
111 #[cfg(target_os = "windows")]
112 {
113 use std::os::windows::prelude::*;
114 use windows_sys::Win32::Storage::FileSystem::*;
115 open_options.access_mode(FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
116 }
117
118 let file = open_options.open(&path)?;
119 let module = unsafe { Module::deserialize_open_file(store.engine(), file)? };
120 let instance = Instance::new(&mut store, &module, &[])?;
121 let func = instance.get_typed_func::<(), i32>(&mut store, "run")?;
122 assert_eq!(func.call(&mut store, ())?, 42);
123
124 Ok(())
125 }
126}
127
128#[test]

Callers 1

Calls 9

OkFunction · 0.85
serializeFunction · 0.70
newFunction · 0.50
writeFunction · 0.50
engineMethod · 0.45
joinMethod · 0.45
pathMethod · 0.45
readMethod · 0.45
openMethod · 0.45

Tested by 1