Skip to content

Commit

Permalink
Use try_from replacing cast in wasmtime-environ.
Browse files Browse the repository at this point in the history
`try_from` is stable now, so cast is unnecessary.
  • Loading branch information
arilotter authored and kubkon committed Jun 9, 2019
1 parent e530a58 commit 1158b5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
1 change: 0 additions & 1 deletion wasmtime-environ/Cargo.toml
Expand Up @@ -16,7 +16,6 @@ cranelift-codegen = "0.30.0"
cranelift-entity = "0.30.0"
cranelift-wasm = "0.30.0"
lightbeam = { path = "../lightbeam", optional = true }
cast = { version = "0.2.2", default-features = false }
failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false }
indexmap = "1.0.2"
Expand Down
25 changes: 13 additions & 12 deletions wasmtime-environ/src/func_environ.rs
@@ -1,8 +1,8 @@
use crate::module::{MemoryPlan, MemoryStyle, Module, TableStyle};
use crate::vmoffsets::VMOffsets;
use crate::WASM_PAGE_SIZE;
use cast;
use core::clone::Clone;
use core::convert::TryFrom;
use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir;
use cranelift_codegen::ir::condcodes::*;
Expand Down Expand Up @@ -353,8 +353,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_table_index(index) {
let base_offset =
cast::i32(self.offsets.vmctx_vmtable_definition_base(def_index)).unwrap();
let current_elements_offset = cast::i32(
i32::try_from(self.offsets.vmctx_vmtable_definition_base(def_index)).unwrap();
let current_elements_offset = i32::try_from(
self.offsets
.vmctx_vmtable_definition_current_elements(def_index),
)
Expand All @@ -364,7 +364,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let from_offset = self.offsets.vmctx_vmtable_import_from(index);
let table = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()),
offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type,
readonly: true,
});
Expand Down Expand Up @@ -410,8 +410,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_memory_index(index) {
let base_offset =
cast::i32(self.offsets.vmctx_vmmemory_definition_base(def_index)).unwrap();
let current_length_offset = cast::i32(
i32::try_from(self.offsets.vmctx_vmmemory_definition_base(def_index)).unwrap();
let current_length_offset = i32::try_from(
self.offsets
.vmctx_vmmemory_definition_current_length(def_index),
)
Expand All @@ -421,7 +421,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let from_offset = self.offsets.vmctx_vmmemory_import_from(index);
let memory = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()),
offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type,
readonly: true,
});
Expand Down Expand Up @@ -488,13 +488,14 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let (ptr, offset) = {
let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_global_index(index) {
let offset = cast::i32(self.offsets.vmctx_vmglobal_definition(def_index)).unwrap();
let offset =
i32::try_from(self.offsets.vmctx_vmglobal_definition(def_index)).unwrap();
(vmctx, offset)
} else {
let from_offset = self.offsets.vmctx_vmglobal_import_from(index);
let global = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()),
offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type,
readonly: true,
});
Expand Down Expand Up @@ -548,7 +549,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(pos.func);
let base = pos.ins().global_value(pointer_type, vmctx);
let offset =
cast::i32(self.offsets.vmctx_vmshared_signature_id(sig_index)).unwrap();
i32::try_from(self.offsets.vmctx_vmshared_signature_id(sig_index)).unwrap();

// Load the caller ID.
let mut mem_flags = ir::MemFlags::trusted();
Expand Down Expand Up @@ -627,12 +628,12 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m

// Load the callee address.
let body_offset =
cast::i32(self.offsets.vmctx_vmfunction_import_body(callee_index)).unwrap();
i32::try_from(self.offsets.vmctx_vmfunction_import_body(callee_index)).unwrap();
let func_addr = pos.ins().load(pointer_type, mem_flags, base, body_offset);

// First append the callee vmctx address.
let vmctx_offset =
cast::i32(self.offsets.vmctx_vmfunction_import_vmctx(callee_index)).unwrap();
i32::try_from(self.offsets.vmctx_vmfunction_import_vmctx(callee_index)).unwrap();
let vmctx = pos.ins().load(pointer_type, mem_flags, base, vmctx_offset);
real_call_args.push(vmctx);

Expand Down
28 changes: 19 additions & 9 deletions wasmtime-environ/src/module_environ.rs
@@ -1,6 +1,7 @@
use crate::func_environ::FuncEnvironment;
use crate::module::{Export, MemoryPlan, Module, TableElements, TablePlan};
use crate::tunables::Tunables;
use core::convert::TryFrom;
use cranelift_codegen::ir;
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
use cranelift_codegen::isa::TargetFrontendConfig;
Expand Down Expand Up @@ -93,7 +94,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result
.module
.signatures
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_signature(&mut self, sig: ir::Signature) {
Expand Down Expand Up @@ -168,10 +169,13 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
}

fn reserve_func_types(&mut self, num: u32) {
self.result.module.functions.reserve_exact(cast::usize(num));
self.result
.module
.functions
.reserve_exact(usize::try_from(num).unwrap());
self.result
.function_body_inputs
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_func_type(&mut self, sig_index: SignatureIndex) {
Expand All @@ -182,7 +186,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result
.module
.table_plans
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_table(&mut self, table: Table) {
Expand All @@ -194,7 +198,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result
.module
.memory_plans
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_memory(&mut self, memory: Memory) {
Expand All @@ -203,15 +207,21 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
}

fn reserve_globals(&mut self, num: u32) {
self.result.module.globals.reserve_exact(cast::usize(num));
self.result
.module
.globals
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_global(&mut self, global: Global) {
self.result.module.globals.push(global);
}

fn reserve_exports(&mut self, num: u32) {
self.result.module.exports.reserve(cast::usize(num));
self.result
.module
.exports
.reserve(usize::try_from(num).unwrap());
}

fn declare_func_export(&mut self, func_index: FuncIndex, name: &str) {
Expand Down Expand Up @@ -251,7 +261,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result
.module
.table_elements
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_table_elements(
Expand Down Expand Up @@ -284,7 +294,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
fn reserve_data_initializers(&mut self, num: u32) {
self.result
.data_initializers
.reserve_exact(cast::usize(num));
.reserve_exact(usize::try_from(num).unwrap());
}

fn declare_data_initialization(
Expand Down
6 changes: 3 additions & 3 deletions wasmtime-environ/src/vmoffsets.rs
Expand Up @@ -2,7 +2,7 @@
//! module.

use crate::module::Module;
use cast;
use core::convert::TryFrom;
use cranelift_codegen::ir;
use cranelift_wasm::{
DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex, MemoryIndex,
Expand All @@ -11,11 +11,11 @@ use cranelift_wasm::{

#[cfg(target_pointer_width = "32")]
fn cast_to_u32(sz: usize) -> u32 {
cast::u32(sz)
u32::try_from(sz).unwrap()
}
#[cfg(target_pointer_width = "64")]
fn cast_to_u32(sz: usize) -> u32 {
match cast::u32(sz) {
match u32::try_from(sz) {
Ok(x) => x,
Err(_) => panic!("overflow in cast from usize to u32"),
}
Expand Down

0 comments on commit 1158b5b

Please sign in to comment.