Search in sources :

Example 1 with Compiler

use of com.google.javascript.jscomp.Compiler in project Bytecoder by mirkosertic.

the class BytecoderMavenMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    File theBaseDirectory = new File(buldDirectory);
    File theBytecoderDirectory = new File(theBaseDirectory, "bytecoder");
    theBytecoderDirectory.mkdirs();
    try {
        ClassLoader theLoader = prepareClassLoader();
        Class theTargetClass = theLoader.loadClass(mainClass);
        CompileTarget theCompileTarget = new CompileTarget(theLoader, CompileTarget.BackendType.valueOf(backend));
        File theBytecoderFileName = new File(theBytecoderDirectory, theCompileTarget.generatedFileName());
        BytecodeMethodSignature theSignature = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.VOID, new BytecodeTypeRef[] { new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(String.class), 1) });
        CompileOptions theOptions = new CompileOptions(new Slf4JLogger(), debugOutput, KnownOptimizer.ALL);
        CompileResult theCode = theCompileTarget.compileToJS(theOptions, theTargetClass, "main", theSignature);
        try (PrintWriter theWriter = new PrintWriter(new FileWriter(theBytecoderFileName))) {
            theWriter.println(theCode.getData());
        }
        if (optimizeWithGoogleClosure) {
            Compiler theCompiler = new Compiler();
            CompilerOptions theClosureOptions = new CompilerOptions();
            theClosureOptions.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
            theClosureOptions.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
            CompilationLevel.valueOf(closureOptimizationLevel).setOptionsForCompilationLevel(theClosureOptions);
            List<SourceFile> theSourceFiles = CommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.BROWSER);
            theSourceFiles.add(SourceFile.fromCode("bytecoder.js", (String) theCode.getData()));
            theCompiler.compile(new ArrayList<>(), theSourceFiles, theClosureOptions);
            String theClosureCode = theCompiler.toSource();
            File theBytecoderClosureFileName = new File(theBytecoderDirectory, "bytecoder-closure.js");
            try (PrintWriter theWriter = new PrintWriter(new FileWriter(theBytecoderClosureFileName))) {
                theWriter.println(theClosureCode);
            }
        }
        if (theCode instanceof WASMCompileResult) {
            WASMCompileResult theWASMCompileResult = (WASMCompileResult) theCode;
            int[] theWASM = wat2wasm(theWASMCompileResult);
            File theBytecoderWASMFileName = new File(theBytecoderDirectory, "bytecoder.wasm");
            try (FileOutputStream theFos = new FileOutputStream(theBytecoderWASMFileName)) {
                for (int aTheWASM : theWASM) {
                    theFos.write(aTheWASM);
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error running bytecoder", e);
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) WASMCompileResult(de.mirkosertic.bytecoder.backend.wasm.WASMCompileResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) CompileOptions(de.mirkosertic.bytecoder.backend.CompileOptions) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BytecodeArrayTypeRef(de.mirkosertic.bytecoder.core.BytecodeArrayTypeRef) FileOutputStream(java.io.FileOutputStream) CompileTarget(de.mirkosertic.bytecoder.backend.CompileTarget) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) URLClassLoader(java.net.URLClassLoader) WASMCompileResult(de.mirkosertic.bytecoder.backend.wasm.WASMCompileResult) CompileResult(de.mirkosertic.bytecoder.backend.CompileResult) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) PrintWriter(java.io.PrintWriter)

Example 2 with Compiler

use of com.google.javascript.jscomp.Compiler in project divolte-collector by divolte.

the class JavaScriptResource method compile.

private static Compiler compile(final String filename, final InputStream javascript, final ImmutableMap<String, Object> scriptConstants, final boolean debugMode) throws IOException {
    final CompilerOptions options = new CompilerOptions();
    COMPILATION_LEVEL.setOptionsForCompilationLevel(options);
    COMPILATION_LEVEL.setTypeBasedOptimizationOptions(options);
    options.setEnvironment(CompilerOptions.Environment.BROWSER);
    options.setLanguageIn(ECMASCRIPT5_STRICT);
    options.setLanguageOut(ECMASCRIPT5_STRICT);
    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
    // can be related more easily to the original JavaScript source.
    if (debugMode) {
        options.setPrettyPrint(true);
        COMPILATION_LEVEL.setDebugOptionsForCompilationLevel(options);
    }
    options.setDefineReplacements(scriptConstants);
    final SourceFile source = SourceFile.fromInputStream(filename, javascript, StandardCharsets.UTF_8);
    final Compiler compiler = new Compiler();
    final ErrorManager errorManager = new Slf4jErrorManager(compiler);
    compiler.setErrorManager(errorManager);
    // TODO: Use an explicit list of externs instead of the default browser set, to control compatibility.
    final List<SourceFile> externs = CommandLineRunner.getBuiltinExterns(options.getEnvironment());
    compiler.compile(externs, ImmutableList.of(source), options);
    return compiler;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) ErrorManager(com.google.javascript.jscomp.ErrorManager) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 3 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class SourceMapTestCase method compile.

protected RunResult compile(String js1, String fileName1, String js2, String fileName2) {
    Compiler compiler = new Compiler();
    CompilerOptions options = getCompilerOptions();
    options.setChecksOnly(true);
    List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode(fileName1, js1));
    if (js2 != null && fileName2 != null) {
        inputs = ImmutableList.of(SourceFile.fromCode(fileName1, js1), SourceFile.fromCode(fileName2, js2));
    }
    Result result = compiler.compile(EXTERNS, inputs, options);
    assertTrue("compilation failed", result.success);
    String source = compiler.toSource();
    StringBuilder sb = new StringBuilder();
    try {
        result.sourceMap.validate(true);
        result.sourceMap.appendTo(sb, "testcode");
    } catch (IOException e) {
        throw new RuntimeException("unexpected exception", e);
    }
    RunResult rr = new RunResult();
    rr.generatedSource = source;
    rr.sourceMap = result.sourceMap;
    rr.sourceMapFileContent = sb.toString();
    return rr;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) Result(com.google.javascript.jscomp.Result)

Example 4 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class ApplySuggestedFixesTest method testApplySuggestedFixes_insideJSDoc.

@Test
public void testApplySuggestedFixes_insideJSDoc() throws Exception {
    String code = "/** @type {Foo} */\nvar foo = new Foo()";
    Compiler compiler = getCompiler(code);
    Node root = compileToScriptRoot(compiler);
    Node varNode = root.getFirstChild();
    Node jsdocRoot = Iterables.getOnlyElement(varNode.getJSDocInfo().getTypeNodes());
    SuggestedFix fix = new SuggestedFix.Builder().insertBefore(jsdocRoot, "!").build();
    List<SuggestedFix> fixes = ImmutableList.of(fix);
    Map<String, String> codeMap = ImmutableMap.of("test", code);
    Map<String, String> newCodeMap = ApplySuggestedFixes.applySuggestedFixesToCode(fixes, codeMap);
    assertThat(newCodeMap).hasSize(1);
    assertThat(newCodeMap).containsEntry("test", "/** @type {!Foo} */\nvar foo = new Foo()");
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 5 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class ApplySuggestedFixesTest method testApplySuggestedFixes_multipleFixesInJsdoc.

@Test
public void testApplySuggestedFixes_multipleFixesInJsdoc() throws Exception {
    String code = "/** @type {Array<Foo>} */\nvar arr = [new Foo()];";
    Compiler compiler = getCompiler(code);
    Node root = compileToScriptRoot(compiler);
    Node varNode = root.getFirstChild();
    Node jsdocRoot = Iterables.getOnlyElement(varNode.getJSDocInfo().getTypeNodes());
    SuggestedFix fix1 = new SuggestedFix.Builder().insertBefore(jsdocRoot, "!").build();
    Node foo = jsdocRoot.getFirstFirstChild();
    SuggestedFix fix2 = new SuggestedFix.Builder().insertBefore(foo, "!").build();
    List<SuggestedFix> fixes = ImmutableList.of(fix1, fix2);
    Map<String, String> codeMap = ImmutableMap.of("test", code);
    Map<String, String> newCodeMap = ApplySuggestedFixes.applySuggestedFixesToCode(fixes, codeMap);
    assertThat(newCodeMap).hasSize(1);
    assertThat(newCodeMap).containsEntry("test", "/** @type {!Array<!Foo>} */\nvar arr = [new Foo()];");
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)107 Test (org.junit.Test)83 Node (com.google.javascript.rhino.Node)80 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)15 SourceFile (com.google.javascript.jscomp.SourceFile)11 IOException (java.io.IOException)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Result (com.google.javascript.jscomp.Result)3 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSModule (com.google.javascript.jscomp.JSModule)2 JSSourceFile (com.google.javascript.jscomp.JSSourceFile)2 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)2 AbstractJSProject (net.vtst.ow.closure.compiler.deps.AbstractJSProject)2 JSProject (net.vtst.ow.closure.compiler.deps.JSProject)2 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)1