Skip to content

Commit

Permalink
Add std.exit
Browse files Browse the repository at this point in the history
  • Loading branch information
gahag committed May 14, 2022
1 parent da570b0 commit 558d441
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/runtime/lib/exit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::convert::TryFrom;

use gc::{Finalize, Trace};

use super::{
CallContext,
NativeFun,
RustFun,
Panic,
Value,
};


inventory::submit!{ RustFun::from(Exit) }

#[derive(Trace, Finalize)]
struct Exit;

impl NativeFun for Exit {
fn name(&self) -> &'static str { "std.exit" }

fn call(&self, context: CallContext) -> Result<Value, Panic> {
match context.args() {
[ val @ Value::Int(i) ] => {
let code = u8::try_from(*i)
.map_err(|_| Panic::value_error(val.copy(), "valid exit code", context.pos.copy()))?;

std::process::exit(code.into())
}

[ other ] => Err(Panic::type_error(other.copy(), "int", context.pos)),
args => Err(Panic::invalid_args(args.len() as u32, 1, context.pos))
}
}
}

0 comments on commit 558d441

Please sign in to comment.