File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -127,6 +127,24 @@ impl<'src> Environment<'src> {
127127 Ok ( Value :: Null )
128128 } ) ;
129129
130+ env. add_builtin_function ( "quit" , |arguments, span| {
131+ if arguments. is_empty ( ) {
132+ process:: exit ( 0 ) ;
133+ }
134+
135+ if arguments. len ( ) != 1 {
136+ return Err ( Error :: new (
137+ span,
138+ format ! (
139+ "Function `quit` expects 0 or 1 arguments, got {}" ,
140+ arguments. len( )
141+ ) ,
142+ ) ) ;
143+ }
144+
145+ process:: exit ( arguments[ 0 ] . number ( span) ? as i32 ) ;
146+ } ) ;
147+
130148 env. add_variable ( "e" , Value :: Number ( std:: f64:: consts:: E ) ) ;
131149 env. add_variable ( "pi" , Value :: Number ( std:: f64:: consts:: PI ) ) ;
132150
Original file line number Diff line number Diff line change @@ -624,3 +624,18 @@ fn len() -> Result {
624624 . expected_stdout ( Exact ( "13\n " ) )
625625 . run ( )
626626}
627+
628+ #[ test]
629+ fn quit ( ) -> Result {
630+ Test :: new ( ) ?. program ( "quit()" ) . expected_status ( 0 ) . run ( ) ?;
631+
632+ Test :: new ( ) ?. program ( "quit(1)" ) . expected_status ( 1 ) . run ( ) ?;
633+
634+ Test :: new ( ) ?
635+ . program ( "quit(1, 2)" )
636+ . expected_status ( 1 )
637+ . expected_stderr ( Contains (
638+ "Function `quit` expects 0 or 1 arguments, got 2" ,
639+ ) )
640+ . run ( )
641+ }
You can’t perform that action at this time.
0 commit comments