diff --git a/com/isoterminal/core.js b/com/isoterminal/core.js index e7730e1..42025c5 100644 --- a/com/isoterminal/core.js +++ b/com/isoterminal/core.js @@ -1,3 +1,11 @@ +//ISOTerminal.prototype.exec(cmd_array,stdin){ +// // exec(['lua'] "print \"hello\") ---> cat /dev/browser/js/stdin | lua > /dev/browser/js/stdout +//} + +ISOTerminal.prototype.send = function(ttyNr, str){ + this.toUint8Array( str ).map( (c) => this.emulator.bus.send(`serial${ttyNr}-input`, c ) ) +} + ISOTerminal.prototype.toUint8Array = function(str) { str = String(str) || String("") // Create a new Uint8Array with the same length as the input string @@ -17,6 +25,9 @@ ISOTerminal.prototype.runISO = function(opts){ if( opts.iso.match(/\.bin$/) ) image.bzimage = { url: opts.iso } let emulator = this.emulator = new V86({ ...image, + uart1:true, // /dev/ttyS1 + uart2:true, // /dev/ttyS2 + uart3:true, // /dev/ttyS3 wasm_path: "com/isoterminal/v86.wasm", memory_size: 32 * 1024 * 1024, vga_memory_size: 2 * 1024 * 1024, diff --git a/com/isoterminal/feat/9pfs_utils.js b/com/isoterminal/feat/9pfs_utils.js index 53a225b..18ca419 100644 --- a/com/isoterminal/feat/9pfs_utils.js +++ b/com/isoterminal/feat/9pfs_utils.js @@ -2,32 +2,6 @@ ISOTerminal.addEventListener('emulator-started', function(){ let emulator = this.emulator let isoterminal = this - emulator.fs9p.Read = async function(inodeid, offset, count){ - let file - const inode = this.inodes[inodeid]; - const inodeDir = this.GetParent(inode.fid) - - if( !inodeDir ){ // undefined = /mnt - for( const [name,childid] of this.inodes[0].direntries ){ - if( childid == inode.fid ){ file = name } - } - } - - if( file ){ - let data = {promise:false, file} - isoterminal.emit('file-read', data) - // if( data.promise ){ return await data.promise } // *FIX* size already - } - - if(this.is_forwarder(inode)) - { - const foreign_id = inode.foreign_id; - return await this.follow_fs(inode).Read(foreign_id, offset, count); - } - - return await this.get_data(inodeid, offset, count); - }; - emulator.fs9p.update_file = async function(file,data){ const p = this.SearchPath(file); diff --git a/com/isoterminal/feat/boot.js b/com/isoterminal/feat/boot.js index dd9019a..1b6368d 100644 --- a/com/isoterminal/feat/boot.js +++ b/com/isoterminal/feat/boot.js @@ -10,7 +10,7 @@ ISOTerminal.prototype.boot = async function(){ env.push( 'export '+String(i).toUpperCase()+'="'+document.location[i]+'"') } await this.emulator.create_file("profile.browser", this.toUint8Array( env.join('\n') ) ) - let boot = `source /mnt/profile ; js "$(cat /mnt/profile.js)"` + let boot = `source /mnt/profile` // exec hash as extra boot cmd if( document.location.hash.length > 1 ){ boot += ` ; cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd` diff --git a/com/isoterminal/feat/javascript.js b/com/isoterminal/feat/javascript.js index edabe84..0b4ad67 100644 --- a/com/isoterminal/feat/javascript.js +++ b/com/isoterminal/feat/javascript.js @@ -9,9 +9,16 @@ ISOTerminal.addEventListener('init', function(){ const buf = await emulator.read_file("dev/browser/js") const decoder = new TextDecoder('utf-8'); const script = decoder.decode(buf) + let PID="?" try{ + if( script.match(/^PID/) ){ + PID = script.match(/^PID=([0-9]+);/)[1] + } let res = (new Function(`${script}`))() if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2) + // write output to 9p with PID as filename + // *FIXME* not flexible / robust + emulator.create_file(PID, this.toUint8Array(res) ) }catch(e){ console.error(e) } diff --git a/com/isoterminal/mnt/js b/com/isoterminal/mnt/js index ba14975..fc453e3 100755 --- a/com/isoterminal/mnt/js +++ b/com/isoterminal/mnt/js @@ -1,4 +1,5 @@ #!/bin/sh +PID=$$ test -z "$1" && { echo "Usage: js 'somefunction(1)'"; exit 0; } test -n "$BROWSER" || { alert warning "/dev/browser not active (are you running outside of v86?)"; } @@ -10,7 +11,19 @@ case "$1" in ;; esac -echo -n "$javascript" > /dev/browser/js +# below is not ideal +# first I tried /dev/ttyS* https://github.com/copy/v86/issues/530 +# and differentiate processes by prefixing output by PID's +# to parse it later with AWK, but it was very hairy + +OUTPUT=/mnt/$PID +echo -n "PID=$PID; $javascript" > /dev/browser/js +sleep 0.1 +test -f $OUTPUT && { + cat $OUTPUT + rm $OUTPUT +} + # should we use flock, an awesome way to make processes read/write the same file # while preventing 1001 concurrency issues? diff --git a/com/isoterminal/mnt/jsh b/com/isoterminal/mnt/jsh index c155bbc..eba95ee 100755 --- a/com/isoterminal/mnt/jsh +++ b/com/isoterminal/mnt/jsh @@ -25,7 +25,7 @@ to_js(){ test -z "$1" || { func=$(to_js "$@") func=${func/,)/)} - js "$func" + js "return $func" hook "$@" exit 0 } @@ -35,7 +35,7 @@ echo "jsh> type 'exit' or CTRL-C to quit" echo "jsh> HINT: to run alert('foo') outside this REPL, run 'jsh alert foo'" echo "jsh>" while true; do - echo -n -e "\r$(printf "\033[0m")jsh> $(printf "\033[0m")" + echo -n -e "\n$(printf "\033[0m")jsh> $(printf "\033[0m")" read line test "$line" = exit && exit js "$line" diff --git a/com/isoterminal/mnt/profile b/com/isoterminal/mnt/profile index 01135c0..67e6be5 100644 --- a/com/isoterminal/mnt/profile +++ b/com/isoterminal/mnt/profile @@ -8,7 +8,7 @@ source /mnt/profile.xrsh source /mnt/profile.sh # source js functions -js "$(cat ~/.profile.js)" +js "$(cat ~/.profile.js)" &>/dev/null & ## forward not-found commands to javascript (via jsh) command_not_found_handle(){ @@ -16,11 +16,13 @@ command_not_found_handle(){ echo "" echo 'TIPS' echo '----' - echo 'js console: ' "type 'jsh'" - echo 'js shellfunction:' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')" - echo 'js log to tty: ' "type 'echo 1 > /dev/browser/console.tty' to enable" - echo 'js capture log: ' "type 'tail -f /dev/browser/console'" - echo 'jsh<->sh hooks: ' "type 'chmod +x ~/hook.d/*/* && alert helloworld'" + echo 'js run ' "type 'js 'alert(\"hello\")'" + echo 'js console.log: ' "type 'console document.baseURI" + echo 'js function as cmd: ' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')" + echo 'js inspect: ' "type 'js \"return document.baseURI\"'" + echo 'js console disable: ' "type 'echo 0 > /dev/browser/console.tty' to enable" + echo 'js capture console: ' "type 'tail -f /dev/browser/console'" + echo 'jsh<->sh hooks: ' "type 'chmod +x ~/hook.d/*/* && alert helloworld'" } resize diff --git a/com/isoterminal/mnt/profile.js b/com/isoterminal/mnt/profile.js index aea7364..ab44e7f 100644 --- a/com/isoterminal/mnt/profile.js +++ b/com/isoterminal/mnt/profile.js @@ -1,3 +1,4 @@ window.helloworld = function(){ alert("hello world") + return "hello world" } diff --git a/com/isoterminal/mnt/profile.sh b/com/isoterminal/mnt/profile.sh index d0fa5d1..84e50fc 100644 --- a/com/isoterminal/mnt/profile.sh +++ b/com/isoterminal/mnt/profile.sh @@ -34,3 +34,11 @@ prompt(){ echo "$answer" hook prompt $1 $answer } + +console(){ + js 'return '$1 +} + +fetch(){ + js '' +} diff --git a/com/isoterminal/mnt/profile.xrsh b/com/isoterminal/mnt/profile.xrsh index 31ccdbe..df7c4cb 100644 --- a/com/isoterminal/mnt/profile.xrsh +++ b/com/isoterminal/mnt/profile.xrsh @@ -25,7 +25,7 @@ test -d /dev/browser || { # emulator.write_file() only writes to /mnt/. :( # should be in /proc, but v86 gives 'no such file or dir' when creating it there ln -s /mnt/console.tty /dev/browser/console.tty - echo 0 > /dev/browser/console.tty + echo 1 > /dev/browser/console.tty touch /mnt/console && ln -s /mnt/console /dev/browser/console touch /mnt/index.html && ln -s /mnt/index.html /dev/browser/index.html ln -s /dev/browser/index.html ~/index.html @@ -38,7 +38,7 @@ test -d /dev/browser || { mkdir ~/bin mkdir -p ~/hook.d/alert echo -e "#!/bin/sh\necho hook.d/alert/yo: yo \$*" > ~/hook.d/alert/yo - echo -e "#!/bin/js\nalert(\"hook.d/alert/yo.js \"+args.slice(1).join(' '))" > ~/hook.d/alert/yo.js + echo -e "#!/bin/js\nstr = \"hook.d/alert/yo.js yo \"+args.slice(1).join(' ')\nalert(str)\nreturn str" > ~/hook.d/alert/yo.js echo -e "#!/usr/bin/env lua\nprint(\"hook.d/alert/yo.lua: yo \" .. arg[1])" > ~/hook.d/alert/yo.lua echo -e "#!/usr/bin/awk -f\nBEGIN{\n\tprint \"hook.d/alert/yo.awk: yo \" ARGV[1]\n}" > ~/hook.d/alert/yo.awk echo -e "#!/bin/sh\necho hello \$*" > ~/bin/hello