js now returns output
/ mirror_to_github (push) Successful in 20s Details
/ test (push) Successful in 5s Details

This commit is contained in:
Leon van Kammen 2024-09-05 17:09:40 +00:00
parent af7e837554
commit 98c3adb791
10 changed files with 54 additions and 38 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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`

View File

@ -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)
}

View File

@ -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?

View File

@ -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"

View File

@ -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,10 +16,12 @@ 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 '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'"
}

View File

@ -1,3 +1,4 @@
window.helloworld = function(){
alert("hello world")
return "hello world"
}

View File

@ -34,3 +34,11 @@ prompt(){
echo "$answer"
hook prompt $1 $answer
}
console(){
js 'return '$1
}
fetch(){
js ''
}

View File

@ -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