Merge pull request #3084 from thierrymarianne/set_stream
Prevent top-level from panicking when calling `set_input/1` with `Stream` variants.
This commit is contained in:
@@ -745,25 +745,50 @@ impl MachineState {
|
|||||||
stream: Stream,
|
stream: Stream,
|
||||||
indices: &mut IndexStore,
|
indices: &mut IndexStore,
|
||||||
) -> CallResult {
|
) -> CallResult {
|
||||||
if let Stream::Readline(ptr) = stream {
|
match stream {
|
||||||
let readline = unsafe { ptr.as_ptr().as_mut() }.unwrap();
|
#[cfg(feature = "http")]
|
||||||
readline.set_atoms_for_completion(&self.atom_tbl);
|
Stream::HttpRead(_) => self.read_term(
|
||||||
return self.read_term(
|
|
||||||
stream,
|
stream,
|
||||||
indices,
|
indices,
|
||||||
MachineState::read_term_from_user_input_eof_handler,
|
MachineState::read_term_from_user_input_eof_handler,
|
||||||
);
|
),
|
||||||
}
|
#[cfg(feature = "tls")]
|
||||||
|
Stream::NamedTls(_) => self.read_term(
|
||||||
if let Stream::Byte(_) = stream {
|
|
||||||
return self.read_term(
|
|
||||||
stream,
|
stream,
|
||||||
indices,
|
indices,
|
||||||
MachineState::read_term_from_user_input_eof_handler,
|
MachineState::read_term_from_user_input_eof_handler,
|
||||||
);
|
),
|
||||||
}
|
Stream::Readline(ptr) => {
|
||||||
|
let readline = unsafe { ptr.as_ptr().as_mut() }.unwrap();
|
||||||
|
readline.set_atoms_for_completion(&self.atom_tbl);
|
||||||
|
self.read_term(
|
||||||
|
stream,
|
||||||
|
indices,
|
||||||
|
MachineState::read_term_from_user_input_eof_handler,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Stream::Byte(_)
|
||||||
|
| Stream::InputChannel(_)
|
||||||
|
| Stream::InputFile(_)
|
||||||
|
| Stream::NamedTcp(_)
|
||||||
|
| Stream::Null(_)
|
||||||
|
| Stream::PipeReader(_)
|
||||||
|
| Stream::StaticString(_) => self.read_term(
|
||||||
|
stream,
|
||||||
|
indices,
|
||||||
|
MachineState::read_term_from_user_input_eof_handler,
|
||||||
|
),
|
||||||
|
_ => {
|
||||||
|
let stub = functor_stub(atom!("read_term_from_user_input"), 3);
|
||||||
|
let err = self.permission_error(
|
||||||
|
Permission::InputStream,
|
||||||
|
atom!("stream"),
|
||||||
|
atom_as_cell!(atom!("user_input")),
|
||||||
|
);
|
||||||
|
|
||||||
unreachable!("Stream must be a Stream::Readline(_)")
|
Err(self.error_form(err, stub))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_term_eof_handler(&mut self, mut stream: Stream) -> Result<OnEOF, MachineStub> {
|
pub fn read_term_eof_handler(&mut self, mut stream: Stream) -> Result<OnEOF, MachineStub> {
|
||||||
|
|||||||
@@ -6735,7 +6735,10 @@ impl Machine {
|
|||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn read_query_term(&mut self) -> CallResult {
|
pub(crate) fn read_query_term(&mut self) -> CallResult {
|
||||||
self.user_input.reset();
|
match self.user_input {
|
||||||
|
Stream::Byte(_) | Stream::Readline(_) => self.user_input.reset(),
|
||||||
|
_ => true,
|
||||||
|
};
|
||||||
|
|
||||||
set_prompt(true);
|
set_prompt(true);
|
||||||
// let result = self.machine_st.read_term(self.user_input, &mut self.indices);
|
// let result = self.machine_st.read_term(self.user_input, &mut self.indices);
|
||||||
@@ -6747,7 +6750,10 @@ impl Machine {
|
|||||||
match result {
|
match result {
|
||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.user_input.reset();
|
match self.user_input {
|
||||||
|
Stream::Byte(_) | Stream::Readline(_) => self.user_input.reset(),
|
||||||
|
_ => true,
|
||||||
|
};
|
||||||
Err(e)
|
Err(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
true.
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
:- use_module(library(files)).
|
||||||
|
|
||||||
|
main :-
|
||||||
|
current_input(UserStream),
|
||||||
|
open('./input', read, InputStream),
|
||||||
|
set_input(InputStream),
|
||||||
|
read_term(T, []), write(T),
|
||||||
|
set_input(UserStream).
|
||||||
|
|
||||||
|
:- initialization(main).
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
true
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
args = ["-f", "--no-add-history", "-g", "consult('set_input_from_top-level.pl').", "-g", "halt."]
|
||||||
Reference in New Issue
Block a user