Use try_from replacing cast in wasmtime-environ.

`try_from` is stable now, so cast is unnecessary.
This commit is contained in:
Ari Lotter
2019-06-08 08:55:16 -04:00
committed by Jakub Konka
parent e530a582af
commit 1158b5bd6c
4 changed files with 35 additions and 25 deletions

View File

@@ -16,7 +16,6 @@ cranelift-codegen = "0.30.0"
cranelift-entity = "0.30.0" cranelift-entity = "0.30.0"
cranelift-wasm = "0.30.0" cranelift-wasm = "0.30.0"
lightbeam = { path = "../lightbeam", optional = true } lightbeam = { path = "../lightbeam", optional = true }
cast = { version = "0.2.2", default-features = false }
failure = { version = "0.1.3", default-features = false } failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false } failure_derive = { version = "0.1.3", default-features = false }
indexmap = "1.0.2" indexmap = "1.0.2"

View File

@@ -1,8 +1,8 @@
use crate::module::{MemoryPlan, MemoryStyle, Module, TableStyle}; use crate::module::{MemoryPlan, MemoryStyle, Module, TableStyle};
use crate::vmoffsets::VMOffsets; use crate::vmoffsets::VMOffsets;
use crate::WASM_PAGE_SIZE; use crate::WASM_PAGE_SIZE;
use cast;
use core::clone::Clone; use core::clone::Clone;
use core::convert::TryFrom;
use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir; use cranelift_codegen::ir;
use cranelift_codegen::ir::condcodes::*; use cranelift_codegen::ir::condcodes::*;
@@ -353,8 +353,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(func); let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_table_index(index) { if let Some(def_index) = self.module.defined_table_index(index) {
let base_offset = let base_offset =
cast::i32(self.offsets.vmctx_vmtable_definition_base(def_index)).unwrap(); i32::try_from(self.offsets.vmctx_vmtable_definition_base(def_index)).unwrap();
let current_elements_offset = cast::i32( let current_elements_offset = i32::try_from(
self.offsets self.offsets
.vmctx_vmtable_definition_current_elements(def_index), .vmctx_vmtable_definition_current_elements(def_index),
) )
@@ -364,7 +364,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let from_offset = self.offsets.vmctx_vmtable_import_from(index); let from_offset = self.offsets.vmctx_vmtable_import_from(index);
let table = func.create_global_value(ir::GlobalValueData::Load { let table = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()), offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type, global_type: pointer_type,
readonly: true, readonly: true,
}); });
@@ -410,8 +410,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(func); let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_memory_index(index) { if let Some(def_index) = self.module.defined_memory_index(index) {
let base_offset = let base_offset =
cast::i32(self.offsets.vmctx_vmmemory_definition_base(def_index)).unwrap(); i32::try_from(self.offsets.vmctx_vmmemory_definition_base(def_index)).unwrap();
let current_length_offset = cast::i32( let current_length_offset = i32::try_from(
self.offsets self.offsets
.vmctx_vmmemory_definition_current_length(def_index), .vmctx_vmmemory_definition_current_length(def_index),
) )
@@ -421,7 +421,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let from_offset = self.offsets.vmctx_vmmemory_import_from(index); let from_offset = self.offsets.vmctx_vmmemory_import_from(index);
let memory = func.create_global_value(ir::GlobalValueData::Load { let memory = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()), offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type, global_type: pointer_type,
readonly: true, readonly: true,
}); });
@@ -488,13 +488,14 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let (ptr, offset) = { let (ptr, offset) = {
let vmctx = self.vmctx(func); let vmctx = self.vmctx(func);
if let Some(def_index) = self.module.defined_global_index(index) { 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) (vmctx, offset)
} else { } else {
let from_offset = self.offsets.vmctx_vmglobal_import_from(index); let from_offset = self.offsets.vmctx_vmglobal_import_from(index);
let global = func.create_global_value(ir::GlobalValueData::Load { let global = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(cast::i32(from_offset).unwrap()), offset: Offset32::new(i32::try_from(from_offset).unwrap()),
global_type: pointer_type, global_type: pointer_type,
readonly: true, readonly: true,
}); });
@@ -548,7 +549,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let vmctx = self.vmctx(pos.func); let vmctx = self.vmctx(pos.func);
let base = pos.ins().global_value(pointer_type, vmctx); let base = pos.ins().global_value(pointer_type, vmctx);
let offset = 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. // Load the caller ID.
let mut mem_flags = ir::MemFlags::trusted(); let mut mem_flags = ir::MemFlags::trusted();
@@ -627,12 +628,12 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
// Load the callee address. // Load the callee address.
let body_offset = 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); let func_addr = pos.ins().load(pointer_type, mem_flags, base, body_offset);
// First append the callee vmctx address. // First append the callee vmctx address.
let vmctx_offset = 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); let vmctx = pos.ins().load(pointer_type, mem_flags, base, vmctx_offset);
real_call_args.push(vmctx); real_call_args.push(vmctx);

View File

@@ -1,6 +1,7 @@
use crate::func_environ::FuncEnvironment; use crate::func_environ::FuncEnvironment;
use crate::module::{Export, MemoryPlan, Module, TableElements, TablePlan}; use crate::module::{Export, MemoryPlan, Module, TableElements, TablePlan};
use crate::tunables::Tunables; use crate::tunables::Tunables;
use core::convert::TryFrom;
use cranelift_codegen::ir; use cranelift_codegen::ir;
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose}; use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
use cranelift_codegen::isa::TargetFrontendConfig; use cranelift_codegen::isa::TargetFrontendConfig;
@@ -93,7 +94,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result self.result
.module .module
.signatures .signatures
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_signature(&mut self, sig: ir::Signature) { fn declare_signature(&mut self, sig: ir::Signature) {
@@ -168,10 +169,13 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
} }
fn reserve_func_types(&mut self, num: u32) { 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 self.result
.function_body_inputs .function_body_inputs
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_func_type(&mut self, sig_index: SignatureIndex) { fn declare_func_type(&mut self, sig_index: SignatureIndex) {
@@ -182,7 +186,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result self.result
.module .module
.table_plans .table_plans
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_table(&mut self, table: Table) { fn declare_table(&mut self, table: Table) {
@@ -194,7 +198,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result self.result
.module .module
.memory_plans .memory_plans
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_memory(&mut self, memory: Memory) { fn declare_memory(&mut self, memory: Memory) {
@@ -203,7 +207,10 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
} }
fn reserve_globals(&mut self, num: u32) { 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) { fn declare_global(&mut self, global: Global) {
@@ -211,7 +218,10 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
} }
fn reserve_exports(&mut self, num: u32) { 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) { fn declare_func_export(&mut self, func_index: FuncIndex, name: &str) {
@@ -251,7 +261,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
self.result self.result
.module .module
.table_elements .table_elements
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_table_elements( fn declare_table_elements(
@@ -284,7 +294,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
fn reserve_data_initializers(&mut self, num: u32) { fn reserve_data_initializers(&mut self, num: u32) {
self.result self.result
.data_initializers .data_initializers
.reserve_exact(cast::usize(num)); .reserve_exact(usize::try_from(num).unwrap());
} }
fn declare_data_initialization( fn declare_data_initialization(

View File

@@ -2,7 +2,7 @@
//! module. //! module.
use crate::module::Module; use crate::module::Module;
use cast; use core::convert::TryFrom;
use cranelift_codegen::ir; use cranelift_codegen::ir;
use cranelift_wasm::{ use cranelift_wasm::{
DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex, MemoryIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex, MemoryIndex,
@@ -11,11 +11,11 @@ use cranelift_wasm::{
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
fn cast_to_u32(sz: usize) -> u32 { fn cast_to_u32(sz: usize) -> u32 {
cast::u32(sz) u32::try_from(sz).unwrap()
} }
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
fn cast_to_u32(sz: usize) -> u32 { fn cast_to_u32(sz: usize) -> u32 {
match cast::u32(sz) { match u32::try_from(sz) {
Ok(x) => x, Ok(x) => x,
Err(_) => panic!("overflow in cast from usize to u32"), Err(_) => panic!("overflow in cast from usize to u32"),
} }