@@ -321,7 +321,8 @@ fn write_profile(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>>
321321}
322322
323323fn run_rustpython ( vm : & VirtualMachine , matches : & ArgMatches ) -> PyResult < ( ) > {
324- import:: init_importlib ( & vm, true ) ?;
324+ // We only include the standard library bytecode in WASI
325+ import:: init_importlib ( & vm, cfg ! ( not( target_os = "wasi" ) ) ) ?;
325326
326327 if let Some ( paths) = option_env ! ( "BUILDTIME_RUSTPYTHONPATH" ) {
327328 let sys_path = vm. get_attribute ( vm. sys_module . clone ( ) , "path" ) ?;
@@ -486,6 +487,7 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
486487 }
487488}
488489
490+ #[ cfg( not( target_os = "wasi" ) ) ]
489491fn run_shell ( vm : & VirtualMachine , scope : Scope ) -> PyResult < ( ) > {
490492 use rustyline:: { error:: ReadlineError , Editor } ;
491493
@@ -596,3 +598,79 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
596598
597599 Ok ( ( ) )
598600}
601+
602+ #[ cfg( target_os = "wasi" ) ]
603+ fn run_shell ( vm : & VirtualMachine , scope : Scope ) -> PyResult < ( ) > {
604+ use std:: io:: prelude:: * ;
605+ use std:: io:: { self , BufRead } ;
606+
607+ println ! (
608+ "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596} " ,
609+ crate_version!( )
610+ ) ;
611+
612+ // Read a single line:
613+ let mut input = String :: new ( ) ;
614+ let mut continuing = false ;
615+
616+ loop {
617+ let prompt_name = if continuing { "ps2" } else { "ps1" } ;
618+ let prompt = vm
619+ . get_attribute ( vm. sys_module . clone ( ) , prompt_name)
620+ . and_then ( |prompt| vm. to_str ( & prompt) ) ;
621+ let prompt = match prompt {
622+ Ok ( ref s) => s. as_str ( ) ,
623+ Err ( _) => "" ,
624+ } ;
625+ print ! ( "{}" , prompt) ;
626+ io:: stdout ( ) . flush ( ) . ok ( ) . expect ( "Could not flush stdout" ) ;
627+
628+ let stdin = io:: stdin ( ) ;
629+
630+ let result = match stdin. lock ( ) . lines ( ) . next ( ) . unwrap ( ) {
631+ Ok ( line) => {
632+ debug ! ( "You entered {:?}" , line) ;
633+ let stop_continuing = line. is_empty ( ) ;
634+
635+ if input. is_empty ( ) {
636+ input = line;
637+ } else {
638+ input. push_str ( & line) ;
639+ }
640+ input. push_str ( "\n " ) ;
641+
642+ if continuing {
643+ if stop_continuing {
644+ continuing = false ;
645+ } else {
646+ continue ;
647+ }
648+ }
649+
650+ match shell_exec ( vm, & input, scope. clone ( ) ) {
651+ ShellExecResult :: Ok => {
652+ input. clear ( ) ;
653+ Ok ( ( ) )
654+ }
655+ ShellExecResult :: Continue => {
656+ continuing = true ;
657+ Ok ( ( ) )
658+ }
659+ ShellExecResult :: PyErr ( err) => {
660+ input. clear ( ) ;
661+ Err ( err)
662+ }
663+ }
664+ }
665+ Err ( err) => {
666+ eprintln ! ( "Readline error: {:?}" , err) ;
667+ break ;
668+ }
669+ } ;
670+
671+ if let Err ( exc) = result {
672+ print_exception ( vm, & exc) ;
673+ }
674+ }
675+ Ok ( ( ) )
676+ }
0 commit comments