better async js execution
This commit is contained in:
parent
834f5c9b22
commit
8014f223a4
3 changed files with 28 additions and 6 deletions
|
|
@ -20,15 +20,38 @@ if( typeof emulator != 'undefined' ){
|
||||||
}else{
|
}else{
|
||||||
// inside browser-thread
|
// inside browser-thread
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* here we're going to execute javascript in the browser,
|
||||||
|
* both a terrible and great idea depending on a corporate vs personal computing angle.
|
||||||
|
*
|
||||||
|
* if a function-string gets evaluated, and it returns a promise..then we assume async.
|
||||||
|
*/
|
||||||
ISOTerminal.addEventListener('javascript-eval', async function(e){
|
ISOTerminal.addEventListener('javascript-eval', async function(e){
|
||||||
const {script,PID} = e.detail
|
const {script,PID} = e.detail
|
||||||
let res;
|
let res;
|
||||||
|
let error = false;
|
||||||
|
|
||||||
|
const output = (res,PID) => {
|
||||||
|
if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2)
|
||||||
|
// update output to 9p with PID as filename (in /mnt/run)
|
||||||
|
if( PID ){
|
||||||
|
this.worker.update_file(`run/${PID}.exit`, error ? "1" : "0")
|
||||||
|
this.worker.update_file(`run/${PID}`, this.convert.toUint8Array(res) )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
let f = new Function(`${script}`);
|
let f = new Function(`${script}`);
|
||||||
res = f();
|
res = f();
|
||||||
if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2)
|
if( res && typeof res.then == 'function' ){ // if we got a promise
|
||||||
|
res.then( (res) => output(res,PID) )
|
||||||
|
res.catch( (e) => output(e,PID) )
|
||||||
|
}else{ // normal sync function
|
||||||
|
output(res,PID)
|
||||||
|
}
|
||||||
}catch(err){
|
}catch(err){
|
||||||
|
error = true
|
||||||
console.error(err)
|
console.error(err)
|
||||||
console.dir(err)
|
console.dir(err)
|
||||||
res = "error: "+err.toString()
|
res = "error: "+err.toString()
|
||||||
|
|
@ -41,10 +64,7 @@ if( typeof emulator != 'undefined' ){
|
||||||
res += script.split("\n")[lnr-1]
|
res += script.split("\n")[lnr-1]
|
||||||
}else console.dir(script)
|
}else console.dir(script)
|
||||||
console.error(res)
|
console.error(res)
|
||||||
}
|
output(res,PID)
|
||||||
// update output to 9p with PID as filename (in /mnt/run)
|
|
||||||
if( PID ){
|
|
||||||
this.worker.update_file(`run/${PID}`, this.convert.toUint8Array(res) )
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ ISOTerminal.prototype.TermInit = function(){
|
||||||
Term.prototype.keyDownHandler = function(original){
|
Term.prototype.keyDownHandler = function(original){
|
||||||
return function (e){
|
return function (e){
|
||||||
if ((e.ctrlKey || e.metaKey) && e.key === 'v') {
|
if ((e.ctrlKey || e.metaKey) && e.key === 'v') {
|
||||||
|
debugger
|
||||||
return true; // bubble up to pasteHandler (see pastedrop.js)
|
return true; // bubble up to pasteHandler (see pastedrop.js)
|
||||||
}
|
}
|
||||||
original.apply(this,[e])
|
original.apply(this,[e])
|
||||||
|
|
|
||||||
|
|
@ -1259,6 +1259,7 @@ Term.prototype.blurHandler = function (ev)
|
||||||
|
|
||||||
Term.prototype.pasteHandler = function (ev)
|
Term.prototype.pasteHandler = function (ev)
|
||||||
{
|
{
|
||||||
|
debugger
|
||||||
var c, str;
|
var c, str;
|
||||||
if (!this.textarea_has_focus) {
|
if (!this.textarea_has_focus) {
|
||||||
c = ev.clipboardData;
|
c = ev.clipboardData;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue