@@ -19,16 +19,39 @@ if( typeof emulator != 'undefined' ){
1919
2020} else {
2121 // inside browser-thread
22-
22+
23+
24+ /*
25+ * here we're going to execute javascript in the browser,
26+ * both a terrible and great idea depending on a corporate vs personal computing angle.
27+ *
28+ * if a function-string gets evaluated, and it returns a promise..then we assume async.
29+ */
2330 ISOTerminal . addEventListener ( 'javascript-eval' , async function ( e ) {
2431 const { script, PID } = e . detail
2532 let res ;
33+ let error = false ;
34+
35+ const output = ( res , PID ) => {
36+ if ( res && typeof res != 'string' ) res = JSON . stringify ( res , null , 2 )
37+ // update output to 9p with PID as filename (in /mnt/run)
38+ if ( PID ) {
39+ this . worker . update_file ( `run/${ PID } .exit` , error ? "1" : "0" )
40+ this . worker . update_file ( `run/${ PID } ` , this . convert . toUint8Array ( res ) )
41+ }
42+ }
2643
2744 try {
2845 let f = new Function ( `${ script } ` ) ;
2946 res = f ( ) ;
30- if ( res && typeof res != 'string' ) res = JSON . stringify ( res , null , 2 )
47+ if ( res && typeof res . then == 'function' ) { // if we got a promise
48+ res . then ( ( res ) => output ( res , PID ) )
49+ res . catch ( ( e ) => output ( e , PID ) )
50+ } else { // normal sync function
51+ output ( res , PID )
52+ }
3153 } catch ( err ) {
54+ error = true
3255 console . error ( err )
3356 console . dir ( err )
3457 res = "error: " + err . toString ( )
@@ -41,10 +64,7 @@ if( typeof emulator != 'undefined' ){
4164 res += script . split ( "\n" ) [ lnr - 1 ]
4265 } else console . dir ( script )
4366 console . error ( res )
44- }
45- // update output to 9p with PID as filename (in /mnt/run)
46- if ( PID ) {
47- this . worker . update_file ( `run/${ PID } ` , this . convert . toUint8Array ( res ) )
67+ output ( res , PID )
4868 }
4969 } )
5070
0 commit comments