Skip to content

Commit

Permalink
Continue refactoring of compilation.
Browse files Browse the repository at this point in the history
Remove public access to some methods that shouldn't have it.
Refactor logic inside checkAndTranspileAndOptimize().
Next step expose public methods that invoke parts of
checkAndTranspileAndOptimize() in a compiler thread as it does.
Rewrite AbstractCommandLineRunner code to use those.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154799837
  • Loading branch information
brad4d committed May 2, 2017
1 parent 0b501ae commit 291a5b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
46 changes: 22 additions & 24 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -841,20 +841,25 @@ public <T extends SourceFile> Result compileModules(
public Result checkAndTranspileAndOptimize() { public Result checkAndTranspileAndOptimize() {
checkState( checkState(
inputs != null && !inputs.isEmpty(), "No inputs. Did you call init() or initModules()?"); inputs != null && !inputs.isEmpty(), "No inputs. Did you call init() or initModules()?");
return runInCompilerThread(new Callable<Result>() { return runInCompilerThread(
@Override new Callable<Result>() {
public Result call() throws Exception { @Override
parseForCompilation(); public Result call() throws Exception {
if (!hasErrors()) { parseForCompilation();
if (options.getInstrumentForCoverageOnly()) { if (!hasErrors()) {
instrumentForCoverage(options.instrumentBranchCoverage); if (options.getInstrumentForCoverageOnly()) {
} else { instrumentForCoverage(options.instrumentBranchCoverage);
compileInternal(); } else {
performChecksAndTranspilation();
if (!hasErrors() && options.shouldOptimize()) {
performOptimizations();
}
}
}
completeCompilation();
return getResult();
} }
} });
return getResult();
}
});
} }


/** /**
Expand Down Expand Up @@ -882,7 +887,7 @@ <T> T runInCompilerThread(Callable<T> callable) {
callable, options != null && options.getTracerMode().isOn()); callable, options != null && options.getTracerMode().isOn());
} }


private void compileInternal() { private void performChecksAndTranspilation() {
if (options.skipNonTranspilationPasses) { if (options.skipNonTranspilationPasses) {
// i.e. whitespace-only mode, which will not work with goog.module without: // i.e. whitespace-only mode, which will not work with goog.module without:
whitespaceOnlyPasses(); whitespaceOnlyPasses();
Expand All @@ -891,15 +896,7 @@ private void compileInternal() {
} }
} else { } else {
check(); // check() also includes transpilation check(); // check() also includes transpilation
if (hasErrors()) {
return;
}

if (!options.checksOnly && !options.shouldGenerateTypedExterns()) {
optimize();
}
} }
completeCompilation();
} }


/** /**
Expand Down Expand Up @@ -1025,7 +1022,7 @@ private PhaseOptimizer createPhaseOptimizer() {
return phaseOptimizer; return phaseOptimizer;
} }


public void check() { void check() {
runCustomPasses(CustomPassExecutionTime.BEFORE_CHECKS); runCustomPasses(CustomPassExecutionTime.BEFORE_CHECKS);


// We are currently only interested in check-passes for progress reporting // We are currently only interested in check-passes for progress reporting
Expand Down Expand Up @@ -2391,7 +2388,8 @@ boolean addLicense(String license) {
// Optimizations // Optimizations
//------------------------------------------------------------------------ //------------------------------------------------------------------------


public void optimize() { void performOptimizations() {
checkState(options.shouldOptimize());
List<PassFactory> optimizations = getPassConfig().getOptimizations(); List<PassFactory> optimizations = getPassConfig().getOptimizations();
if (optimizations.isEmpty()) { if (optimizations.isEmpty()) {
return; return;
Expand Down
7 changes: 7 additions & 0 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -3355,4 +3355,11 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
outputCharset = Charset.forName(outputCharsetName); outputCharset = Charset.forName(outputCharsetName);
} }
} }

boolean shouldOptimize() {
return !skipNonTranspilationPasses
&& !checksOnly
&& !shouldGenerateTypedExterns()
&& !instrumentForCoverageOnly;
}
} }
2 changes: 1 addition & 1 deletion test/com/google/javascript/jscomp/CompilerTest.java
Expand Up @@ -975,7 +975,7 @@ public void testCheckSaveRestoreOptimize() throws Exception {
new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
compiler.restoreState(byteArrayInputStream); compiler.restoreState(byteArrayInputStream);


compiler.optimize(); compiler.performOptimizations();
String source = compiler.toSource(); String source = compiler.toSource();
assertEquals("'use strict';console.log(2);", source); assertEquals("'use strict';console.log(2);", source);


Expand Down

0 comments on commit 291a5b0

Please sign in to comment.