aboutsummaryrefslogtreecommitdiff
path: root/modules/ext/console.js
diff options
context:
space:
mode:
Diffstat (limited to 'modules/ext/console.js')
-rw-r--r--modules/ext/console.js35
1 files changed, 31 insertions, 4 deletions
diff --git a/modules/ext/console.js b/modules/ext/console.js
index 5d3007a..60f75a7 100644
--- a/modules/ext/console.js
+++ b/modules/ext/console.js
@@ -30,13 +30,28 @@ class JSConsoleModule {
evalJS(args) {
if (!args[0]) return this.terminal.print('Usage: js <expression>', 'warning');
const expr = args.join(' ');
- // v4.5.67: Sandboxed eval — restrict access to dangerous globals, still allows math/JSON/Array etc.
+ // v4.6.128: Sandboxed eval — restrict access to dangerous globals, still allows math/JSON/Array etc.
try {
const result = (function() {
'use strict';
// eslint-disable-next-line no-eval
return eval(expr);
})();
+ // A thenable must be awaited: an uncaught `import('fs')` rejection escapes to
+ // window.onunhandledrejection and is auto-filed as a runtime bug report.
+ if (result && typeof result.then === 'function') {
+ result.then(val => {
+ const output = val === undefined ? 'undefined' :
+ typeof val === 'string' ? val :
+ val === null ? 'null' :
+ typeof val === 'object' ? JSON.stringify(val, null, 2) :
+ String(val);
+ this.terminal.print(output, 'success');
+ }).catch(e => {
+ this._printError(e);
+ });
+ return;
+ }
const output = result === undefined ? 'undefined' :
typeof result === 'string' ? result :
result === null ? 'null' :
@@ -44,7 +59,19 @@ class JSConsoleModule {
String(result);
this.terminal.print(output, 'success');
} catch (e) {
- this.terminal.print(`Error: ${e.message}`, 'error');
+ this._printError(e);
+ }
+ }
+
+ // Single error sink: prints `Error: <msg>`, plus a Node-builtin hint when the browser
+ // could not resolve a bare specifier (v4.6.128).
+ _printError(e) {
+ const msg = (e && e.message) ? e.message : String(e);
+ this.terminal.print(`Error: ${msg}`, 'error');
+ const m = /(?:Failed to resolve module specifier|Cannot find module) '([^']+)'/.exec(msg);
+ if (m) {
+ this.terminal.print(`'${m[1]}' is a Node built-in; the browser has no Node — ` +
+ `use the VFS (ls/cat/run, terminal.fs) instead`, 'warning');
}
}
@@ -73,7 +100,7 @@ class JSConsoleModule {
String(val);
this.terminal.print(output, 'success');
}).catch(e => {
- this.terminal.print(`Error: ${e.message}`, 'error');
+ this._printError(e);
});
return;
}
@@ -84,7 +111,7 @@ class JSConsoleModule {
String(result);
this.terminal.print(output, 'success');
} catch (e) {
- this.terminal.print(`Error: ${e.message}`, 'error');
+ this._printError(e);
}
}