Skip to content

Commit 65a7d1f

Browse files
committed
Dedup signatures and make them const
1 parent c0640c2 commit 65a7d1f

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

packages/vm/src/environment.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ mod test {
283283
use crate::backend::Storage;
284284
use crate::conversion::ref_to_u32;
285285
use crate::errors::VmError;
286+
use crate::signatures::*;
286287
use crate::size::Size;
287288
use crate::testing::{MockQuerier, MockStorage};
288289
#[cfg(feature = "metering")]
@@ -291,7 +292,7 @@ mod test {
291292
use cosmwasm_std::{
292293
coins, from_binary, to_vec, AllBalanceResponse, BankQuery, Empty, HumanAddr, QueryRequest,
293294
};
294-
use wasmer::{imports, Function, FunctionType, Instance as WasmerInstance, Type, Val};
295+
use wasmer::{imports, Function, Instance as WasmerInstance, Val};
295296

296297
static CONTRACT: &[u8] = include_bytes!("../testdata/contract.wasm");
297298

@@ -316,24 +317,18 @@ mod test {
316317

317318
let module = compile(&CONTRACT, Some(TESTING_MEMORY_LIMIT)).unwrap();
318319
let store = module.store();
319-
let i32_to_void = FunctionType::new(vec![Type::I32], vec![]);
320-
let i32_to_i32 = FunctionType::new(vec![Type::I32], vec![Type::I32]);
321-
let i32i32_to_void = FunctionType::new(vec![Type::I32, Type::I32], vec![]);
322-
let i32i32_to_i32 = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]);
323-
let i32i32i32_to_i32 =
324-
FunctionType::new(vec![Type::I32, Type::I32, Type::I32], vec![Type::I32]);
325320
// we need stubs for all required imports
326321
let import_obj = imports! {
327322
"env" => {
328-
"db_read" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
329-
"db_write" => Function::new(store, &i32i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
330-
"db_remove" => Function::new(store, &i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
331-
"db_scan" => Function::new(store, &i32i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
332-
"db_next" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
333-
"query_chain" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
334-
"canonicalize_address" => Function::new(store, &i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
335-
"humanize_address" => Function::new(store, &i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
336-
"debug" => Function::new(store, &i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
323+
"db_read" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
324+
"db_write" => Function::new(store, I32_I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
325+
"db_remove" => Function::new(store, I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
326+
"db_scan" => Function::new(store, I32_I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
327+
"db_next" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
328+
"query_chain" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
329+
"canonicalize_address" => Function::new(store, I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
330+
"humanize_address" => Function::new(store, I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
331+
"debug" => Function::new(store, I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
337332
},
338333
};
339334
let instance = Box::from(WasmerInstance::new(&module, &import_obj).unwrap());

packages/vm/src/imports.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,11 @@ mod test {
292292
SystemError, SystemResult, WasmQuery,
293293
};
294294
use std::ptr::NonNull;
295-
use wasmer::{imports, Function, FunctionType, Instance as WasmerInstance, Type, Val};
295+
use wasmer::{imports, Function, Instance as WasmerInstance, Val};
296296

297297
use crate::backend::{BackendError, Storage};
298298
use crate::environment::move_into_environment;
299+
use crate::signatures::*;
299300
use crate::size::Size;
300301
use crate::testing::{MockApi, MockQuerier, MockStorage};
301302
use crate::wasm_backend::compile;
@@ -326,24 +327,18 @@ mod test {
326327

327328
let module = compile(&CONTRACT, Some(TESTING_MEMORY_LIMIT)).unwrap();
328329
let store = module.store();
329-
let i32_to_void = FunctionType::new(vec![Type::I32], vec![]);
330-
let i32_to_i32 = FunctionType::new(vec![Type::I32], vec![Type::I32]);
331-
let i32i32_to_void = FunctionType::new(vec![Type::I32, Type::I32], vec![]);
332-
let i32i32_to_i32 = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]);
333-
let i32i32i32_to_i32 =
334-
FunctionType::new(vec![Type::I32, Type::I32, Type::I32], vec![Type::I32]);
335330
// we need stubs for all required imports
336331
let import_obj = imports! {
337332
"env" => {
338-
"db_read" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
339-
"db_write" => Function::new(store, &i32i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
340-
"db_remove" => Function::new(store, &i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
341-
"db_scan" => Function::new(store, &i32i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
342-
"db_next" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
343-
"query_chain" => Function::new(store, &i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
344-
"canonicalize_address" => Function::new(store, &i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
345-
"humanize_address" => Function::new(store, &i32i32_to_i32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
346-
"debug" => Function::new(store, &i32_to_void, |_args: &[Val]| { Ok(vec![]) }),
333+
"db_read" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
334+
"db_write" => Function::new(store, I32_I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
335+
"db_remove" => Function::new(store, I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
336+
"db_scan" => Function::new(store, I32_I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
337+
"db_next" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
338+
"query_chain" => Function::new(store, I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
339+
"canonicalize_address" => Function::new(store, I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
340+
"humanize_address" => Function::new(store, I32_I32_TO_I32, |_args: &[Val]| { Ok(vec![Val::I32(0)]) }),
341+
"debug" => Function::new(store, I32_TO_VOID, |_args: &[Val]| { Ok(vec![]) }),
347342
},
348343
};
349344
let instance = Box::from(WasmerInstance::new(&module, &import_obj).unwrap());

packages/vm/src/instance.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::collections::HashSet;
22
use std::marker::PhantomData;
33
use std::ptr::NonNull;
44

5-
use wasmer::{
6-
Exports, Function, FunctionType, ImportObject, Instance as WasmerInstance, Module, Type, Val,
7-
};
5+
use wasmer::{Exports, Function, ImportObject, Instance as WasmerInstance, Module, Val};
86

97
use crate::backend::{Api, Backend, Querier, Storage};
108
use crate::conversion::{ref_to_u32, to_u32};
@@ -18,6 +16,7 @@ use crate::imports::{
1816
#[cfg(feature = "iterator")]
1917
use crate::imports::{native_db_next, native_db_scan};
2018
use crate::memory::{read_region, write_region};
19+
use crate::signatures::*;
2120
use crate::size::Size;
2221
use crate::wasm_backend::{compile, get_gas_left, set_gas_left};
2322

@@ -85,8 +84,6 @@ where
8584

8685
let env = Environment::new(gas_limit, print_debug);
8786

88-
let i32i32_to_i32 = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]);
89-
9087
let mut import_obj = ImportObject::new();
9188
let mut env_imports = Exports::new();
9289

@@ -121,7 +118,7 @@ where
121118
// Ownership of both input and output pointer is not transferred to the host.
122119
env_imports.insert(
123120
"canonicalize_address",
124-
Function::new_with_env(store, &i32i32_to_i32, env.clone(), move |env, args| {
121+
Function::new_with_env(store, I32_I32_TO_I32, env.clone(), move |env, args| {
125122
let source_ptr = ref_to_u32(&args[0])?;
126123
let destination_ptr = ref_to_u32(&args[1])?;
127124
let ptr =
@@ -136,7 +133,7 @@ where
136133
// Ownership of both input and output pointer is not transferred to the host.
137134
env_imports.insert(
138135
"humanize_address",
139-
Function::new_with_env(store, &i32i32_to_i32, env.clone(), move |env, args| {
136+
Function::new_with_env(store, I32_I32_TO_I32, env.clone(), move |env, args| {
140137
let source_ptr = ref_to_u32(&args[0])?;
141138
let destination_ptr = ref_to_u32(&args[1])?;
142139
let ptr = do_humanize_address::<A, S, Q>(api, &env, source_ptr, destination_ptr)?;

packages/vm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod memory;
1414
mod middleware;
1515
mod modules;
1616
mod serde;
17+
mod signatures;
1718
mod size;
1819
pub mod testing;
1920
mod wasm_backend;

packages/vm/src/signatures.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use wasmer::Type;
2+
3+
#[cfg(test)]
4+
pub const I32_TO_VOID: ([Type; 1], [Type; 0]) = ([Type::I32], []);
5+
#[cfg(test)]
6+
pub const I32_TO_I32: ([Type; 1], [Type; 1]) = ([Type::I32], [Type::I32]);
7+
#[cfg(test)]
8+
pub const I32_I32_TO_VOID: ([Type; 2], [Type; 0]) = ([Type::I32, Type::I32], []);
9+
pub const I32_I32_TO_I32: ([Type; 2], [Type; 1]) = ([Type::I32, Type::I32], [Type::I32]);
10+
#[cfg(test)]
11+
pub const I32_I32_I32_TO_I32: ([Type; 3], [Type; 1]) =
12+
([Type::I32, Type::I32, Type::I32], [Type::I32]);

0 commit comments

Comments
 (0)