Skip to content

Commit 899cc32

Browse files
eira-franshampepyakin
authored andcommitted
rustfmt (#151)
1 parent da558c7 commit 899cc32

31 files changed

+9717
-9528
lines changed

benches/build.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
use std::env;
22
use std::process;
33

4-
54
fn main() {
6-
println!("cargo:rerun-if-changed=./wasm-kernel/");
5+
println!("cargo:rerun-if-changed=./wasm-kernel/");
76

8-
// The CARGO environment variable provides a path to the executable that
9-
// runs this build process.
10-
let cargo_bin = env::var("CARGO").expect("CARGO env variable should be defined");
7+
// The CARGO environment variable provides a path to the executable that
8+
// runs this build process.
9+
let cargo_bin = env::var("CARGO").expect("CARGO env variable should be defined");
1110

12-
// Build a release version of wasm-kernel. The code in the output wasm binary
13-
// will be used in benchmarks.
14-
let output = process::Command::new(cargo_bin)
15-
.arg("build")
16-
.arg("--target=wasm32-unknown-unknown")
17-
.arg("--release")
18-
.arg("--manifest-path=./wasm-kernel/Cargo.toml")
19-
.arg("--verbose")
20-
.output()
21-
.expect("failed to execute `cargo`");
11+
// Build a release version of wasm-kernel. The code in the output wasm binary
12+
// will be used in benchmarks.
13+
let output = process::Command::new(cargo_bin)
14+
.arg("build")
15+
.arg("--target=wasm32-unknown-unknown")
16+
.arg("--release")
17+
.arg("--manifest-path=./wasm-kernel/Cargo.toml")
18+
.arg("--verbose")
19+
.output()
20+
.expect("failed to execute `cargo`");
2221

23-
if !output.status.success() {
24-
let msg = format!(
25-
"status: {status}\nstdout: {stdout}\nstderr: {stderr}\n",
26-
status=output.status,
27-
stdout=String::from_utf8_lossy(&output.stdout),
28-
stderr=String::from_utf8_lossy(&output.stderr),
29-
);
30-
panic!("{}", msg);
31-
}
22+
if !output.status.success() {
23+
let msg = format!(
24+
"status: {status}\nstdout: {stdout}\nstderr: {stderr}\n",
25+
status = output.status,
26+
stdout = String::from_utf8_lossy(&output.stdout),
27+
stderr = String::from_utf8_lossy(&output.stderr),
28+
);
29+
panic!("{}", msg);
30+
}
3231
}

examples/interpret.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ extern crate wasmi;
44

55
use std::env::args;
66
use std::fs::File;
7-
use wasmi::{ModuleInstance, NopExternals, RuntimeValue, ImportsBuilder, Module};
7+
use wasmi::{ImportsBuilder, Module, ModuleInstance, NopExternals, RuntimeValue};
88

99
fn load_from_file(filename: &str) -> Module {
10-
use std::io::prelude::*;
11-
let mut file = File::open(filename).unwrap();
12-
let mut buf = Vec::new();
13-
file.read_to_end(&mut buf).unwrap();
14-
Module::from_buffer(buf).unwrap()
10+
use std::io::prelude::*;
11+
let mut file = File::open(filename).unwrap();
12+
let mut buf = Vec::new();
13+
file.read_to_end(&mut buf).unwrap();
14+
Module::from_buffer(buf).unwrap()
1515
}
1616

1717
fn main() {
@@ -27,10 +27,10 @@ fn main() {
2727
let module = load_from_file(&args[1]);
2828

2929
// Intialize deserialized module. It adds module into It expects 3 parameters:
30-
// - a name for the module
31-
// - a module declaration
32-
// - "main" module doesn't import native module(s) this is why we don't need to provide external native modules here
33-
// This test shows how to implement native module https://github.com/NikVolf/parity-wasm/blob/master/src/interpreter/tests/basics.rs#L197
30+
// - a name for the module
31+
// - a module declaration
32+
// - "main" module doesn't import native module(s) this is why we don't need to provide external native modules here
33+
// This test shows how to implement native module https://github.com/NikVolf/parity-wasm/blob/master/src/interpreter/tests/basics.rs#L197
3434
let main = ModuleInstance::new(&module, &ImportsBuilder::default())
3535
.expect("Failed to instantiate module")
3636
.run_start(&mut NopExternals)
@@ -40,5 +40,8 @@ fn main() {
4040
let argument: i32 = args[2].parse().expect("Integer argument required");
4141

4242
// "_call" export of function to be executed with an i32 argument and prints the result of execution
43-
println!("Result: {:?}", main.invoke_export("_call", &[RuntimeValue::I32(argument)], &mut NopExternals));
43+
println!(
44+
"Result: {:?}",
45+
main.invoke_export("_call", &[RuntimeValue::I32(argument)], &mut NopExternals)
46+
);
4447
}

examples/invoke.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ extern crate wasmi;
33

44
use std::env::args;
55

6-
use parity_wasm::elements::{Internal, External, Type, FunctionType, ValueType};
7-
use wasmi::{RuntimeValue, ModuleInstance, NopExternals, ImportsBuilder};
8-
6+
use parity_wasm::elements::{External, FunctionType, Internal, Type, ValueType};
7+
use wasmi::{ImportsBuilder, ModuleInstance, NopExternals, RuntimeValue};
98

109
fn main() {
1110
let args: Vec<_> = args().collect();
@@ -23,14 +22,19 @@ fn main() {
2322
// Export section has an entry with a func_name with an index inside a module
2423
let export_section = module.export_section().expect("No export section found");
2524
// It's a section with function declarations (which are references to the type section entries)
26-
let function_section = module.function_section().expect("No function section found");
25+
let function_section = module
26+
.function_section()
27+
.expect("No function section found");
2728
// Type section stores function types which are referenced by function_section entries
2829
let type_section = module.type_section().expect("No type section found");
2930

3031
// Given function name used to find export section entry which contains
3132
// an `internal` field which points to the index in the function index space
32-
let found_entry = export_section.entries().iter()
33-
.find(|entry| func_name == entry.field()).expect(&format!("No export with name {} found", func_name));
33+
let found_entry = export_section
34+
.entries()
35+
.iter()
36+
.find(|entry| func_name == entry.field())
37+
.expect(&format!("No export with name {} found", func_name));
3438

3539
// Function index in the function index space (internally-defined + imported)
3640
let function_index: usize = match found_entry.internal() {
@@ -41,32 +45,59 @@ fn main() {
4145
// We need to count import section entries (functions only!) to subtract it from function_index
4246
// and obtain the index within the function section
4347
let import_section_len: usize = match module.import_section() {
44-
Some(import) =>
45-
import.entries().iter().filter(|entry| match entry.external() {
48+
Some(import) => import
49+
.entries()
50+
.iter()
51+
.filter(|entry| match entry.external() {
4652
&External::Function(_) => true,
4753
_ => false,
48-
}).count(),
54+
})
55+
.count(),
4956
None => 0,
5057
};
5158

5259
// Calculates a function index within module's function section
5360
let function_index_in_section = function_index - import_section_len;
5461

5562
// Getting a type reference from a function section entry
56-
let func_type_ref: usize = function_section.entries()[function_index_in_section].type_ref() as usize;
63+
let func_type_ref: usize =
64+
function_section.entries()[function_index_in_section].type_ref() as usize;
5765

5866
// Use the reference to get an actual function type
5967
let function_type: &FunctionType = match &type_section.types()[func_type_ref] {
6068
&Type::Function(ref func_type) => func_type,
6169
};
6270

6371
// Parses arguments and constructs runtime values in correspondence of their types
64-
function_type.params().iter().enumerate().map(|(i, value)| match value {
65-
&ValueType::I32 => RuntimeValue::I32(program_args[i].parse::<i32>().expect(&format!("Can't parse arg #{} as i32", program_args[i]))),
66-
&ValueType::I64 => RuntimeValue::I64(program_args[i].parse::<i64>().expect(&format!("Can't parse arg #{} as i64", program_args[i]))),
67-
&ValueType::F32 => RuntimeValue::F32(program_args[i].parse::<f32>().expect(&format!("Can't parse arg #{} as f32", program_args[i])).into()),
68-
&ValueType::F64 => RuntimeValue::F64(program_args[i].parse::<f64>().expect(&format!("Can't parse arg #{} as f64", program_args[i])).into()),
69-
}).collect::<Vec<RuntimeValue>>()
72+
function_type
73+
.params()
74+
.iter()
75+
.enumerate()
76+
.map(|(i, value)| match value {
77+
&ValueType::I32 => RuntimeValue::I32(
78+
program_args[i]
79+
.parse::<i32>()
80+
.expect(&format!("Can't parse arg #{} as i32", program_args[i])),
81+
),
82+
&ValueType::I64 => RuntimeValue::I64(
83+
program_args[i]
84+
.parse::<i64>()
85+
.expect(&format!("Can't parse arg #{} as i64", program_args[i])),
86+
),
87+
&ValueType::F32 => RuntimeValue::F32(
88+
program_args[i]
89+
.parse::<f32>()
90+
.expect(&format!("Can't parse arg #{} as f32", program_args[i]))
91+
.into(),
92+
),
93+
&ValueType::F64 => RuntimeValue::F64(
94+
program_args[i]
95+
.parse::<f64>()
96+
.expect(&format!("Can't parse arg #{} as f64", program_args[i]))
97+
.into(),
98+
),
99+
})
100+
.collect::<Vec<RuntimeValue>>()
70101
};
71102

72103
let loaded_module = wasmi::Module::from_parity_wasm_module(module).expect("Module to be valid");
@@ -81,5 +112,9 @@ fn main() {
81112
.run_start(&mut NopExternals)
82113
.expect("Failed to run start function in module");
83114

84-
println!("Result: {:?}", main.invoke_export(func_name, &args, &mut NopExternals).expect(""));
115+
println!(
116+
"Result: {:?}",
117+
main.invoke_export(func_name, &args, &mut NopExternals)
118+
.expect("")
119+
);
85120
}

0 commit comments

Comments
 (0)