From a8d1d78643621e49c5af3fce9df704f16068f5d9 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sat, 25 Feb 2023 12:21:13 +0100 Subject: [PATCH] fix some issues in stream api --- src/lib.rs | 28 +++++++++++++----- src/stream.rs | 82 ++++++++++++++++++++++++++++----------------------- 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 612fc99..c59acf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,20 +60,32 @@ macro_rules! require_int_on_stack { }; } #[macro_export] -macro_rules! require_array_on_stack { - ($name:tt, $stack:expr, $fn:literal) => { - let _binding = $stack.pop(); - let Value::Array(ref $name) = _binding.lock_ro().native else { +macro_rules! require_array { + ($name:tt, $array:expr, $stack:expr, $fn:literal) => { + let Value::Array(ref $name) = $array.lock_ro().native else { return $stack.err(ErrorKind::InvalidCall($fn.to_owned())) }; }; } #[macro_export] -macro_rules! require_mut_array_on_stack { - ($name:tt, $stack:expr, $fn:literal) => { - let _binding = $stack.pop(); - let Value::Array(ref mut $name) = _binding.lock().native else { +macro_rules! require_mut_array { + ($name:tt, $array:expr, $stack:expr, $fn:literal) => { + let Value::Array(ref mut $name) = $array.lock().native else { return $stack.err(ErrorKind::InvalidCall($fn.to_owned())) }; }; } +#[macro_export] +macro_rules! require_array_on_stack { + ($name:tt, $stack:expr, $fn:literal) => { + let binding = $stack.pop(); + require_array!($name, binding, $stack, $fn) + }; +} +#[macro_export] +macro_rules! require_mut_array_on_stack { + ($name:tt, $stack:expr, $fn:literal) => { + let binding = $stack.pop(); + require_mut_array!($name, binding, $stack, $fn) + }; +} diff --git a/src/stream.rs b/src/stream.rs index 173a19d..46c1188 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - fs::{OpenOptions, File}, + fs::{OpenOptions}, io::Read, io::Write, mem, @@ -10,7 +10,7 @@ use std::{ use once_cell::sync::Lazy; -use crate::{mutex::Mut, runtime::*, type_err, require_on_stack, require_int_on_stack, require_array_on_stack, require_mut_array_on_stack}; +use crate::{mutex::Mut, runtime::*, *}; static STREAM_TYPES: Lazy>>> = Lazy::new(|| Arc::new(Mut::new(HashMap::new()))); @@ -187,46 +187,54 @@ pub fn write_all_stream(stack: &mut Stack) -> OError { pub fn read_stream(stack: &mut Stack) -> OError { require_on_stack!(id, Mega, stack, "read-stream"); - require_mut_array_on_stack!(a, stack, "read-stream"); - let stream = runtime(|rt| { - rt.get_stream(id as u128) - .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) - })?; - let mut vec = vec![0; a.len()]; - stack.push( - Value::Mega( - stream - .lock() - .read(&mut vec[..]) - .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))? as i128, - ) - .spl(), - ); - a.clone_from_slice( - &vec.into_iter() - .map(|x| Value::Int(x as i32).spl()) - .collect::>(), - ); + let array = stack.pop(); + { + require_mut_array!(a, array, stack, "read-stream"); + let stream = runtime(|rt| { + rt.get_stream(id as u128) + .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) + })?; + let mut vec = vec![0; a.len()]; + stack.push( + Value::Mega( + stream + .lock() + .read(&mut vec[..]) + .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))? as i128, + ) + .spl(), + ); + a.clone_from_slice( + &vec.into_iter() + .map(|x| Value::Int(x as i32).spl()) + .collect::>(), + ); + } + stack.push(array); Ok(()) } pub fn read_all_stream(stack: &mut Stack) -> OError { require_on_stack!(id, Mega, stack, "read-all-stream"); - require_mut_array_on_stack!(a, stack, "read-all-stream"); - let stream = runtime(|rt| { - rt.get_stream(id as u128) - .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) - })?; - let mut vec = vec![0; a.len()]; - stream - .lock() - .read_exact(&mut vec[..]) - .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))?; - a.clone_from_slice( - &vec.into_iter() - .map(|x| Value::Int(x as i32).spl()) - .collect::>(), - ); + let array = stack.pop(); + { + require_mut_array!(a, array, stack, "read-all-stream"); + let stream = runtime(|rt| { + rt.get_stream(id as u128) + .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) + })?; + let mut vec = vec![0; a.len()]; + stream + .lock() + .read_exact(&mut vec[..]) + .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))?; + a.clone_from_slice( + &vec.into_iter() + .map(|x| Value::Int(x as i32).spl()) + .collect::>(), + ); + } + stack.push(array); Ok(()) }