Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Split SandboxInstance::get_global_val implementation per backend (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7CFE authored Apr 21, 2022
1 parent 6091c6b commit 286775a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
30 changes: 7 additions & 23 deletions client/executor/common/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ use std::{collections::HashMap, rc::Rc};

#[cfg(feature = "wasmer-sandbox")]
use wasmer_backend::{
instantiate as wasmer_instantiate, invoke as wasmer_invoke, new_memory as wasmer_new_memory,
Backend as WasmerBackend, MemoryWrapper as WasmerMemoryWrapper,
get_global as wasmer_get_global, instantiate as wasmer_instantiate, invoke as wasmer_invoke,
new_memory as wasmer_new_memory, Backend as WasmerBackend,
MemoryWrapper as WasmerMemoryWrapper,
};

use wasmi_backend::{
instantiate as wasmi_instantiate, invoke as wasmi_invoke, new_memory as wasmi_new_memory,
MemoryWrapper as WasmiMemoryWrapper,
get_global as wasmi_get_global, instantiate as wasmi_instantiate, invoke as wasmi_invoke,
new_memory as wasmi_new_memory, MemoryWrapper as WasmiMemoryWrapper,
};

/// Index of a function inside the supervisor.
Expand Down Expand Up @@ -213,27 +214,10 @@ impl SandboxInstance {
/// Returns `Some(_)` if the global could be found.
pub fn get_global_val(&self, name: &str) -> Option<sp_wasm_interface::Value> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => {
let wasmi_global = wasmi_instance.export_by_name(name)?.as_global()?.get();

Some(wasmi_global.into())
},
BackendInstance::Wasmi(wasmi_instance) => wasmi_get_global(wasmi_instance, name),

#[cfg(feature = "wasmer-sandbox")]
BackendInstance::Wasmer(wasmer_instance) => {
use sp_wasm_interface::Value;

let global = wasmer_instance.exports.get_global(name).ok()?;
let wasmtime_value = match global.get() {
wasmer::Val::I32(val) => Value::I32(val),
wasmer::Val::I64(val) => Value::I64(val),
wasmer::Val::F32(val) => Value::F32(f32::to_bits(val)),
wasmer::Val::F64(val) => Value::F64(f64::to_bits(val)),
_ => None?,
};

Some(wasmtime_value)
},
BackendInstance::Wasmer(wasmer_instance) => wasmer_get_global(wasmer_instance, name),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions client/executor/common/src/sandbox/wasmer_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,17 @@ impl MemoryTransfer for MemoryWrapper {
}
}
}

/// Get global value by name
pub fn get_global(instance: &wasmer::Instance, name: &str) -> Option<Value> {
let global = instance.exports.get_global(name).ok()?;
let wasmtime_value = match global.get() {
wasmer::Val::I32(val) => Value::I32(val),
wasmer::Val::I64(val) => Value::I64(val),
wasmer::Val::F32(val) => Value::F32(f32::to_bits(val)),
wasmer::Val::F64(val) => Value::F64(f64::to_bits(val)),
_ => None?,
};

Some(wasmtime_value)
}
5 changes: 5 additions & 0 deletions client/executor/common/src/sandbox/wasmi_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,8 @@ pub fn invoke(
})
})
}

/// Get global value by name
pub fn get_global(instance: &wasmi::ModuleRef, name: &str) -> Option<Value> {
Some(instance.export_by_name(name)?.as_global()?.get().into())
}

0 comments on commit 286775a

Please sign in to comment.