Fix to exit correctly when quit() or something is called

This commit is contained in:
Shunsuke Shibayama 2022-08-18 20:27:28 +09:00
parent 3314825fc1
commit 1078345a98
4 changed files with 35 additions and 3 deletions

View file

@ -10,7 +10,7 @@ use std::vec::IntoIter;
use crate::color::{GREEN, RESET};
use crate::config::{ErgConfig, Input, BUILD_DATE, GIT_HASH_SHORT, SEMVER};
use crate::error::{ErrorDisplay, Location, MultiErrorDisplay};
use crate::error::{ErrorDisplay, ErrorKind, Location, MultiErrorDisplay};
use crate::ty::Type;
use crate::Str;
use crate::{addr_eq, chomp, log, switch_unreachable};
@ -366,8 +366,17 @@ pub trait Runnable: Sized {
output.write_all((out + "\n").as_bytes()).unwrap();
output.flush().unwrap();
}
Err(e) => {
e.fmt_all_stderr();
Err(errs) => {
if errs
.first()
.map(|e| e.core().kind == ErrorKind::SystemExit)
.unwrap_or(false)
{
instance.finish();
log!(f output, "{GREEN}[DEBUG] The REPL has finished successfully.{RESET}\n");
process::exit(0);
}
errs.fmt_all_stderr();
}
}
output.write_all(instance.ps1().as_bytes()).unwrap();

View file

@ -198,6 +198,23 @@ impl CompileError {
caused_by,
)
}
pub fn system_exit() -> Self {
Self::new(
ErrorCore::new(
0,
SystemExit,
Location::Unknown,
switch_lang!(
"japanese" => format!("システムを終了します"),
"english" => format!("system is exiting"),
),
None,
),
Input::Dummy,
"".into(),
)
}
}
#[derive(Debug)]

View file

@ -104,6 +104,9 @@ impl Runnable for DummyVM {
match self.stream.as_mut().unwrap().read(&mut buf) {
Result::Ok(n) => {
let s = std::str::from_utf8(&buf[..n]).unwrap();
if s == "[Exception] SystemExit" {
return Err(CompileErrors::from(CompileError::system_exit()));
}
s.to_string()
}
Result::Err(e) => {

View file

@ -25,6 +25,9 @@ while True:
__res = str(exec('__importlib.reload(o)'))
else:
__res = str(exec('import o'))
except SystemExit:
__client_socket.send('[Exception] SystemExit'.encode())
continue
except e:
__res = str(e)
__already_loaded = True