From 8ebe55f45735c03b8a7f8dfd946574b556d3efe6 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 22 Aug 2018 11:32:15 -0400 Subject: [PATCH 1/4] simple implementation of await --- lib/core/modules/coderunner/codeRunner.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index c34af4125..95bc5b40f 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -41,6 +41,10 @@ class CodeRunner { if (!cb) { cb = function() {}; } + if (code.startsWith('await')) { + code = code.substring(5); // remove await keyword + code += '.then(console.log).catch(console.error)'; // Add promise catch + } let result = RunCode.doEval(code); if (forConsoleOnly && self.ipc.isServer()) { self.commands.push({code}); From a0b06c61979bf0ff06b575ae769f756bb4d2eb20 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 22 Aug 2018 14:09:10 -0400 Subject: [PATCH 2/4] make it work with variables too --- lib/core/modules/coderunner/codeRunner.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 95bc5b40f..9b965e695 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -41,9 +41,19 @@ class CodeRunner { if (!cb) { cb = function() {}; } - if (code.startsWith('await')) { - code = code.substring(5); // remove await keyword - code += '.then(console.log).catch(console.error)'; // Add promise catch + const awaitIdx = code.indexOf('await'); + if (awaitIdx > -1) { + let end = code.length; + if (code[end - 1] === ';') { + end--; // Remove the `;` because we add function calls + } + if (awaitIdx < 2) { + code = code.substring(5, end); // remove await keyword + code += '.then(console.log).catch(console.error);'; // Add promise catch + } else { + code = code.substring(0, end); // remove ending `;` + code = `(async function() {${code}.then(console.log);})();`; + } } let result = RunCode.doEval(code); if (forConsoleOnly && self.ipc.isServer()) { From 326e12d23aedcd3517dfc6081e6ddcaf4828b009 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 22 Aug 2018 14:23:23 -0400 Subject: [PATCH 3/4] fix one with variable --- lib/core/modules/coderunner/codeRunner.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 9b965e695..14484c86b 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -43,16 +43,15 @@ class CodeRunner { } const awaitIdx = code.indexOf('await'); if (awaitIdx > -1) { - let end = code.length; - if (code[end - 1] === ';') { - end--; // Remove the `;` because we add function calls - } if (awaitIdx < 2) { + let end = code.length; + if (code[end - 1] === ';') { + end--; // Remove the `;` because we add function calls + } code = code.substring(5, end); // remove await keyword code += '.then(console.log).catch(console.error);'; // Add promise catch } else { - code = code.substring(0, end); // remove ending `;` - code = `(async function() {${code}.then(console.log);})();`; + code = `(async function() {${code}})();`; } } let result = RunCode.doEval(code); From c6abafff0542ad4bb20a739ad949558aa1a7eabb Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 23 Aug 2018 13:38:58 -0400 Subject: [PATCH 4/4] fix printing --- lib/core/modules/coderunner/codeRunner.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 14484c86b..fc308beac 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -49,12 +49,14 @@ class CodeRunner { end--; // Remove the `;` because we add function calls } code = code.substring(5, end); // remove await keyword - code += '.then(console.log).catch(console.error);'; // Add promise catch } else { code = `(async function() {${code}})();`; } } let result = RunCode.doEval(code); + if (result instanceof Promise) { + return result.then((value) => cb(null, value)).catch(cb); + } if (forConsoleOnly && self.ipc.isServer()) { self.commands.push({code}); self.ipc.broadcast("runcode:newCommand", {code});