wrap stream instances in one Rc<RefCell<..>>, use Ref/RefMut to access stream options
This commit is contained in:
@@ -1363,7 +1363,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_stream(&mut self, iter: &mut HCPreOrderIterator, stream: &Stream, max_depth: usize) {
|
fn print_stream(&mut self, iter: &mut HCPreOrderIterator, stream: &Stream, max_depth: usize) {
|
||||||
if let Some(alias) = &stream.options.alias {
|
if let Some(alias) = &stream.options().alias {
|
||||||
self.print_atom(alias);
|
self.print_atom(alias);
|
||||||
} else {
|
} else {
|
||||||
if self.format_struct(iter, max_depth, 1, clause_name!("$stream")) {
|
if self.format_struct(iter, max_depth, 1, clause_name!("$stream")) {
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -402,7 +402,7 @@ impl MachineState {
|
|||||||
3,
|
3,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if orig_stream.options.eof_action == EOFAction::Reset {
|
if orig_stream.options().eof_action == EOFAction::Reset {
|
||||||
if self.fail == false {
|
if self.fail == false {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -387,7 +387,7 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn configure_streams(&mut self) {
|
pub fn configure_streams(&mut self) {
|
||||||
self.user_input.options.alias = Some(clause_name!("user_input"));
|
self.user_input.options_mut().alias = Some(clause_name!("user_input"));
|
||||||
|
|
||||||
self.indices
|
self.indices
|
||||||
.stream_aliases
|
.stream_aliases
|
||||||
@@ -395,7 +395,7 @@ impl Machine {
|
|||||||
|
|
||||||
self.indices.streams.insert(self.user_input.clone());
|
self.indices.streams.insert(self.user_input.clone());
|
||||||
|
|
||||||
self.user_output.options.alias = Some(clause_name!("user_output"));
|
self.user_output.options_mut().alias = Some(clause_name!("user_output"));
|
||||||
|
|
||||||
self.indices
|
self.indices
|
||||||
.stream_aliases
|
.stream_aliases
|
||||||
|
|||||||
@@ -197,13 +197,25 @@ impl fmt::Debug for StreamInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct InnerStream {
|
||||||
|
options: StreamOptions,
|
||||||
|
stream_inst: StreamInstance,
|
||||||
|
past_end_of_stream: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct WrappedStreamInstance(Rc<RefCell<(bool, StreamInstance)>>);
|
struct WrappedStreamInstance(Rc<RefCell<InnerStream>>);
|
||||||
|
|
||||||
impl WrappedStreamInstance {
|
impl WrappedStreamInstance {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn new(stream_inst: StreamInstance) -> Self {
|
fn new(stream_inst: StreamInstance, past_end_of_stream: bool) -> Self {
|
||||||
WrappedStreamInstance(Rc::new(RefCell::new((false, stream_inst))))
|
WrappedStreamInstance(Rc::new(RefCell::new(
|
||||||
|
InnerStream {
|
||||||
|
options: StreamOptions::default(),
|
||||||
|
stream_inst,
|
||||||
|
past_end_of_stream }
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +305,6 @@ impl Default for StreamOptions {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Hash)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
pub struct Stream {
|
pub struct Stream {
|
||||||
pub options: StreamOptions,
|
|
||||||
stream_inst: WrappedStreamInstance,
|
stream_inst: WrappedStreamInstance,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,9 +363,25 @@ impl Stream {
|
|||||||
ptr as *const u8
|
ptr as *const u8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn options(&self) -> std::cell::Ref<'_, StreamOptions> {
|
||||||
|
std::cell::Ref::map(
|
||||||
|
self.stream_inst.0.borrow(),
|
||||||
|
|inner_stream| &inner_stream.options,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn options_mut(&mut self) -> std::cell::RefMut<'_, StreamOptions> {
|
||||||
|
std::cell::RefMut::map(
|
||||||
|
self.stream_inst.0.borrow_mut(),
|
||||||
|
|inner_stream| &mut inner_stream.options,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn position(&mut self) -> Option<u64> {
|
pub(crate) fn position(&mut self) -> Option<u64> {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::InputFile(_, ref mut file) => file.seek(SeekFrom::Current(0)).ok(),
|
StreamInstance::InputFile(_, ref mut file) => file.seek(SeekFrom::Current(0)).ok(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@@ -363,7 +390,9 @@ impl Stream {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn set_position(&mut self, position: u64) {
|
pub(crate) fn set_position(&mut self, position: u64) {
|
||||||
match self.stream_inst.0.borrow_mut().deref_mut() {
|
match self.stream_inst.0.borrow_mut().deref_mut() {
|
||||||
(past_end_of_stream, StreamInstance::InputFile(_, ref mut file)) => {
|
InnerStream { past_end_of_stream,
|
||||||
|
stream_inst: StreamInstance::InputFile(_, ref mut file),
|
||||||
|
.. } => {
|
||||||
file.seek(SeekFrom::Start(position)).unwrap();
|
file.seek(SeekFrom::Start(position)).unwrap();
|
||||||
|
|
||||||
if let Ok(metadata) = file.metadata() {
|
if let Ok(metadata) = file.metadata() {
|
||||||
@@ -376,7 +405,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn past_end_of_stream(&self) -> bool {
|
pub(crate) fn past_end_of_stream(&self) -> bool {
|
||||||
self.stream_inst.0.borrow_mut().0
|
self.stream_inst.0.borrow_mut().past_end_of_stream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -386,7 +415,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn set_past_end_of_stream(&mut self) {
|
pub(crate) fn set_past_end_of_stream(&mut self) {
|
||||||
self.stream_inst.0.borrow_mut().0 = true;
|
self.stream_inst.0.borrow_mut().past_end_of_stream = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -396,7 +425,9 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match self.stream_inst.0.borrow_mut().deref_mut() {
|
match self.stream_inst.0.borrow_mut().deref_mut() {
|
||||||
(past_end_of_stream, StreamInstance::InputFile(_, ref mut file)) => {
|
InnerStream { past_end_of_stream,
|
||||||
|
stream_inst: StreamInstance::InputFile(_, ref mut file),
|
||||||
|
.. } => {
|
||||||
match file.metadata() {
|
match file.metadata() {
|
||||||
Ok(metadata) => {
|
Ok(metadata) => {
|
||||||
if let Ok(position) = file.seek(SeekFrom::Current(0)) {
|
if let Ok(position) = file.seek(SeekFrom::Current(0)) {
|
||||||
@@ -404,17 +435,17 @@ impl Stream {
|
|||||||
Ordering::Equal => AtEndOfStream::At,
|
Ordering::Equal => AtEndOfStream::At,
|
||||||
Ordering::Less => AtEndOfStream::Not,
|
Ordering::Less => AtEndOfStream::Not,
|
||||||
Ordering::Greater => {
|
Ordering::Greater => {
|
||||||
*past_end_of_stream = true; //self.set_past_end_of_stream();
|
*past_end_of_stream = true;
|
||||||
AtEndOfStream::Past
|
AtEndOfStream::Past
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
*past_end_of_stream = true; //self.set_past_end_of_stream();
|
*past_end_of_stream = true;
|
||||||
AtEndOfStream::Past
|
AtEndOfStream::Past
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
*past_end_of_stream = true; //self.set_past_end_of_stream();
|
*past_end_of_stream = true;
|
||||||
AtEndOfStream::Past
|
AtEndOfStream::Past
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -425,7 +456,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn file_name(&self) -> Option<ClauseName> {
|
pub(crate) fn file_name(&self) -> Option<ClauseName> {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::InputFile(ref name, _) => Some(name.clone()),
|
StreamInstance::InputFile(ref name, _) => Some(name.clone()),
|
||||||
StreamInstance::OutputFile(ref name, ..) => Some(name.clone()),
|
StreamInstance::OutputFile(ref name, ..) => Some(name.clone()),
|
||||||
StreamInstance::TcpStream(ref name, _) => Some(name.clone()),
|
StreamInstance::TcpStream(ref name, _) => Some(name.clone()),
|
||||||
@@ -435,12 +466,12 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn mode(&self) -> &'static str {
|
pub(crate) fn mode(&self) -> &'static str {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::Bytes(_)
|
StreamInstance::Bytes(_) |
|
||||||
| StreamInstance::PausedPrologStream(..)
|
StreamInstance::PausedPrologStream(..) |
|
||||||
| StreamInstance::ReadlineStream(_)
|
StreamInstance::ReadlineStream(_) |
|
||||||
| StreamInstance::StaticStr(_)
|
StreamInstance::StaticStr(_) |
|
||||||
| StreamInstance::InputFile(..) => "read",
|
StreamInstance::InputFile(..) => "read",
|
||||||
StreamInstance::TcpStream(..) | StreamInstance::TlsStream(..) => "read_append",
|
StreamInstance::TcpStream(..) | StreamInstance::TlsStream(..) => "read_append",
|
||||||
StreamInstance::OutputFile(_, _, true) => "append",
|
StreamInstance::OutputFile(_, _, true) => "append",
|
||||||
StreamInstance::Stdout | StreamInstance::OutputFile(_, _, false) => "write",
|
StreamInstance::Stdout | StreamInstance::OutputFile(_, _, false) => "write",
|
||||||
@@ -451,8 +482,7 @@ impl Stream {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn from_inst(stream_inst: StreamInstance) -> Self {
|
fn from_inst(stream_inst: StreamInstance) -> Self {
|
||||||
Stream {
|
Stream {
|
||||||
options: StreamOptions::default(),
|
stream_inst: WrappedStreamInstance::new(stream_inst, false),
|
||||||
stream_inst: WrappedStreamInstance::new(stream_inst),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,7 +516,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_stdout(&self) -> bool {
|
pub(crate) fn is_stdout(&self) -> bool {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::Stdout => true,
|
StreamInstance::Stdout => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@@ -494,7 +524,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_stdin(&self) -> bool {
|
pub(crate) fn is_stdin(&self) -> bool {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::ReadlineStream(_) => true,
|
StreamInstance::ReadlineStream(_) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@@ -502,12 +532,12 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn close(&mut self) {
|
pub(crate) fn close(&mut self) {
|
||||||
self.stream_inst.0.borrow_mut().1 = StreamInstance::Null;
|
self.stream_inst.0.borrow_mut().stream_inst = StreamInstance::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_null_stream(&self) -> bool {
|
pub(crate) fn is_null_stream(&self) -> bool {
|
||||||
if let StreamInstance::Null = self.stream_inst.0.borrow().1 {
|
if let StreamInstance::Null = self.stream_inst.0.borrow().stream_inst {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
@@ -516,32 +546,32 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_input_stream(&self) -> bool {
|
pub(crate) fn is_input_stream(&self) -> bool {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::TcpStream(..)
|
StreamInstance::TcpStream(..) |
|
||||||
| StreamInstance::TlsStream(..)
|
StreamInstance::TlsStream(..) |
|
||||||
| StreamInstance::Bytes(_)
|
StreamInstance::Bytes(_) |
|
||||||
| StreamInstance::PausedPrologStream(..)
|
StreamInstance::PausedPrologStream(..) |
|
||||||
| StreamInstance::ReadlineStream(_)
|
StreamInstance::ReadlineStream(_) |
|
||||||
| StreamInstance::StaticStr(_)
|
StreamInstance::StaticStr(_) |
|
||||||
| StreamInstance::InputFile(..) => true,
|
StreamInstance::InputFile(..) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_output_stream(&self) -> bool {
|
pub(crate) fn is_output_stream(&self) -> bool {
|
||||||
match self.stream_inst.0.borrow().1 {
|
match self.stream_inst.0.borrow().stream_inst {
|
||||||
StreamInstance::Stdout
|
StreamInstance::Stdout |
|
||||||
| StreamInstance::TcpStream(..)
|
StreamInstance::TcpStream(..) |
|
||||||
| StreamInstance::TlsStream(..)
|
StreamInstance::TlsStream(..) |
|
||||||
| StreamInstance::Bytes(_)
|
StreamInstance::Bytes(_) |
|
||||||
| StreamInstance::OutputFile(..) => true,
|
StreamInstance::OutputFile(..) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unpause_stream(&mut self) {
|
fn unpause_stream(&mut self) {
|
||||||
let stream_inst = match self.stream_inst.0.borrow_mut().1 {
|
let stream_inst = match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::PausedPrologStream(ref put_back, ref mut stream_inst)
|
StreamInstance::PausedPrologStream(ref put_back, ref mut stream_inst)
|
||||||
if put_back.is_empty() =>
|
if put_back.is_empty() =>
|
||||||
{
|
{
|
||||||
@@ -552,16 +582,16 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.stream_inst.0.borrow_mut().1 = stream_inst;
|
self.stream_inst.0.borrow_mut().stream_inst = stream_inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true on success.
|
// returns true on success.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn reset(&mut self) -> bool {
|
pub(super) fn reset(&mut self) -> bool {
|
||||||
self.stream_inst.0.borrow_mut().0 = false;
|
self.stream_inst.0.borrow_mut().past_end_of_stream = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::Bytes(ref mut cursor) => {
|
StreamInstance::Bytes(ref mut cursor) => {
|
||||||
cursor.set_position(0);
|
cursor.set_position(0);
|
||||||
return true;
|
return true;
|
||||||
@@ -587,7 +617,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn peek_byte(&mut self) -> std::io::Result<u8> {
|
pub(crate) fn peek_byte(&mut self) -> std::io::Result<u8> {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::Bytes(ref mut cursor) => {
|
StreamInstance::Bytes(ref mut cursor) => {
|
||||||
let mut b = [0u8; 1];
|
let mut b = [0u8; 1];
|
||||||
let pos = cursor.position();
|
let pos = cursor.position();
|
||||||
@@ -631,7 +661,7 @@ impl Stream {
|
|||||||
pub(crate) fn peek_char(&mut self) -> std::io::Result<char> {
|
pub(crate) fn peek_char(&mut self) -> std::io::Result<char> {
|
||||||
use unicode_reader::CodePoints;
|
use unicode_reader::CodePoints;
|
||||||
|
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::InputFile(_, ref mut file) => {
|
StreamInstance::InputFile(_, ref mut file) => {
|
||||||
let c = {
|
let c = {
|
||||||
let mut iter = CodePoints::from(&*file);
|
let mut iter = CodePoints::from(&*file);
|
||||||
@@ -679,7 +709,7 @@ impl Stream {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn pause_stream(&mut self, buf: Vec<io::Result<char>>) -> io::Result<()> {
|
pub(crate) fn pause_stream(&mut self, buf: Vec<io::Result<char>>) -> io::Result<()> {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::PausedPrologStream(ref mut inner_buf, _) => {
|
StreamInstance::PausedPrologStream(ref mut inner_buf, _) => {
|
||||||
inner_buf.extend(parser_top_to_bytes(buf)?.into_iter());
|
inner_buf.extend(parser_top_to_bytes(buf)?.into_iter());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -688,10 +718,12 @@ impl Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !buf.is_empty() {
|
if !buf.is_empty() {
|
||||||
let stream_inst =
|
let stream_inst = mem::replace(
|
||||||
mem::replace(&mut self.stream_inst.0.borrow_mut().1, StreamInstance::Null);
|
&mut self.stream_inst.0.borrow_mut().stream_inst,
|
||||||
|
StreamInstance::Null,
|
||||||
|
);
|
||||||
|
|
||||||
self.stream_inst.0.borrow_mut().1 = StreamInstance::PausedPrologStream(
|
self.stream_inst.0.borrow_mut().stream_inst = StreamInstance::PausedPrologStream(
|
||||||
parser_top_to_bytes(buf)?,
|
parser_top_to_bytes(buf)?,
|
||||||
Box::new(stream_inst),
|
Box::new(stream_inst),
|
||||||
);
|
);
|
||||||
@@ -710,13 +742,15 @@ impl MachineState {
|
|||||||
caller: ClauseName,
|
caller: ClauseName,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
) -> CallResult {
|
) -> CallResult {
|
||||||
match stream.options.eof_action {
|
let eof_action = stream.options().eof_action;
|
||||||
|
|
||||||
|
match eof_action {
|
||||||
EOFAction::Error => {
|
EOFAction::Error => {
|
||||||
stream.set_past_end_of_stream();
|
stream.set_past_end_of_stream();
|
||||||
return Err(self.open_past_eos_error(stream.clone(), caller, arity));
|
return Err(self.open_past_eos_error(stream.clone(), caller, arity));
|
||||||
}
|
}
|
||||||
EOFAction::EOFCode => {
|
EOFAction::EOFCode => {
|
||||||
let end_of_stream = if stream.options.stream_type == StreamType::Binary {
|
let end_of_stream = if stream.options().stream_type == StreamType::Binary {
|
||||||
Addr::Fixnum(-1)
|
Addr::Fixnum(-1)
|
||||||
} else {
|
} else {
|
||||||
self.heap
|
self.heap
|
||||||
@@ -979,7 +1013,7 @@ impl MachineState {
|
|||||||
Some("stream") // 8.14.2.3 g)
|
Some("stream") // 8.14.2.3 g)
|
||||||
} else if input.is_none() && !stream.is_output_stream() {
|
} else if input.is_none() && !stream.is_output_stream() {
|
||||||
Some("stream") // 8.14.2.3 g)
|
Some("stream") // 8.14.2.3 g)
|
||||||
} else if stream.options.stream_type != expected_type {
|
} else if stream.options().stream_type != expected_type {
|
||||||
Some(expected_type.other().as_str()) // 8.14.2.3 h)
|
Some(expected_type.other().as_str()) // 8.14.2.3 h)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -1014,7 +1048,7 @@ impl MachineState {
|
|||||||
impl Read for Stream {
|
impl Read for Stream {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||||
let bytes_read = self.stream_inst.0.borrow_mut().1.read(buf)?;
|
let bytes_read = self.stream_inst.0.borrow_mut().stream_inst.read(buf)?;
|
||||||
self.unpause_stream();
|
self.unpause_stream();
|
||||||
Ok(bytes_read)
|
Ok(bytes_read)
|
||||||
}
|
}
|
||||||
@@ -1022,17 +1056,17 @@ impl Read for Stream {
|
|||||||
|
|
||||||
impl Write for Stream {
|
impl Write for Stream {
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::OutputFile(_, ref mut file, _) => file.write(buf),
|
StreamInstance::OutputFile(_, ref mut file, _) => file.write(buf),
|
||||||
StreamInstance::TcpStream(_, ref mut tcp_stream) => tcp_stream.write(buf),
|
StreamInstance::TcpStream(_, ref mut tcp_stream) => tcp_stream.write(buf),
|
||||||
StreamInstance::TlsStream(_, ref mut tls_stream) => tls_stream.write(buf),
|
StreamInstance::TlsStream(_, ref mut tls_stream) => tls_stream.write(buf),
|
||||||
StreamInstance::Bytes(ref mut cursor) => cursor.write(buf),
|
StreamInstance::Bytes(ref mut cursor) => cursor.write(buf),
|
||||||
StreamInstance::Stdout => stdout().write(buf),
|
StreamInstance::Stdout => stdout().write(buf),
|
||||||
StreamInstance::PausedPrologStream(..)
|
StreamInstance::PausedPrologStream(..) |
|
||||||
| StreamInstance::StaticStr(_)
|
StreamInstance::StaticStr(_) |
|
||||||
| StreamInstance::ReadlineStream(_)
|
StreamInstance::ReadlineStream(_) |
|
||||||
| StreamInstance::InputFile(..)
|
StreamInstance::InputFile(..) |
|
||||||
| StreamInstance::Null => Err(std::io::Error::new(
|
StreamInstance::Null => Err(std::io::Error::new(
|
||||||
ErrorKind::PermissionDenied,
|
ErrorKind::PermissionDenied,
|
||||||
StreamError::WriteToInputStream,
|
StreamError::WriteToInputStream,
|
||||||
)),
|
)),
|
||||||
@@ -1040,17 +1074,17 @@ impl Write for Stream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> std::io::Result<()> {
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
match self.stream_inst.0.borrow_mut().1 {
|
match self.stream_inst.0.borrow_mut().stream_inst {
|
||||||
StreamInstance::OutputFile(_, ref mut file, _) => file.flush(),
|
StreamInstance::OutputFile(_, ref mut file, _) => file.flush(),
|
||||||
StreamInstance::TcpStream(_, ref mut tcp_stream) => tcp_stream.flush(),
|
StreamInstance::TcpStream(_, ref mut tcp_stream) => tcp_stream.flush(),
|
||||||
StreamInstance::TlsStream(_, ref mut tls_stream) => tls_stream.flush(),
|
StreamInstance::TlsStream(_, ref mut tls_stream) => tls_stream.flush(),
|
||||||
StreamInstance::Bytes(ref mut cursor) => cursor.flush(),
|
StreamInstance::Bytes(ref mut cursor) => cursor.flush(),
|
||||||
StreamInstance::Stdout => stdout().flush(),
|
StreamInstance::Stdout => stdout().flush(),
|
||||||
StreamInstance::PausedPrologStream(..)
|
StreamInstance::PausedPrologStream(..) |
|
||||||
| StreamInstance::StaticStr(_)
|
StreamInstance::StaticStr(_) |
|
||||||
| StreamInstance::ReadlineStream(_)
|
StreamInstance::ReadlineStream(_) |
|
||||||
| StreamInstance::InputFile(..)
|
StreamInstance::InputFile(..) |
|
||||||
| StreamInstance::Null => Err(std::io::Error::new(
|
StreamInstance::Null => Err(std::io::Error::new(
|
||||||
ErrorKind::PermissionDenied,
|
ErrorKind::PermissionDenied,
|
||||||
StreamError::FlushToInputStream,
|
StreamError::FlushToInputStream,
|
||||||
)),
|
)),
|
||||||
|
|||||||
@@ -467,7 +467,7 @@ impl MachineState {
|
|||||||
} else {
|
} else {
|
||||||
self.p.local() + 1
|
self.p.local() + 1
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(self.p = CodePtr::REPL(repl_code_ptr, p))
|
Ok(self.p = CodePtr::REPL(repl_code_ptr, p))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1319,7 +1319,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1395,7 +1395,7 @@ impl MachineState {
|
|||||||
2,
|
2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1417,7 +1417,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1491,7 +1491,7 @@ impl MachineState {
|
|||||||
2,
|
2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1520,7 +1520,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1610,7 +1610,7 @@ impl MachineState {
|
|||||||
2,
|
2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2027,7 +2027,7 @@ impl MachineState {
|
|||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
let string = self.heap_pstr_iter(self[temp_v!(2)]).to_string();
|
let string = self.heap_pstr_iter(self[temp_v!(2)]).to_string();
|
||||||
|
|
||||||
if stream.options.stream_type == StreamType::Binary {
|
if stream.options().stream_type == StreamType::Binary {
|
||||||
for c in string.chars() {
|
for c in string.chars() {
|
||||||
if c as u32 > 255 {
|
if c as u32 > 255 {
|
||||||
let stub = MachineError::functor_stub(clause_name!("$put_chars"), 2);
|
let stub = MachineError::functor_stub(clause_name!("$put_chars"), 2);
|
||||||
@@ -2172,7 +2172,7 @@ impl MachineState {
|
|||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
self.eof_action(self[temp_v!(2)], &mut stream, clause_name!("get_byte"), 2)?;
|
self.eof_action(self[temp_v!(2)], &mut stream, clause_name!("get_byte"), 2)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2253,7 +2253,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2327,7 +2327,7 @@ impl MachineState {
|
|||||||
2,
|
2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2363,7 +2363,7 @@ impl MachineState {
|
|||||||
|
|
||||||
let mut string = String::new();
|
let mut string = String::new();
|
||||||
|
|
||||||
if stream.options.stream_type == StreamType::Binary {
|
if stream.options().stream_type == StreamType::Binary {
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
let mut chunk = stream.take(num as u64);
|
let mut chunk = stream.take(num as u64);
|
||||||
chunk.read_to_end(&mut buf).ok();
|
chunk.read_to_end(&mut buf).ok();
|
||||||
@@ -2402,7 +2402,7 @@ impl MachineState {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
if stream.past_end_of_stream() {
|
if stream.past_end_of_stream() {
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2490,7 +2490,7 @@ impl MachineState {
|
|||||||
2,
|
2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if EOFAction::Reset != stream.options.eof_action {
|
if EOFAction::Reset != stream.options().eof_action {
|
||||||
return return_from_clause!(self.last_call, self);
|
return return_from_clause!(self.last_call, self);
|
||||||
} else if self.fail {
|
} else if self.fail {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -2664,8 +2664,8 @@ impl MachineState {
|
|||||||
if !stream.is_stdin() && !stream.is_stdout() {
|
if !stream.is_stdin() && !stream.is_stdout() {
|
||||||
stream.close();
|
stream.close();
|
||||||
|
|
||||||
if let Some(alias) = stream.options.alias {
|
if let Some(ref alias) = stream.options().alias {
|
||||||
indices.stream_aliases.remove(&alias);
|
indices.stream_aliases.remove(alias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3153,11 +3153,11 @@ impl MachineState {
|
|||||||
_ => self.stream_from_file_spec(clause_name!(""), indices, &options)?,
|
_ => self.stream_from_file_spec(clause_name!(""), indices, &options)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
stream.options = options;
|
*stream.options_mut() = options;
|
||||||
|
|
||||||
indices.streams.insert(stream.clone());
|
indices.streams.insert(stream.clone());
|
||||||
|
|
||||||
if let Some(ref alias) = &stream.options.alias {
|
if let Some(ref alias) = &stream.options().alias {
|
||||||
indices.stream_aliases.insert(alias.clone(), stream.clone());
|
indices.stream_aliases.insert(alias.clone(), stream.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4261,9 +4261,9 @@ impl MachineState {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
stream.options = options;
|
*stream.options_mut() = options;
|
||||||
|
|
||||||
if let Some(ref alias) = &stream.options.alias {
|
if let Some(ref alias) = &stream.options().alias {
|
||||||
indices.stream_aliases.insert(alias.clone(), stream.clone());
|
indices.stream_aliases.insert(alias.clone(), stream.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4398,9 +4398,9 @@ impl MachineState {
|
|||||||
let mut tcp_stream =
|
let mut tcp_stream =
|
||||||
Stream::from_tcp_stream(client.clone(), tcp_stream);
|
Stream::from_tcp_stream(client.clone(), tcp_stream);
|
||||||
|
|
||||||
tcp_stream.options = options;
|
*tcp_stream.options_mut() = options;
|
||||||
|
|
||||||
if let Some(ref alias) = &tcp_stream.options.alias {
|
if let Some(ref alias) = &tcp_stream.options().alias {
|
||||||
indices
|
indices
|
||||||
.stream_aliases
|
.stream_aliases
|
||||||
.insert(alias.clone(), tcp_stream.clone());
|
.insert(alias.clone(), tcp_stream.clone());
|
||||||
@@ -4467,7 +4467,7 @@ impl MachineState {
|
|||||||
let mut stream =
|
let mut stream =
|
||||||
self.get_stream_or_alias(self[temp_v!(1)], indices, "set_stream_position", 2)?;
|
self.get_stream_or_alias(self[temp_v!(1)], indices, "set_stream_position", 2)?;
|
||||||
|
|
||||||
if !stream.options.reposition {
|
if !stream.options().reposition {
|
||||||
let stub = MachineError::functor_stub(clause_name!("set_stream_position"), 2);
|
let stub = MachineError::functor_stub(clause_name!("set_stream_position"), 2);
|
||||||
|
|
||||||
let err = MachineError::permission_error(
|
let err = MachineError::permission_error(
|
||||||
@@ -4526,7 +4526,7 @@ impl MachineState {
|
|||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
"alias" => {
|
"alias" => {
|
||||||
if let Some(alias) = &stream.options.alias {
|
if let Some(alias) = &stream.options().alias {
|
||||||
HeapCellValue::Atom(alias.clone(), None)
|
HeapCellValue::Atom(alias.clone(), None)
|
||||||
} else {
|
} else {
|
||||||
self.fail = true;
|
self.fail = true;
|
||||||
@@ -4547,11 +4547,11 @@ impl MachineState {
|
|||||||
HeapCellValue::Atom(clause_name!(end_of_stream_pos.as_str()), None)
|
HeapCellValue::Atom(clause_name!(end_of_stream_pos.as_str()), None)
|
||||||
}
|
}
|
||||||
"eof_action" => HeapCellValue::Atom(
|
"eof_action" => HeapCellValue::Atom(
|
||||||
clause_name!(stream.options.eof_action.as_str()),
|
clause_name!(stream.options().eof_action.as_str()),
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
"reposition" => HeapCellValue::Atom(
|
"reposition" => HeapCellValue::Atom(
|
||||||
clause_name!(if stream.options.reposition {
|
clause_name!(if stream.options().reposition {
|
||||||
"true"
|
"true"
|
||||||
} else {
|
} else {
|
||||||
"false"
|
"false"
|
||||||
@@ -4559,7 +4559,7 @@ impl MachineState {
|
|||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
"type" => HeapCellValue::Atom(
|
"type" => HeapCellValue::Atom(
|
||||||
clause_name!(stream.options.stream_type.as_property_str()),
|
clause_name!(stream.options().stream_type.as_property_str()),
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
_ => {
|
_ => {
|
||||||
@@ -4815,7 +4815,7 @@ impl MachineState {
|
|||||||
|
|
||||||
let opt_err = if !stream.is_output_stream() {
|
let opt_err = if !stream.is_output_stream() {
|
||||||
Some("stream") // 8.14.2.3 g)
|
Some("stream") // 8.14.2.3 g)
|
||||||
} else if stream.options.stream_type == StreamType::Binary {
|
} else if stream.options().stream_type == StreamType::Binary {
|
||||||
Some("binary_stream") // 8.14.2.3 h)
|
Some("binary_stream") // 8.14.2.3 h)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
Reference in New Issue
Block a user