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,7 +187,9 @@ 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();
{
require_mut_array!(a, array, stack, "read-stream");
let stream = runtime(|rt| { let stream = runtime(|rt| {
rt.get_stream(id as u128) rt.get_stream(id as u128)
.ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}"))))
@ -207,12 +209,16 @@ pub fn read_stream(stack: &mut Stack) -> OError {
.map(|x| Value::Int(x as i32).spl()) .map(|x| Value::Int(x as i32).spl())
.collect::<Vec<_>>(), .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();
{
require_mut_array!(a, array, stack, "read-all-stream");
let stream = runtime(|rt| { let stream = runtime(|rt| {
rt.get_stream(id as u128) rt.get_stream(id as u128)
.ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}")))) .ok_or_else(|| stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}"))))
@ -227,6 +233,8 @@ pub fn read_all_stream(stack: &mut Stack) -> OError {
.map(|x| Value::Int(x as i32).spl()) .map(|x| Value::Int(x as i32).spl())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
); );
}
stack.push(array);
Ok(()) Ok(())
} }