xrsh-buildroot/buildroot-v86/board/v86/rootfs_overlay/bin/js

41 lines
1.1 KiB
Bash
Executable file

#!/bin/sh
PID=$$
test -z "$1" && { echo "Usage: js 'somefunction(1)'"; exit 0; }
source /etc/profile.sh
test -n "$BROWSER" || { alert warning "/dev/browser not active (are you running outside of v86?)"; }
javascript="$*"
# if we are run as shebang, use the file as input
test -f "$1" && {
javascript="args = String('$*').split(' '); $(cat $1 | tail +2)"
}
# 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/run/$PID # com/isoterminal/feat/javascript.js writes output here
echo -n "PID=$PID; $javascript" > /dev/browser/js
# check for file output
check_ready(){
test -f $OUTPUT && {
cat $OUTPUT | grep -vE '^undefined$';
code="$(cat ${OUTPUT}.exit)"
rm $OUTPUT;
exit $code;
}
}
check_ready
while sleep 0.3; do check_ready; done
# should we use flock, an awesome way to make processes read/write the same file
# while preventing 1001 concurrency issues?
# attempt:
#
# flock /dev/browser/js -c "echo \"$javascript\" > /dev/browser/js"