From 49ba5f30c5f9569ecdca24e492db60663719685c Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Tue, 12 May 2020 20:28:39 -0600 Subject: [PATCH] throw existence_error on failed put_byte write (#492) --- src/prolog/machine/system_calls.rs | 50 +++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/prolog/machine/system_calls.rs b/src/prolog/machine/system_calls.rs index 6ba35837..065bbabc 100644 --- a/src/prolog/machine/system_calls.rs +++ b/src/prolog/machine/system_calls.rs @@ -2065,14 +2065,56 @@ impl MachineState { match Number::try_from((addr, &self.heap)) { Ok(Number::Integer(n)) => { if let Some(nb) = n.to_u8() { - discard_result!(stream.write(&mut [nb])); - return return_from_clause!(self.last_call, self); + match stream.write(&mut [nb]) { + Ok(1) => { + return return_from_clause!(self.last_call, self); + } + _ => { + let stub = MachineError::functor_stub( + clause_name!("put_byte"), + 2, + ); + + let addr = self.heap.to_unifiable( + HeapCellValue::Stream(stream.clone()), + ); + + return Err(self.error_form( + MachineError::existence_error( + self.heap.h(), + ExistenceError::Stream(addr), + ), + stub, + )); + } + } } } Ok(Number::Fixnum(n)) => { if let Ok(nb) = u8::try_from(n) { - discard_result!(stream.write(&mut [nb])); - return return_from_clause!(self.last_call, self); + match stream.write(&mut [nb]) { + Ok(1) => { + return return_from_clause!(self.last_call, self); + } + _ => { + let stub = MachineError::functor_stub( + clause_name!("put_byte"), + 2, + ); + + let addr = self.heap.to_unifiable( + HeapCellValue::Stream(stream.clone()), + ); + + return Err(self.error_form( + MachineError::existence_error( + self.heap.h(), + ExistenceError::Stream(addr), + ), + stub, + )); + } + } } } _ => { -- 2.54.0