Skip to content

Commit

Permalink
Merge branch 'master' into render_constrained_system_friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinuel committed Feb 9, 2021
2 parents 6366072 + 076d77a commit f26c9c5
Show file tree
Hide file tree
Showing 24 changed files with 488 additions and 389 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Rust - Continuous Integration

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: check

check_no_std:
name: Check (no_std)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features --features core

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Checkout Submodules
run: git submodule update --init --recursive
- uses: actions-rs/cargo@v1
env:
RUSTFLAGS: '--cfg debug_assertions'
with:
command: test
args: --release

fmt:
name: Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

clippy_no_std:
name: Clippy (no_std)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --no-default-features --features core -- -D warnings
28 changes: 13 additions & 15 deletions examples/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ fn main() {
.entries()
.iter()
.find(|entry| func_name == entry.field())
.expect(&format!("No export with name {} found", func_name));
.unwrap_or_else(|| panic!("No export with name {} found", func_name));

// Function index in the function index space (internally-defined + imported)
let function_index: usize = match found_entry.internal() {
&Internal::Function(index) => index as usize,
Internal::Function(index) => *index as usize,
_ => panic!("Founded export is not a function"),
};

Expand All @@ -48,10 +48,7 @@ fn main() {
Some(import) => import
.entries()
.iter()
.filter(|entry| match entry.external() {
&External::Function(_) => true,
_ => false,
})
.filter(|entry| matches!(entry.external(), External::Function(_)))
.count(),
None => 0,
};
Expand All @@ -64,8 +61,9 @@ fn main() {
function_section.entries()[function_index_in_section].type_ref() as usize;

// Use the reference to get an actual function type
#[allow(clippy::infallible_destructuring_match)]
let function_type: &FunctionType = match &type_section.types()[func_type_ref] {
&Type::Function(ref func_type) => func_type,
Type::Function(ref func_type) => func_type,
};

// Parses arguments and constructs runtime values in correspondence of their types
Expand All @@ -74,26 +72,26 @@ fn main() {
.iter()
.enumerate()
.map(|(i, value)| match value {
&ValueType::I32 => RuntimeValue::I32(
ValueType::I32 => RuntimeValue::I32(
program_args[i]
.parse::<i32>()
.expect(&format!("Can't parse arg #{} as i32", program_args[i])),
.unwrap_or_else(|_| panic!("Can't parse arg #{} as i32", program_args[i])),
),
&ValueType::I64 => RuntimeValue::I64(
ValueType::I64 => RuntimeValue::I64(
program_args[i]
.parse::<i64>()
.expect(&format!("Can't parse arg #{} as i64", program_args[i])),
.unwrap_or_else(|_| panic!("Can't parse arg #{} as i64", program_args[i])),
),
&ValueType::F32 => RuntimeValue::F32(
ValueType::F32 => RuntimeValue::F32(
program_args[i]
.parse::<f32>()
.expect(&format!("Can't parse arg #{} as f32", program_args[i]))
.unwrap_or_else(|_| panic!("Can't parse arg #{} as f32", program_args[i]))
.into(),
),
&ValueType::F64 => RuntimeValue::F64(
ValueType::F64 => RuntimeValue::F64(
program_args[i]
.parse::<f64>()
.expect(&format!("Can't parse arg #{} as f64", program_args[i]))
.unwrap_or_else(|_| panic!("Can't parse arg #{} as f64", program_args[i]))
.into(),
),
})
Expand Down
14 changes: 8 additions & 6 deletions examples/tictactoe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::wrong_self_convention)]

extern crate parity_wasm;
extern crate wasmi;

Expand Down Expand Up @@ -67,7 +69,7 @@ mod tictactoe {
}

pub fn set(&mut self, idx: i32, player: Player) -> Result<(), Error> {
if idx < 0 || idx > 9 {
if !(0..9).contains(&idx) {
return Err(Error::OutOfRange);
}
if self.board[idx as usize] != None {
Expand All @@ -78,7 +80,7 @@ mod tictactoe {
}

pub fn get(&self, idx: i32) -> Result<Option<Player>, Error> {
if idx < 0 || idx > 9 {
if !(0..9).contains(&idx) {
return Err(Error::OutOfRange);
}
Ok(self.board[idx as usize])
Expand All @@ -103,6 +105,7 @@ mod tictactoe {
];

// Returns Some(player) if all cells contain same Player.
#[allow(clippy::question_mark)]
let all_same = |i1: usize, i2: usize, i3: usize| -> Option<Player> {
if self.board[i1].is_none() {
return None;
Expand Down Expand Up @@ -221,14 +224,13 @@ fn play(
{
let mut runtime = Runtime {
player: turn_of,
game: game,
game,
};
let _ = instance.invoke_export("mk_turn", &[], &mut runtime)?;
}

match game.game_result() {
Some(game_result) => break game_result,
None => {}
if let Some(game_result) = game.game_result() {
break game_result;
}

turn_of = next_turn_of;
Expand Down
16 changes: 8 additions & 8 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub(crate) enum FuncInstanceInternal {
impl fmt::Debug for FuncInstance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.as_internal() {
&FuncInstanceInternal::Internal { ref signature, .. } => {
FuncInstanceInternal::Internal { ref signature, .. } => {
// We can't write description of self.module here, because it generate
// debug string for function instances and this will lead to infinite loop.
write!(f, "Internal {{ signature={:?} }}", signature,)
}
&FuncInstanceInternal::Host { ref signature, .. } => {
FuncInstanceInternal::Host { ref signature, .. } => {
write!(f, "Host {{ signature={:?} }}", signature)
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl FuncInstance {
) -> FuncRef {
let func = FuncInstanceInternal::Internal {
signature,
module: module,
module,
body: Rc::new(body),
};
FuncRef(Rc::new(FuncInstance(func)))
Expand Down Expand Up @@ -269,19 +269,19 @@ impl<'args> FuncInvocation<'args> {
/// Whether this invocation is currently resumable.
pub fn is_resumable(&self) -> bool {
match &self.kind {
&FuncInvocationKind::Internal(ref interpreter) => interpreter.state().is_resumable(),
&FuncInvocationKind::Host { .. } => false,
FuncInvocationKind::Internal(ref interpreter) => interpreter.state().is_resumable(),
FuncInvocationKind::Host { .. } => false,
}
}

/// If the invocation is resumable, the expected return value type to be feed back in.
pub fn resumable_value_type(&self) -> Option<ValueType> {
match &self.kind {
&FuncInvocationKind::Internal(ref interpreter) => match interpreter.state() {
&InterpreterState::Resumable(ref value_type) => value_type.clone(),
FuncInvocationKind::Internal(ref interpreter) => match interpreter.state() {
InterpreterState::Resumable(ref value_type) => *value_type,
_ => None,
},
&FuncInvocationKind::Host { .. } => None,
FuncInvocationKind::Host { .. } => None,
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl<'a> RuntimeArgs<'a> {
where
T: FromRuntimeValue,
{
Ok(self
.nth_value_checked(idx)?
self.nth_value_checked(idx)?
.try_into()
.ok_or_else(|| TrapKind::UnexpectedSignature)?)
.ok_or(TrapKind::UnexpectedSignature)
.map_err(Into::into)
}

/// Extract argument as a [`RuntimeValue`] by index `idx`.
Expand Down
26 changes: 8 additions & 18 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,56 +255,46 @@ pub trait ModuleImportResolver {

impl ModuleImportResolver for ModuleRef {
fn resolve_func(&self, field_name: &str, _signature: &Signature) -> Result<FuncRef, Error> {
Ok(self
.export_by_name(field_name)
self.export_by_name(field_name)
.ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
.as_func()
.cloned()
.ok_or_else(|| {
Error::Instantiation(format!("Export {} is not a function", field_name))
})?)
.ok_or_else(|| Error::Instantiation(format!("Export {} is not a function", field_name)))
}

fn resolve_global(
&self,
field_name: &str,
_global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error> {
Ok(self
.export_by_name(field_name)
self.export_by_name(field_name)
.ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
.as_global()
.cloned()
.ok_or_else(|| {
Error::Instantiation(format!("Export {} is not a global", field_name))
})?)
.ok_or_else(|| Error::Instantiation(format!("Export {} is not a global", field_name)))
}

fn resolve_memory(
&self,
field_name: &str,
_memory_type: &MemoryDescriptor,
) -> Result<MemoryRef, Error> {
Ok(self
.export_by_name(field_name)
self.export_by_name(field_name)
.ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
.as_memory()
.cloned()
.ok_or_else(|| {
Error::Instantiation(format!("Export {} is not a memory", field_name))
})?)
.ok_or_else(|| Error::Instantiation(format!("Export {} is not a memory", field_name)))
}

fn resolve_table(
&self,
field_name: &str,
_table_type: &TableDescriptor,
) -> Result<TableRef, Error> {
Ok(self
.export_by_name(field_name)
self.export_by_name(field_name)
.ok_or_else(|| Error::Instantiation(format!("Export {} not found", field_name)))?
.as_table()
.cloned()
.ok_or_else(|| Error::Instantiation(format!("Export {} is not a table", field_name)))?)
.ok_or_else(|| Error::Instantiation(format!("Export {} is not a table", field_name)))
}
}
6 changes: 1 addition & 5 deletions src/isa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,7 @@ impl<'a> Iterator for InstructionIter<'a> {

#[inline]
fn next(&mut self) -> Option<Self::Item> {
let internal = if let Some(i) = self.instructions.get(self.position as usize) {
i
} else {
return None;
};
let internal = self.instructions.get(self.position as usize)?;

let out = match *internal {
InstructionInternal::GetLocal(x) => Instruction::GetLocal(x),
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@

#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::len_without_is_empty)]
#![allow(clippy::new_ret_no_self)]

#[cfg(not(feature = "std"))]
#[macro_use]
Expand Down Expand Up @@ -246,10 +248,7 @@ pub enum TrapKind {
impl TrapKind {
/// Whether this trap is specified by the host.
pub fn is_host(&self) -> bool {
match self {
&TrapKind::Host(_) => true,
_ => false,
}
matches!(self, TrapKind::Host(_))
}
}

Expand Down Expand Up @@ -333,6 +332,7 @@ impl Error {
}
}

#[allow(clippy::from_over_into)]
impl Into<String> for Error {
fn into(self) -> String {
match self {
Expand Down
Loading

0 comments on commit f26c9c5

Please sign in to comment.