From 9ffec0b896a00fed41f0fce0c964c7ea46c1cf60 Mon Sep 17 00:00:00 2001 From: Partouf Date: Tue, 6 Nov 2018 18:15:36 +0100 Subject: [PATCH] refactor to start execute earlier and glue it on the result near the end --- lib/base-compiler.js | 51 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/base-compiler.js b/lib/base-compiler.js index cacfc0cd1..d99d4c6e8 100644 --- a/lib/base-compiler.js +++ b/lib/base-compiler.js @@ -142,17 +142,15 @@ class BaseCompiler { .then(execResult => { execResult.stdout = utils.parseOutput(execResult.stdout); execResult.stderr = utils.parseOutput(execResult.stderr); - result.execResult = execResult; - return result; + return execResult; }).catch(err => { // TODO: is this the best way? Perhaps failures in sandbox shouldn't reject // with "results", but instead should play on? - result.execResult = { + return { stdout: err.stdout ? utils.parseOutput(err.stdout) : [], stderr: err.stderr ? utils.parseOutput(err.stderr) : [], code: err.code !== undefined ? err.code : -1 }; - return result; }); } @@ -308,8 +306,7 @@ class BaseCompiler { return this.getOrBuildExecutable(key) .then(buildResult => { if (buildResult.code !== 0) { - result.execResult = buildResult; - return result; + return buildResult; } else { return this.runExecutable(buildResult.executableFilename, result, executeParameters); @@ -345,9 +342,6 @@ class BaseCompiler { return cacheGet .then((result) => { if (result) { - if (doExecute) { - return this.handleExecution(key, result, executeParameters); - } return result; } @@ -434,6 +428,8 @@ class BaseCompiler { }); }); + let executionPromise; + return compileToAsmPromise .then(results => { let optOutput; @@ -450,6 +446,14 @@ class BaseCompiler { } return result; }) + .then(result => { + if (doExecute) { + executionPromise = this.handleExecution(key, result, executeParameters); + } else { + executionPromise = Promise.resolve(false); + } + return result; + }) .then(result => { const outputFilebase = "output"; const outputFilename = this.getOutputFilename(result.dirPath, outputFilebase); @@ -484,24 +488,19 @@ class BaseCompiler { return result; }) .then(result => { - if (doExecute) { - return this.handleExecution(key, result, executeParameters) - .then((result) => { - if (result.okToCache) { - return this.env.cachePut(key, result).then(() => result); - } - else - return result; - }); - } + return Promise.all([executionPromise]).then(([executionResult]) => { + if (executionResult) { + result.execResult = executionResult; + } - if (result.okToCache) { - // TODO: asynchronously do this? Is it possible to "fire and forget" the promise? - // Do we want to do it higher up? - return this.env.cachePut(key, result).then(() => result); - } - else - return result; + if (result.okToCache) { + // TODO: asynchronously do this? Is it possible to "fire and forget" the promise? + // Do we want to do it higher up? + return this.env.cachePut(key, result).then(() => result); + } + else + return result; + }); }); }); });