add flush_output/{0,1}, past_end_of_stream

This commit is contained in:
Mark Thom
2020-05-05 14:10:49 -06:00
parent ab62603c5a
commit 53ae8b9bd1
8 changed files with 110 additions and 116 deletions

View File

@@ -1,6 +1,5 @@
use crate::prolog::machine::machine_indices::*;
use crate::prolog::machine::stack::*;
use crate::prolog::machine::streams::*;
use std::mem;
use std::ops::IndexMut;
@@ -215,24 +214,6 @@ impl<T: CopierTarget> CopyTermState<T> {
}
}
fn copy_stream(&mut self, addr: usize) {
let threshold = self.target.threshold();
let trail_item = mem::replace(
&mut self.target[addr],
HeapCellValue::Addr(Addr::Stream(threshold)),
);
self.trail.push((
Ref::HeapCell(addr),
trail_item,
));
self.target.push(HeapCellValue::Stream(Stream::null_stream()));
self.scan += 1;
}
fn copy_structure(&mut self, addr: usize) {
match self.target[addr].context_free_clone() {
HeapCellValue::NamedStr(arity, name, fixity) => {
@@ -285,11 +266,12 @@ impl<T: CopierTarget> CopyTermState<T> {
*self.value_at_scan() = HeapCellValue::Addr(addr);
}
}
Addr::Lis(h) if h >= self.old_h => {
self.scan += 1;
}
Addr::Lis(h) => {
self.copy_list(h);
if h >= self.old_h {
self.scan += 1;
} else {
self.copy_list(h);
}
}
addr @ Addr::AttrVar(_) |
addr @ Addr::HeapCell(_) |
@@ -303,7 +285,7 @@ impl<T: CopierTarget> CopyTermState<T> {
self.copy_partial_string(addr, n);
}
Addr::Stream(h) => {
self.copy_stream(h);
*self.value_at_scan() = self.target[h].context_free_clone();
}
_ => {
self.scan += 1;

View File

@@ -288,10 +288,10 @@ impl<T: RawBlockTraits> HeapTemplate<T> {
HeapCellValue::Addr(addr) => {
addr
}
val @ HeapCellValue::Atom(..)
| val @ HeapCellValue::Integer(_)
| val @ HeapCellValue::DBRef(_)
| val @ HeapCellValue::Rational(_) => {
val @ HeapCellValue::Atom(..) |
val @ HeapCellValue::Integer(_) |
val @ HeapCellValue::DBRef(_) |
val @ HeapCellValue::Rational(_) => {
Addr::Con(self.push(val))
}
val @ HeapCellValue::NamedStr(..) => {

View File

@@ -443,8 +443,8 @@ impl HeapCellValue {
&HeapCellValue::PartialString(ref pstr, has_tail) => {
HeapCellValue::PartialString(pstr.clone(), has_tail)
}
&HeapCellValue::Stream(_) => {
HeapCellValue::Stream(Stream::null_stream())
&HeapCellValue::Stream(ref stream) => {
HeapCellValue::Stream(stream.clone())
}
&HeapCellValue::TcpListener(_) => {
HeapCellValue::Atom(clause_name!("$socket_server"), None)

View File

@@ -619,6 +619,32 @@ impl MachineState {
stream: Stream,
indices: &mut IndexStore,
) -> CallResult {
let opt_err =
if !stream.is_input_stream() {
Some("stream") // 8.14.2.3 g)
} else if stream.options.stream_type == StreamType::Binary {
Some("binary_stream") // 8.14.2.3 h)
} else {
None
};
if let Some(err_string) = opt_err {
let stub = MachineError::functor_stub(clause_name!("read_term"), 3);
let addr = vec![
HeapCellValue::Stream(stream)
];
let err = MachineError::permission_error(
self.heap.h(),
Permission::InputStream,
err_string,
addr,
);
return Err(self.error_form(err, stub));
}
let mut orig_stream = stream.clone();
let mut stream = self.open_parsing_stream(stream, "read_term", 3)?;

View File

@@ -37,7 +37,6 @@ pub enum StreamInstance {
OutputFile(File),
Null,
ReadlineStream(ReadlineStream),
// Stdin,
Stdout,
TcpStream(TcpStream),
}
@@ -154,6 +153,7 @@ impl Default for StreamOptions {
#[derive(Debug, Clone, Hash)]
pub struct Stream {
past_end_of_stream: bool,
pub options: StreamOptions,
stream_inst: WrappedStreamInstance,
}
@@ -172,45 +172,25 @@ impl From<TcpStream> for Stream {
tcp_stream.set_read_timeout(None).unwrap();
tcp_stream.set_write_timeout(None).unwrap();
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::TcpStream(tcp_stream)
)
}
Stream::from_inst(StreamInstance::TcpStream(tcp_stream))
}
}
impl From<String> for Stream {
fn from(string: String) -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::Bytes(Cursor::new(string.into_bytes()))
)
}
Stream::from_inst(StreamInstance::Bytes(Cursor::new(string.into_bytes())))
}
}
impl From<ReadlineStream> for Stream {
fn from(rl_stream: ReadlineStream) -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::ReadlineStream(rl_stream)
),
}
Stream::from_inst(StreamInstance::ReadlineStream(rl_stream))
}
}
impl From<&'static str> for Stream {
fn from(src: &'static str) -> Stream {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::DynReadSource(Box::new(src.as_bytes()))
),
}
Stream::from_inst(StreamInstance::DynReadSource(Box::new(src.as_bytes())))
}
}
@@ -229,61 +209,31 @@ impl Stream {
ptr as *const u8
}
#[inline]
fn from_inst(stream_inst: StreamInstance) -> Self {
Stream {
past_end_of_stream: false,
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(stream_inst)
}
}
#[inline]
pub(crate)
fn stdout() -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::Stdout
),
}
Stream::from_inst(StreamInstance::Stdout)
}
#[inline]
pub(crate)
fn from_file_as_output(file: File) -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::OutputFile(file)
),
}
Stream::from_inst(StreamInstance::OutputFile(file))
}
#[inline]
pub(crate)
fn from_file_as_input(file: File) -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::InputFile(file)
),
}
}
/*
#[inline]
pub(crate)
fn stdin() -> Self {
Stream {
options: StreamOptions::default(),
stream_inst: WrappedStreamInstance::new(
StreamInstance::Stdin
),
}
}
*/
#[inline]
pub(crate)
fn null_stream() -> Self {
Stream {
options: StreamOptions::default(), // TODO: null_options?
stream_inst: WrappedStreamInstance::new(
StreamInstance::Null
),
}
Stream::from_inst(StreamInstance::InputFile(file))
}
#[inline]
@@ -388,9 +338,10 @@ impl MachineState {
) -> CallResult {
match stream.options.eof_action {
EOFAction::Error => {
let stub = MachineError::functor_stub(caller, arity);
stream.past_end_of_stream = true;
let stream = vec![
let stub = MachineError::functor_stub(caller, arity);
let payload = vec![
HeapCellValue::Stream(stream.clone())
];
@@ -398,7 +349,7 @@ impl MachineState {
self.heap.h(),
Permission::InputStream,
"past_end_of_stream",
stream,
payload,
);
Err(self.error_form(err, stub))
@@ -408,10 +359,12 @@ impl MachineState {
HeapCellValue::Atom(clause_name!("end_of_stream"), None)
);
stream.past_end_of_stream = true;
Ok(self.unify(result, end_of_stream))
}
EOFAction::Reset => {
Ok(self.fail = !stream.reset())
stream.past_end_of_stream = !stream.reset();
Ok(self.fail = stream.past_end_of_stream)
}
}
}

View File

@@ -1603,8 +1603,8 @@ impl MachineState {
let a1 = self[temp_v!(1)];
match result {
Some(Ok(b)) => {
self.unify(Addr::Char(b as char), a1);
Some(Ok(c)) => {
self.unify(Addr::Char(c), a1);
}
Some(Err(_)) => {
let end_of_file = self.heap.to_unifiable(HeapCellValue::Atom(
@@ -1623,6 +1623,29 @@ impl MachineState {
}
}
}
&SystemClauseType::FlushOutput => {
let mut stream =
self.get_stream_or_alias(self[temp_v!(1)], indices, "flush_output", 1)?;
if stream.is_input_stream() {
let stub = MachineError::functor_stub(clause_name!("flush_output"), 1);
let addr = vec![
HeapCellValue::Stream(stream)
];
let err = MachineError::permission_error(
self.heap.h(),
Permission::OutputStream,
"stream",
addr,
);
return Err(self.error_form(err, stub));
}
stream.flush().unwrap();
}
&SystemClauseType::GetSingleChar => {
let c = get_single_char();
@@ -3925,14 +3948,12 @@ impl MachineState {
if let Some(err_string) = opt_err {
let stub = MachineError::functor_stub(clause_name!("write_term"), 3);
let h = self.heap.h();
let addr = self.heap.to_unifiable(
let addr = vec![
HeapCellValue::Stream(stream)
);
];
let err = MachineError::permission_error(
h + 1,
self.heap.h(),
Permission::OutputStream,
err_string,
addr,