fix some issues in stream api

This commit is contained in:
Daniella 2023-02-25 12:21:13 +01:00
parent f30c212c85
commit a8d1d78643
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
2 changed files with 65 additions and 45 deletions

View file

@ -60,20 +60,32 @@ macro_rules! require_int_on_stack {
}; };
} }
#[macro_export] #[macro_export]
macro_rules! require_array_on_stack { macro_rules! require_array {
($name:tt, $stack:expr, $fn:literal) => { ($name:tt, $array:expr, $stack:expr, $fn:literal) => {
let _binding = $stack.pop(); let Value::Array(ref $name) = $array.lock_ro().native else {
let Value::Array(ref $name) = _binding.lock_ro().native else {
return $stack.err(ErrorKind::InvalidCall($fn.to_owned())) return $stack.err(ErrorKind::InvalidCall($fn.to_owned()))
}; };
}; };
} }
#[macro_export] #[macro_export]
macro_rules! require_mut_array_on_stack { macro_rules! require_mut_array {
($name:tt, $stack:expr, $fn:literal) => { ($name:tt, $array:expr, $stack:expr, $fn:literal) => {
let _binding = $stack.pop(); let Value::Array(ref mut $name) = $array.lock().native else {
let Value::Array(ref mut $name) = _binding.lock().native else {
return $stack.err(ErrorKind::InvalidCall($fn.to_owned())) 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)
};
}

View file

@ -1,6 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{OpenOptions, File}, fs::{OpenOptions},
io::Read, io::Read,
io::Write, io::Write,
mem, mem,
@ -10,7 +10,7 @@ use std::{
use once_cell::sync::Lazy; 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<Arc<Mut<HashMap<String, StreamType>>>> = static STREAM_TYPES: Lazy<Arc<Mut<HashMap<String, StreamType>>>> =
Lazy::new(|| Arc::new(Mut::new(HashMap::new()))); 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 { pub fn read_stream(stack: &mut Stack) -> OError {
require_on_stack!(id, Mega, stack, "read-stream"); require_on_stack!(id, Mega, stack, "read-stream");
require_mut_array_on_stack!(a, stack, "read-stream"); let array = stack.pop();
let stream = runtime(|rt| { {
rt.get_stream(id as u128) require_mut_array!(a, array, stack, "read-stream");
.ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) let stream = runtime(|rt| {
})?; rt.get_stream(id as u128)
let mut vec = vec![0; a.len()]; .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}"))))
stack.push( })?;
Value::Mega( let mut vec = vec![0; a.len()];
stream stack.push(
.lock() Value::Mega(
.read(&mut vec[..]) stream
.map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))? as i128, .lock()
) .read(&mut vec[..])
.spl(), .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))? as i128,
); )
a.clone_from_slice( .spl(),
&vec.into_iter() );
.map(|x| Value::Int(x as i32).spl()) a.clone_from_slice(
.collect::<Vec<_>>(), &vec.into_iter()
); .map(|x| Value::Int(x as i32).spl())
.collect::<Vec<_>>(),
);
}
stack.push(array);
Ok(()) Ok(())
} }
pub fn read_all_stream(stack: &mut Stack) -> OError { pub fn read_all_stream(stack: &mut Stack) -> OError {
require_on_stack!(id, Mega, stack, "read-all-stream"); require_on_stack!(id, Mega, stack, "read-all-stream");
require_mut_array_on_stack!(a, stack, "read-all-stream"); let array = stack.pop();
let stream = runtime(|rt| { {
rt.get_stream(id as u128) require_mut_array!(a, array, stack, "read-all-stream");
.ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) let stream = runtime(|rt| {
})?; rt.get_stream(id as u128)
let mut vec = vec![0; a.len()]; .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}"))))
stream })?;
.lock() let mut vec = vec![0; a.len()];
.read_exact(&mut vec[..]) stream
.map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))?; .lock()
a.clone_from_slice( .read_exact(&mut vec[..])
&vec.into_iter() .map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))?;
.map(|x| Value::Int(x as i32).spl()) a.clone_from_slice(
.collect::<Vec<_>>(), &vec.into_iter()
); .map(|x| Value::Int(x as i32).spl())
.collect::<Vec<_>>(),
);
}
stack.push(array);
Ok(()) Ok(())
} }