js now returns output
This commit is contained in:
parent
af7e837554
commit
98c3adb791
|
@ -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) {
|
ISOTerminal.prototype.toUint8Array = function(str) {
|
||||||
str = String(str) || String("")
|
str = String(str) || String("")
|
||||||
// Create a new Uint8Array with the same length as the input 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 }
|
if( opts.iso.match(/\.bin$/) ) image.bzimage = { url: opts.iso }
|
||||||
|
|
||||||
let emulator = this.emulator = new V86({ ...image,
|
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",
|
wasm_path: "com/isoterminal/v86.wasm",
|
||||||
memory_size: 32 * 1024 * 1024,
|
memory_size: 32 * 1024 * 1024,
|
||||||
vga_memory_size: 2 * 1024 * 1024,
|
vga_memory_size: 2 * 1024 * 1024,
|
||||||
|
|
|
@ -2,32 +2,6 @@ ISOTerminal.addEventListener('emulator-started', function(){
|
||||||
let emulator = this.emulator
|
let emulator = this.emulator
|
||||||
let isoterminal = this
|
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){
|
emulator.fs9p.update_file = async function(file,data){
|
||||||
|
|
||||||
const p = this.SearchPath(file);
|
const p = this.SearchPath(file);
|
||||||
|
|
|
@ -10,7 +10,7 @@ ISOTerminal.prototype.boot = async function(){
|
||||||
env.push( 'export '+String(i).toUpperCase()+'="'+document.location[i]+'"')
|
env.push( 'export '+String(i).toUpperCase()+'="'+document.location[i]+'"')
|
||||||
}
|
}
|
||||||
await this.emulator.create_file("profile.browser", this.toUint8Array( env.join('\n') ) )
|
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
|
// exec hash as extra boot cmd
|
||||||
if( document.location.hash.length > 1 ){
|
if( document.location.hash.length > 1 ){
|
||||||
boot += ` ; cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd`
|
boot += ` ; cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd`
|
||||||
|
|
|
@ -9,9 +9,16 @@ ISOTerminal.addEventListener('init', function(){
|
||||||
const buf = await emulator.read_file("dev/browser/js")
|
const buf = await emulator.read_file("dev/browser/js")
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
const script = decoder.decode(buf)
|
const script = decoder.decode(buf)
|
||||||
|
let PID="?"
|
||||||
try{
|
try{
|
||||||
|
if( script.match(/^PID/) ){
|
||||||
|
PID = script.match(/^PID=([0-9]+);/)[1]
|
||||||
|
}
|
||||||
let res = (new Function(`${script}`))()
|
let res = (new Function(`${script}`))()
|
||||||
if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2)
|
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){
|
}catch(e){
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
PID=$$
|
||||||
test -z "$1" && { echo "Usage: js 'somefunction(1)'"; exit 0; }
|
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?)"; }
|
test -n "$BROWSER" || { alert warning "/dev/browser not active (are you running outside of v86?)"; }
|
||||||
|
@ -10,7 +11,19 @@ case "$1" in
|
||||||
;;
|
;;
|
||||||
esac
|
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
|
# should we use flock, an awesome way to make processes read/write the same file
|
||||||
# while preventing 1001 concurrency issues?
|
# while preventing 1001 concurrency issues?
|
||||||
|
|
|
@ -25,7 +25,7 @@ to_js(){
|
||||||
test -z "$1" || {
|
test -z "$1" || {
|
||||||
func=$(to_js "$@")
|
func=$(to_js "$@")
|
||||||
func=${func/,)/)}
|
func=${func/,)/)}
|
||||||
js "$func"
|
js "return $func"
|
||||||
hook "$@"
|
hook "$@"
|
||||||
exit 0
|
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> HINT: to run alert('foo') outside this REPL, run 'jsh alert foo'"
|
||||||
echo "jsh>"
|
echo "jsh>"
|
||||||
while true; do
|
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
|
read line
|
||||||
test "$line" = exit && exit
|
test "$line" = exit && exit
|
||||||
js "$line"
|
js "$line"
|
||||||
|
|
|
@ -8,7 +8,7 @@ source /mnt/profile.xrsh
|
||||||
source /mnt/profile.sh
|
source /mnt/profile.sh
|
||||||
|
|
||||||
# source js functions
|
# source js functions
|
||||||
js "$(cat ~/.profile.js)"
|
js "$(cat ~/.profile.js)" &>/dev/null &
|
||||||
|
|
||||||
## forward not-found commands to javascript (via jsh)
|
## forward not-found commands to javascript (via jsh)
|
||||||
command_not_found_handle(){
|
command_not_found_handle(){
|
||||||
|
@ -16,11 +16,13 @@ command_not_found_handle(){
|
||||||
echo ""
|
echo ""
|
||||||
echo 'TIPS'
|
echo 'TIPS'
|
||||||
echo '----'
|
echo '----'
|
||||||
echo 'js console: ' "type 'jsh'"
|
echo 'js run ' "type 'js 'alert(\"hello\")'"
|
||||||
echo 'js shellfunction:' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')"
|
echo 'js console.log: ' "type 'console document.baseURI"
|
||||||
echo 'js log to tty: ' "type 'echo 1 > /dev/browser/console.tty' to enable"
|
echo 'js function as cmd: ' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')"
|
||||||
echo 'js capture log: ' "type 'tail -f /dev/browser/console'"
|
echo 'js inspect: ' "type 'js \"return document.baseURI\"'"
|
||||||
echo 'jsh<->sh hooks: ' "type 'chmod +x ~/hook.d/*/* && alert helloworld'"
|
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
|
resize
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
window.helloworld = function(){
|
window.helloworld = function(){
|
||||||
alert("hello world")
|
alert("hello world")
|
||||||
|
return "hello world"
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,3 +34,11 @@ prompt(){
|
||||||
echo "$answer"
|
echo "$answer"
|
||||||
hook prompt $1 $answer
|
hook prompt $1 $answer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console(){
|
||||||
|
js 'return '$1
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(){
|
||||||
|
js ''
|
||||||
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ test -d /dev/browser || {
|
||||||
# emulator.write_file() only writes to /mnt/. :(
|
# emulator.write_file() only writes to /mnt/. :(
|
||||||
# should be in /proc, but v86 gives 'no such file or dir' when creating it there
|
# 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
|
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/console && ln -s /mnt/console /dev/browser/console
|
||||||
touch /mnt/index.html && ln -s /mnt/index.html /dev/browser/index.html
|
touch /mnt/index.html && ln -s /mnt/index.html /dev/browser/index.html
|
||||||
ln -s /dev/browser/index.html ~/index.html
|
ln -s /dev/browser/index.html ~/index.html
|
||||||
|
@ -38,7 +38,7 @@ test -d /dev/browser || {
|
||||||
mkdir ~/bin
|
mkdir ~/bin
|
||||||
mkdir -p ~/hook.d/alert
|
mkdir -p ~/hook.d/alert
|
||||||
echo -e "#!/bin/sh\necho hook.d/alert/yo: yo \$*" > ~/hook.d/alert/yo
|
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/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 "#!/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
|
echo -e "#!/bin/sh\necho hello \$*" > ~/bin/hello
|
||||||
|
|
Loading…
Reference in New Issue