Skip to content

Commit 5913761

Browse files
authored
wasmtime-wizer: add type_hints to global_get (#12314)
InstanceState::global_get now takes a parameter to tell the system what type this global expects in JS, WebAssembly.Instance always returns number which has to be converted for snapshotting
1 parent 67ce1ef commit 5913761

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

crates/wizer/src/component/snapshot.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use wasmparser::ValType;
2+
13
use crate::component::info::Accessor;
24
use crate::component::{ComponentContext, ComponentInstanceState, WIZER_INSTANCE};
35
use crate::snapshot::Snapshot;
@@ -80,7 +82,7 @@ impl<S> InstanceState for ViaComponent<'_, '_, S>
8082
where
8183
S: ComponentInstanceState,
8284
{
83-
async fn global_get(&mut self, name: &str) -> SnapshotVal {
85+
async fn global_get(&mut self, name: &str, _: ValType) -> SnapshotVal {
8486
let Accessor::Global {
8587
accessor_export_name,
8688
ty,

crates/wizer/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub use crate::info::ModuleContext;
2424
pub use crate::snapshot::SnapshotVal;
2525
use ::wasmtime::{Result, bail, error::Context as _};
2626
use std::collections::{HashMap, HashSet};
27+
pub use wasmparser::ValType;
2728

2829
const DEFAULT_KEEP_INIT_FUNC: bool = false;
2930

@@ -377,7 +378,11 @@ pub trait InstanceState {
377378
///
378379
/// This function panics if `name` isn't an exported global or if the type
379380
/// of the global doesn't fit in `SnapshotVal`.
380-
fn global_get(&mut self, name: &str) -> impl Future<Output = SnapshotVal> + Send;
381+
fn global_get(
382+
&mut self,
383+
name: &str,
384+
type_hint: ValType,
385+
) -> impl Future<Output = SnapshotVal> + Send;
381386

382387
/// Loads the contents of the memory specified by `name`, returning the
383388
/// entier contents as a `Vec<u8>`.

crates/wizer/src/snapshot.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ async fn snapshot_globals(
7575
log::debug!("Snapshotting global values");
7676

7777
let mut ret = Vec::new();
78-
for (i, name) in module.defined_global_exports.as_ref().unwrap().iter() {
79-
let val = ctx.global_get(&name).await;
80-
ret.push((*i, val));
78+
for (i, ty, name) in module.defined_globals() {
79+
if let Some(name) = name {
80+
let val = ctx.global_get(name, ty.content_type).await;
81+
ret.push((i, val));
82+
}
8183
}
8284
ret
8385
}

crates/wizer/src/wasmtime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{InstanceState, SnapshotVal, Wizer};
2+
use wasmparser::ValType;
23
use wasmtime::error::Context;
4+
35
use wasmtime::{Extern, Instance, Module, Result, Store, Val};
46

57
impl Wizer {
@@ -93,7 +95,7 @@ pub struct WasmtimeWizer<'a, T: 'static> {
9395
}
9496

9597
impl<T: Send> InstanceState for WasmtimeWizer<'_, T> {
96-
async fn global_get(&mut self, name: &str) -> SnapshotVal {
98+
async fn global_get(&mut self, name: &str, _: ValType) -> SnapshotVal {
9799
let global = self.instance.get_global(&mut *self.store, name).unwrap();
98100
match global.get(&mut *self.store) {
99101
Val::I32(x) => SnapshotVal::I32(x),

0 commit comments

Comments
 (0)