bunch of small fixes

This commit is contained in:
vms 2021-04-20 02:36:40 +03:00
parent 748cf3b0f6
commit 93bf5e153d
11 changed files with 85 additions and 55 deletions

4
Cargo.lock generated
View File

@ -28,7 +28,7 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "fluence-it-types"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"it-to-bytes",
"nom",
@ -225,7 +225,7 @@ checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasmer-interface-types-fl"
version = "0.19.0"
version = "0.20.0"
dependencies = [
"fluence-it-types",
"it-to-bytes",

View File

@ -39,8 +39,6 @@ where
.view();
let writer = MemoryWriter::new(memory_view, offset);
let values_count = array_values.len();
// here it's known that all interface values have the same type
for value in array_values {
match value {
@ -59,29 +57,38 @@ where
IValue::F64(value) => writer.write_array(value.to_le_bytes()),
IValue::String(value) => {
let string_pointer =
write_to_instance_mem(instance, instruction.clone(), value.as_bytes())?;
write_to_instance_mem(instance, instruction.clone(), value.as_bytes())? as u32;
let string_size = value.len() as u32;
writer.write_array(string_pointer.to_le_bytes());
writer.write_array(value.len().to_le_bytes());
writer.write_array(string_size.to_le_bytes());
}
IValue::ByteArray(values) => {
let array_pointer =
write_to_instance_mem(instance, instruction.clone(), &values)? as u32;
let array_size = values.len() as u32;
writer.write_array(array_pointer.to_le_bytes());
writer.write_array(array_size.to_le_bytes());
}
IValue::ByteArray(values) => writer.write_slice(&values),
IValue::Array(values) => {
let (array_offset, array_size) =
array_lower_memory_impl(instance, instruction.clone(), values)?;
let (array_offset, array_size) = (array_offset as u32, array_size as u32);
writer.write_array(array_offset.to_le_bytes());
writer.write_array(array_size.to_le_bytes());
}
IValue::Record(values) => {
let record_offset =
super::record_lower_memory_impl(instance, instruction.clone(), values)?;
super::record_lower_memory_impl(instance, instruction.clone(), values)? as u32;
writer.write_array(record_offset.to_le_bytes());
}
}
}
Ok((offset as _, values_count as _))
Ok((offset as _, writer.written_values() as _))
}
fn value_size(value: &IValue) -> usize {
@ -97,9 +104,7 @@ fn value_size(value: &IValue) -> usize {
IValue::U64(_) => 8,
IValue::F32(_) => 4,
IValue::F64(_) => 8,
IValue::String(_) => 4,
IValue::ByteArray(_) => 4,
IValue::Array(_) => 4,
IValue::String(_) | IValue::ByteArray(_) | IValue::Array(_) => 2 * 4,
IValue::I32(_) => 4,
IValue::I64(_) => 8,
IValue::Record(_) => 4,

View File

@ -3,15 +3,18 @@ use std::cell::Cell;
pub(super) struct MemoryWriter<'m> {
memory_view: &'m [Cell<u8>],
offset: Cell<usize>,
written_values: Cell<usize>,
}
impl<'m> MemoryWriter<'m> {
pub(crate) fn new(memory_view: &'m [Cell<u8>], offset: usize) -> Self {
let offset = Cell::new(offset);
let written_values = Cell::new(0);
Self {
memory_view,
offset,
written_values,
}
}
@ -19,8 +22,10 @@ impl<'m> MemoryWriter<'m> {
let offset = self.offset.get();
self.memory_view[offset].set(value);
self.offset.set(offset + 1);
self.update_counter();
}
#[allow(dead_code)]
pub(crate) fn write_slice(&self, values: &[u8]) {
let offset = self.offset.get();
@ -31,6 +36,7 @@ impl<'m> MemoryWriter<'m> {
}
self.offset.set(offset + values.len());
self.update_counter();
}
pub(crate) fn write_array<const N: usize>(&self, values: [u8; N]) {
@ -43,5 +49,15 @@ impl<'m> MemoryWriter<'m> {
}
self.offset.set(offset + values.len());
self.update_counter();
}
fn update_counter(&self) {
let written_values_count = self.written_values.get();
self.written_values.set(written_values_count + 1);
}
pub(crate) fn written_values(&self) -> usize {
self.written_values.get()
}
}

View File

@ -223,14 +223,14 @@ def_read_func!(read_u64_array, (u64, elements_count), {
let mut result = Vec::with_capacity(elements_count);
for element_id in 0..elements_count {
let value = u64::from_le_bytes([
Cell::get(&memory_view[4 * element_id]),
Cell::get(&memory_view[4 * element_id + 1]),
Cell::get(&memory_view[4 * element_id + 2]),
Cell::get(&memory_view[4 * element_id + 3]),
Cell::get(&memory_view[4 * element_id + 4]),
Cell::get(&memory_view[4 * element_id + 5]),
Cell::get(&memory_view[4 * element_id + 6]),
Cell::get(&memory_view[4 * element_id + 7]),
Cell::get(&memory_view[8 * element_id]),
Cell::get(&memory_view[8 * element_id + 1]),
Cell::get(&memory_view[8 * element_id + 2]),
Cell::get(&memory_view[8 * element_id + 3]),
Cell::get(&memory_view[8 * element_id + 4]),
Cell::get(&memory_view[8 * element_id + 5]),
Cell::get(&memory_view[8 * element_id + 6]),
Cell::get(&memory_view[8 * element_id + 7]),
]);
result.push(IValue::U64(value));
}
@ -244,14 +244,14 @@ def_read_func!(read_f64_array, (f64, elements_count), {
let mut result = Vec::with_capacity(elements_count);
for element_id in 0..elements_count {
let value = f64::from_le_bytes([
Cell::get(&memory_view[4 * element_id]),
Cell::get(&memory_view[4 * element_id + 1]),
Cell::get(&memory_view[4 * element_id + 2]),
Cell::get(&memory_view[4 * element_id + 3]),
Cell::get(&memory_view[4 * element_id + 4]),
Cell::get(&memory_view[4 * element_id + 5]),
Cell::get(&memory_view[4 * element_id + 6]),
Cell::get(&memory_view[4 * element_id + 7]),
Cell::get(&memory_view[8 * element_id]),
Cell::get(&memory_view[8 * element_id + 1]),
Cell::get(&memory_view[8 * element_id + 2]),
Cell::get(&memory_view[8 * element_id + 3]),
Cell::get(&memory_view[8 * element_id + 4]),
Cell::get(&memory_view[8 * element_id + 5]),
Cell::get(&memory_view[8 * element_id + 6]),
Cell::get(&memory_view[8 * element_id + 7]),
]);
result.push(IValue::F64(value));
}
@ -265,14 +265,14 @@ def_read_func!(read_s64_array, (i64, elements_count), {
let mut result = Vec::with_capacity(elements_count);
for element_id in 0..elements_count {
let value = i64::from_le_bytes([
Cell::get(&memory_view[4 * element_id]),
Cell::get(&memory_view[4 * element_id + 1]),
Cell::get(&memory_view[4 * element_id + 2]),
Cell::get(&memory_view[4 * element_id + 3]),
Cell::get(&memory_view[4 * element_id + 4]),
Cell::get(&memory_view[4 * element_id + 5]),
Cell::get(&memory_view[4 * element_id + 6]),
Cell::get(&memory_view[4 * element_id + 7]),
Cell::get(&memory_view[8 * element_id]),
Cell::get(&memory_view[8 * element_id + 1]),
Cell::get(&memory_view[8 * element_id + 2]),
Cell::get(&memory_view[8 * element_id + 3]),
Cell::get(&memory_view[8 * element_id + 4]),
Cell::get(&memory_view[8 * element_id + 5]),
Cell::get(&memory_view[8 * element_id + 6]),
Cell::get(&memory_view[8 * element_id + 7]),
]);
result.push(IValue::S64(value));
}
@ -286,14 +286,14 @@ def_read_func!(read_i64_array, (i64, elements_count), {
let mut result = Vec::with_capacity(elements_count);
for element_id in 0..elements_count {
let value = i64::from_le_bytes([
Cell::get(&memory_view[4 * element_id]),
Cell::get(&memory_view[4 * element_id + 1]),
Cell::get(&memory_view[4 * element_id + 2]),
Cell::get(&memory_view[4 * element_id + 3]),
Cell::get(&memory_view[4 * element_id + 4]),
Cell::get(&memory_view[4 * element_id + 5]),
Cell::get(&memory_view[4 * element_id + 6]),
Cell::get(&memory_view[4 * element_id + 7]),
Cell::get(&memory_view[8 * element_id]),
Cell::get(&memory_view[8 * element_id + 1]),
Cell::get(&memory_view[8 * element_id + 2]),
Cell::get(&memory_view[8 * element_id + 3]),
Cell::get(&memory_view[8 * element_id + 4]),
Cell::get(&memory_view[8 * element_id + 5]),
Cell::get(&memory_view[8 * element_id + 6]),
Cell::get(&memory_view[8 * element_id + 7]),
]);
result.push(IValue::I64(value));
}

View File

@ -17,6 +17,8 @@ use crate::IType;
use crate::IValue;
use crate::NEVec;
pub use records::record_size;
pub(crate) use argument_get::argument_get;
pub(crate) use arrays::*;
pub(crate) use call_core::call_core;

View File

@ -2,6 +2,8 @@ mod lift_record;
mod lower_record;
mod value_reader;
pub use lift_record::record_size;
pub(crate) use lift_record::record_lift_memory_impl;
pub(crate) use lower_record::record_lower_memory_impl;

View File

@ -58,8 +58,8 @@ where
))
}
/// Returns record size in bytes.
fn record_size(record_type: &IRecordType) -> usize {
/// Returns the record size in bytes.
pub fn record_size(record_type: &IRecordType) -> usize {
let mut record_size = 0;
for field_type in record_type.fields.iter() {

View File

@ -36,28 +36,30 @@ where
IValue::F64(value) => result.extend_from_slice(&value.to_le_bytes()),
IValue::String(value) => {
let string_pointer =
write_to_instance_mem(instance, instruction.clone(), value.as_bytes())?;
write_to_instance_mem(instance, instruction.clone(), value.as_bytes())? as u32;
result.extend_from_slice(&string_pointer.to_le_bytes());
result.extend_from_slice(&value.len().to_le_bytes());
result.extend_from_slice(&(value.len() as u32).to_le_bytes());
}
IValue::ByteArray(value) => {
let array_pointer = write_to_instance_mem(instance, instruction.clone(), &value)?;
let array_pointer =
write_to_instance_mem(instance, instruction.clone(), &value)? as u32;
result.extend_from_slice(&array_pointer.to_le_bytes());
result.extend_from_slice(&value.len().to_le_bytes());
result.extend_from_slice(&(value.len() as u32).to_le_bytes());
}
IValue::Array(values) => {
let (offset, size) =
super::array_lower_memory_impl(instance, instruction.clone(), values)?;
result.extend_from_slice(&offset.to_le_bytes());
result.extend_from_slice(&size.to_le_bytes());
result.extend_from_slice(&(offset as u32).to_le_bytes());
result.extend_from_slice(&(size as u32).to_le_bytes());
}
IValue::Record(values) => {
let record_ptr = record_lower_memory_impl(instance, instruction.clone(), values)?;
let record_ptr =
record_lower_memory_impl(instance, instruction.clone(), values)? as u32;
result.extend_from_slice(&record_ptr.to_le_bytes());
}

View File

@ -69,7 +69,6 @@ macro_rules! read_ty {
};
}
// TODO: rewrite this with macros
impl ValueReader {
pub(super) fn new(stream: Vec<u8>) -> Self {
let offset = Cell::new(0);

View File

@ -4,9 +4,11 @@ mod instructions;
pub mod stack;
pub mod wasm;
pub use instructions::record_size;
pub use instructions::Instruction;
use crate::errors::{InstructionResult, InterpreterResult};
use crate::IValue;
pub use instructions::Instruction;
use stack::Stack;
use std::{convert::TryFrom, marker::PhantomData};

View File

@ -73,6 +73,8 @@ pub use fluence_it_types::IValue;
pub use it_to_bytes::ToBytes;
pub use crate::interpreter::record_size;
#[cfg(feature = "serde")]
pub use crate::serde::de::from_interface_values;