mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-04-22 05:38:00 +00:00
Cleanup (#162)
* Remove unnecessary references * Shorten expressions * Remove unnecessary reference * Simplify expression * Fix formatting
This commit is contained in:
+11
-11
@@ -109,7 +109,7 @@ impl Counter {
|
||||
self.finalize_metered_block(cursor)?;
|
||||
|
||||
// Pop the control block stack.
|
||||
let closing_control_block = self.stack.pop().ok_or_else(|| ())?;
|
||||
let closing_control_block = self.stack.pop().ok_or(())?;
|
||||
let closing_control_index = self.stack.len();
|
||||
|
||||
if self.stack.is_empty() {
|
||||
@@ -118,7 +118,7 @@ impl Counter {
|
||||
|
||||
// Update the lowest_forward_br_target for the control block now on top of the stack.
|
||||
{
|
||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
||||
let control_block = self.stack.last_mut().ok_or(())?;
|
||||
control_block.lowest_forward_br_target = min(
|
||||
control_block.lowest_forward_br_target,
|
||||
closing_control_block.lowest_forward_br_target,
|
||||
@@ -140,7 +140,7 @@ impl Counter {
|
||||
/// Finalized blocks have final cost which will not change later.
|
||||
fn finalize_metered_block(&mut self, cursor: usize) -> Result<(), ()> {
|
||||
let closing_metered_block = {
|
||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
||||
let control_block = self.stack.last_mut().ok_or(())?;
|
||||
mem::replace(
|
||||
&mut control_block.active_metered_block,
|
||||
MeteredBlock { start_pos: cursor + 1, cost: 0 },
|
||||
@@ -181,14 +181,14 @@ impl Counter {
|
||||
// Update the lowest_forward_br_target of the current control block.
|
||||
for &index in indices {
|
||||
let target_is_loop = {
|
||||
let target_block = self.stack.get(index).ok_or_else(|| ())?;
|
||||
let target_block = self.stack.get(index).ok_or(())?;
|
||||
target_block.is_loop
|
||||
};
|
||||
if target_is_loop {
|
||||
continue
|
||||
}
|
||||
|
||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
||||
let control_block = self.stack.last_mut().ok_or(())?;
|
||||
control_block.lowest_forward_br_target =
|
||||
min(control_block.lowest_forward_br_target, index);
|
||||
}
|
||||
@@ -203,14 +203,14 @@ impl Counter {
|
||||
|
||||
/// Get a reference to the currently active metered block.
|
||||
fn active_metered_block(&mut self) -> Result<&mut MeteredBlock, ()> {
|
||||
let top_block = self.stack.last_mut().ok_or_else(|| ())?;
|
||||
let top_block = self.stack.last_mut().ok_or(())?;
|
||||
Ok(&mut top_block.active_metered_block)
|
||||
}
|
||||
|
||||
/// Increment the cost of the current block by the specified value.
|
||||
fn increment(&mut self, val: u32) -> Result<(), ()> {
|
||||
let top_block = self.active_metered_block()?;
|
||||
top_block.cost = top_block.cost.checked_add(val).ok_or_else(|| ())?;
|
||||
top_block.cost = top_block.cost.checked_add(val).ok_or(())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -308,20 +308,20 @@ pub(crate) fn determine_metered_blocks<R: Rules>(
|
||||
counter.increment(instruction_cost)?;
|
||||
|
||||
// Label is a relative index into the control stack.
|
||||
let active_index = counter.active_control_block_index().ok_or_else(|| ())?;
|
||||
let target_index = active_index.checked_sub(*label as usize).ok_or_else(|| ())?;
|
||||
let active_index = counter.active_control_block_index().ok_or(())?;
|
||||
let target_index = active_index.checked_sub(*label as usize).ok_or(())?;
|
||||
counter.branch(cursor, &[target_index])?;
|
||||
},
|
||||
BrTable(br_table_data) => {
|
||||
counter.increment(instruction_cost)?;
|
||||
|
||||
let active_index = counter.active_control_block_index().ok_or_else(|| ())?;
|
||||
let active_index = counter.active_control_block_index().ok_or(())?;
|
||||
let target_indices = [br_table_data.default]
|
||||
.iter()
|
||||
.chain(br_table_data.table.iter())
|
||||
.map(|label| active_index.checked_sub(*label as usize))
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.ok_or_else(|| ())?;
|
||||
.ok_or(())?;
|
||||
counter.branch(cursor, &target_indices)?;
|
||||
},
|
||||
Return => {
|
||||
|
||||
+1
-1
@@ -723,7 +723,7 @@ fn custom_round(
|
||||
idx: &mut usize,
|
||||
sections: &mut Vec<elements::Section>,
|
||||
) {
|
||||
while let Some(other_section) = map.get(&idx) {
|
||||
while let Some(other_section) = map.get(idx) {
|
||||
sections.push(other_section.clone());
|
||||
*idx += 1;
|
||||
}
|
||||
|
||||
+7
-11
@@ -43,14 +43,14 @@ pub fn optimize(
|
||||
}
|
||||
|
||||
// If there is start function in module, it should stary
|
||||
module.start_section().map(|ss| stay.insert(resolve_function(&module, ss)));
|
||||
module.start_section().map(|ss| stay.insert(resolve_function(module, ss)));
|
||||
|
||||
// All symbols used in data/element segments are also should be preserved
|
||||
let mut init_symbols = Vec::new();
|
||||
if let Some(data_section) = module.data_section() {
|
||||
for segment in data_section.entries() {
|
||||
push_code_symbols(
|
||||
&module,
|
||||
module,
|
||||
segment
|
||||
.offset()
|
||||
.as_ref()
|
||||
@@ -63,7 +63,7 @@ pub fn optimize(
|
||||
if let Some(elements_section) = module.elements_section() {
|
||||
for segment in elements_section.entries() {
|
||||
push_code_symbols(
|
||||
&module,
|
||||
module,
|
||||
segment
|
||||
.offset()
|
||||
.as_ref()
|
||||
@@ -72,7 +72,7 @@ pub fn optimize(
|
||||
&mut init_symbols,
|
||||
);
|
||||
for func_index in segment.members() {
|
||||
stay.insert(resolve_function(&module, *func_index));
|
||||
stay.insert(resolve_function(module, *func_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -403,13 +403,9 @@ pub fn optimize(
|
||||
}
|
||||
|
||||
// Also drop all custom sections
|
||||
module.sections_mut().retain(|section| {
|
||||
if let elements::Section::Custom(_) = section {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
module
|
||||
.sections_mut()
|
||||
.retain(|section| !matches!(section, elements::Section::Custom(_)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+3
-4
@@ -120,7 +120,7 @@ pub fn pack_instance(
|
||||
mbuilder.push_import(
|
||||
builder::import()
|
||||
.module("env")
|
||||
.field(&target.symbols().ret)
|
||||
.field(target.symbols().ret)
|
||||
.external()
|
||||
.func(import_sig)
|
||||
.build(),
|
||||
@@ -172,11 +172,10 @@ pub fn pack_instance(
|
||||
let last_function_index = ctor_module.functions_space();
|
||||
|
||||
// We ensure here that module has the DataSection
|
||||
if ctor_module
|
||||
if !ctor_module
|
||||
.sections()
|
||||
.iter()
|
||||
.find(|section| matches!(**section, Section::Data(_)))
|
||||
.is_none()
|
||||
.any(|section| matches!(*section, Section::Data(_)))
|
||||
{
|
||||
// DataSection has to be the last non-custom section according the to the spec
|
||||
ctor_module
|
||||
|
||||
+1
-5
@@ -340,10 +340,6 @@ impl Rules for Set {
|
||||
}
|
||||
|
||||
fn memory_grow_cost(&self) -> Option<MemoryGrowCost> {
|
||||
if let Some(val) = NonZeroU32::new(self.grow) {
|
||||
Some(MemoryGrowCost::Linear(val))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
NonZeroU32::new(self.grow).map(MemoryGrowCost::Linear)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,10 +80,7 @@ impl Stack {
|
||||
/// Returns `Err` if the control stack is empty.
|
||||
fn pop_frame(&mut self) -> Result<Frame, Error> {
|
||||
trace!(target: "max_height", "pop_frame: {:?}", self.control_stack.last());
|
||||
Ok(self
|
||||
.control_stack
|
||||
.pop()
|
||||
.ok_or_else(|| Error("stack must be non-empty".into()))?)
|
||||
self.control_stack.pop().ok_or_else(|| Error("stack must be non-empty".into()))
|
||||
}
|
||||
|
||||
/// Truncate the height of value stack to the specified height.
|
||||
|
||||
@@ -176,7 +176,7 @@ fn compute_stack_costs(module: &elements::Module) -> Result<Vec<u32>, Error> {
|
||||
// We can't calculate stack_cost of the import functions.
|
||||
Ok(0)
|
||||
} else {
|
||||
compute_stack_cost(func_idx as u32, &module)
|
||||
compute_stack_cost(func_idx as u32, module)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
|
||||
Reference in New Issue
Block a user