From ccf57f469b4a28deb723d51cf3d5ec8d8bd79f2a Mon Sep 17 00:00:00 2001 From: rluble Date: Tue, 16 May 2017 12:37:57 -0700 Subject: [PATCH] Implement --save_after_checks and --continue_saved_compilation. Some compiler options are not supported yet in this mode due to missing serialization of some of the compiler state: - NTI. - inline variables. - coalesce variable names. - disambiguate properties. - dead assignment elimination ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=156216287 --- .../jscomp/AbstractCommandLineRunner.java | 56 +++++++++++- .../google/javascript/jscomp/Compiler.java | 91 ++++++++++++------- .../javascript/jscomp/CompilerTest.java | 1 + 3 files changed, 116 insertions(+), 32 deletions(-) diff --git a/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java b/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java index 5c111a12a98..4022df27904 100644 --- a/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java +++ b/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java @@ -47,6 +47,7 @@ import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.Flushable; import java.io.IOException; @@ -116,6 +117,14 @@ public abstract class AbstractCommandLineRunner files) { * Serializable state of the compiler. */ private static class CompilerState implements Serializable { - CompilerOptions options; + + Node externAndJsRoot; Node externsRoot; Node jsRoot; - Node externAndJsRoot; List externs; List inputs; Map inputsById; JSTypeRegistry typeRegistry; - - CompilerState( - CompilerOptions options, - Node externsRoot, - Node jsRoot, - Node externAndJsRoot, - List externs, - List inputs, - Map inputsById, - JSTypeRegistry typeRegistry) { - this.options = options; - this.externsRoot = externsRoot; - this.jsRoot = jsRoot; - this.externAndJsRoot = externAndJsRoot; - this.typeRegistry = typeRegistry; - this.externs = externs; - this.inputs = inputs; - this.inputsById = inputsById; + MostRecentTypechecker mostRecentTypeChecker; + CompilerInput synthesizedExternsInput; + CompilerInput synthesizedExternsInputAtEnd; + Map injectedLibraries; + Node lastInjectedLibrary; + GlobalVarReferenceMap globalRefMap; + GlobalTypeInfo symbolTable; + private final boolean hasRegExpGlobalReferences; + + CompilerState(Compiler compiler) { + this.externsRoot = checkNotNull(compiler.externsRoot); + this.jsRoot = checkNotNull(compiler.jsRoot); + this.externAndJsRoot = checkNotNull(compiler.externAndJsRoot); + this.typeRegistry = compiler.typeRegistry; + this.externs = compiler.externs; + this.inputs = checkNotNull(compiler.inputs); + this.inputsById = checkNotNull(compiler.inputsById); + this.mostRecentTypeChecker = compiler.mostRecentTypechecker; + this.synthesizedExternsInput = compiler.synthesizedExternsInput; + this.synthesizedExternsInputAtEnd = compiler.synthesizedExternsInputAtEnd; + this.injectedLibraries = compiler.injectedLibraries; + this.globalRefMap = compiler.globalRefMap; + this.lastInjectedLibrary = compiler.lastInjectedLibrary; + this.symbolTable = compiler.symbolTable; + this.hasRegExpGlobalReferences = compiler.hasRegExpGlobalReferences; } } @GwtIncompatible("ObjectOutputStream") public void saveState(OutputStream outputStream) throws IOException { - CompilerState compilerState = new CompilerState( - options, externsRoot, jsRoot, externAndJsRoot, externs, inputs, inputsById, typeRegistry); try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) { - objectOutputStream.writeObject(compilerState); + runInCompilerThread(new Callable() { + @Override + public Void call() throws Exception { + objectOutputStream.writeObject(new CompilerState(Compiler.this)); + return null; + } + }); } } @GwtIncompatible("ObjectInputStream") public void restoreState(InputStream inputStream) throws Exception { - try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) { - CompilerState compilerState = (CompilerState) objectInputStream.readObject(); - options = compilerState.options; + try (final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) { + CompilerState compilerState = runInCompilerThread(new Callable() { + @Override + public CompilerState call() throws Exception { + return (CompilerState) objectInputStream.readObject(); + } + }); externs = compilerState.externs; inputs = compilerState.inputs; inputsById.clear(); @@ -3352,7 +3371,17 @@ public void restoreState(InputStream inputStream) throws Exception { externAndJsRoot = compilerState.externAndJsRoot; externsRoot = compilerState.externsRoot; jsRoot = compilerState.jsRoot; + mostRecentTypechecker = compilerState.mostRecentTypeChecker; + synthesizedExternsInput = compilerState.synthesizedExternsInput; + synthesizedExternsInputAtEnd = compilerState.synthesizedExternsInputAtEnd; + injectedLibraries.clear(); + injectedLibraries.putAll(compilerState.injectedLibraries); + lastInjectedLibrary = compilerState.lastInjectedLibrary; + globalRefMap = compilerState.globalRefMap; + symbolTable = compilerState.symbolTable; + hasRegExpGlobalReferences = compilerState.hasRegExpGlobalReferences; } initWarningsGuard(options.getWarningsGuard()); + maybeSetTracker(); } } diff --git a/test/com/google/javascript/jscomp/CompilerTest.java b/test/com/google/javascript/jscomp/CompilerTest.java index 827618d3864..5a34edd9d6f 100644 --- a/test/com/google/javascript/jscomp/CompilerTest.java +++ b/test/com/google/javascript/jscomp/CompilerTest.java @@ -972,6 +972,7 @@ public void testCheckSaveRestoreOptimize() throws Exception { compiler.saveState(byteArrayOutputStream); compiler = new Compiler(new TestErrorManager()); + compiler.options = options; ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); compiler.restoreState(byteArrayInputStream);