Skip to content

Commit 8014f22

Browse files
better async js execution
1 parent 834f5c9 commit 8014f22

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

com/isoterminal/feat/javascript.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,39 @@ if( typeof emulator != 'undefined' ){
1919

2020
}else{
2121
// inside browser-thread
22-
22+
23+
24+
/*
25+
* here we're going to execute javascript in the browser,
26+
* both a terrible and great idea depending on a corporate vs personal computing angle.
27+
*
28+
* if a function-string gets evaluated, and it returns a promise..then we assume async.
29+
*/
2330
ISOTerminal.addEventListener('javascript-eval', async function(e){
2431
const {script,PID} = e.detail
2532
let res;
33+
let error = false;
34+
35+
const output = (res,PID) => {
36+
if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2)
37+
// update output to 9p with PID as filename (in /mnt/run)
38+
if( PID ){
39+
this.worker.update_file(`run/${PID}.exit`, error ? "1" : "0")
40+
this.worker.update_file(`run/${PID}`, this.convert.toUint8Array(res) )
41+
}
42+
}
2643

2744
try{
2845
let f = new Function(`${script}`);
2946
res = f();
30-
if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2)
47+
if( res && typeof res.then == 'function' ){ // if we got a promise
48+
res.then( (res) => output(res,PID) )
49+
res.catch( (e) => output(e,PID) )
50+
}else{ // normal sync function
51+
output(res,PID)
52+
}
3153
}catch(err){
54+
error = true
3255
console.error(err)
3356
console.dir(err)
3457
res = "error: "+err.toString()
@@ -41,10 +64,7 @@ if( typeof emulator != 'undefined' ){
4164
res += script.split("\n")[lnr-1]
4265
}else console.dir(script)
4366
console.error(res)
44-
}
45-
// update output to 9p with PID as filename (in /mnt/run)
46-
if( PID ){
47-
this.worker.update_file(`run/${PID}`, this.convert.toUint8Array(res) )
67+
output(res,PID)
4868
}
4969
})
5070

com/isoterminal/feat/term.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ISOTerminal.prototype.TermInit = function(){
3434
Term.prototype.keyDownHandler = function(original){
3535
return function (e){
3636
if ((e.ctrlKey || e.metaKey) && e.key === 'v') {
37+
debugger
3738
return true; // bubble up to pasteHandler (see pastedrop.js)
3839
}
3940
original.apply(this,[e])

com/isoterminal/term.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@ Term.prototype.blurHandler = function (ev)
12591259

12601260
Term.prototype.pasteHandler = function (ev)
12611261
{
1262+
debugger
12621263
var c, str;
12631264
if (!this.textarea_has_focus) {
12641265
c = ev.clipboardData;

0 commit comments

Comments
 (0)