1- use compiler_lib:: modules:: { lexer:: lexer, parser:: Parser , vm:: VM } ;
1+ use compiler_lib:: modules:: { lexer:: lexer, parser:: Parser , vm:: { VM , Limits } } ;
22use std:: { env, fs, process:: exit} ;
33use log:: { debug, info, error} ;
44
5- fn parse_args ( ) -> ( String , usize , bool ) {
5+ fn parse_args ( ) -> ( String , usize , bool , bool ) {
66 let args: Vec < _ > = env:: args ( ) . skip ( 1 ) . collect ( ) ;
7- if args. is_empty ( ) || args. contains ( & "-h" . into ( ) ) {
8- println ! ( "usage: edge [-d] [-dd] [-q] <file>" ) ; exit ( 0 )
7+ if args. is_empty ( ) || args. contains ( & "-h" . into ( ) ) {
8+ println ! ( "usage: edge [-d] [-dd] [-q] [--sandbox] <file>" ) ; exit ( 0 )
99 }
1010 let p = args. iter ( ) . find ( |& a| !a. starts_with ( '-' ) ) . cloned ( ) . unwrap_or_else ( || {
1111 eprintln ! ( "abort: execution failed because no input target was specified" ) ; exit ( 1 )
1212 } ) ;
1313 let v = args. iter ( ) . filter ( |& a| a == "-d" ) . count ( ) + ( args. iter ( ) . filter ( |& a| a == "-dd" ) . count ( ) * 2 ) ;
14- ( p, if v > 0 { v + 2 } else { 0 } , args. contains ( & "-q" . into ( ) ) )
14+ ( p, if v > 0 { v + 2 } else { 0 } , args. contains ( & "-q" . into ( ) ) , args . contains ( & "--sandbox" . into ( ) ) )
1515}
1616
17- fn run ( path : & str , v : usize , q : bool ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
17+ fn run ( path : & str , v : usize , q : bool , sandbox : bool ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
1818 stderrlog:: new ( ) . module ( module_path ! ( ) ) . verbosity ( v) . quiet ( q) . init ( ) . ok ( ) ;
1919
2020 let src = fs:: read_to_string ( path) . map_err ( |e| format ! ( "io: cannot access '{}' because {}" , path, e) ) ?;
@@ -29,9 +29,10 @@ fn run(path: &str, v: usize, q: bool) -> Result<(), Box<dyn std::error::Error>>
2929
3030 info ! ( "emit: snapshot created [ops={} consts={}]" , chunk. instructions. len( ) , chunk. constants. len( ) ) ;
3131
32- let mut vm = VM :: new ( & chunk) ;
32+ let limits = if sandbox { Limits :: sandbox ( ) } else { Limits :: none ( ) } ;
33+ let mut vm = VM :: with_limits ( & chunk, limits) ;
3334 vm. run ( ) . map_err ( |e| format ! ( "trap: cpu-stop triggered by '{}' (illegal operation in main thread)" , e) ) ?;
34-
35+
3536 vm. output . iter ( ) . for_each ( |l| println ! ( "{l}" ) ) ;
3637
3738 let ( sp, tot) = vm. cache_stats ( ) ;
@@ -40,9 +41,9 @@ fn run(path: &str, v: usize, q: bool) -> Result<(), Box<dyn std::error::Error>>
4041}
4142
4243fn main ( ) {
43- let ( p, v, q) = parse_args ( ) ;
44- if let Err ( e) = run ( & p, v, q) {
45- error ! ( "process terminated: {}" , e) ;
46- exit ( 1 )
44+ let ( p, v, q, sandbox ) = parse_args ( ) ;
45+ if let Err ( e) = run ( & p, v, q, sandbox ) {
46+ error ! ( "process terminated: {}" , e) ;
47+ exit ( 1 )
4748 }
4849}
0 commit comments