Search in sources :

Example 1 with SourceFile

use of com.google.javascript.jscomp.SourceFile 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 SourceFile

use of com.google.javascript.jscomp.SourceFile in project JSCover by tntim96.

the class SourceProcessor method instrumentSource.

protected String instrumentSource(String sourceURI, String source) {
    SourceFile sourceFile = SourceFile.fromCode(sourceURI, source);
    // com.google.javascript.jscomp.parsing.parser.SourceFile sf = new com.google.javascript.jscomp.parsing.parser.SourceFile(sourceURI, source);
    // LineNumberTable lineNumberTable = new LineNumberTable(sf);
    ParserRunner.ParseResult parsed = parse(source, sourceFile);
    Node jsRoot = parsed.ast;
    // System.out.println("jsRoot.toStringTree():\n" + jsRoot.toStringTree());
    commentsHandler.processComments(parsed.comments);
    NodeWalker nodeWalker = new NodeWalker();
    nodeWalker.visit(jsRoot, instrumenter);
    // System.out.println("jsRoot.toStringTree():\n" + jsRoot.toStringTree());
    if (includeBranchCoverage) {
        instrumentBranch(jsRoot, nodeWalker, branchInstrumentor);
    }
    return new CodePrinter.Builder(jsRoot).setCompilerOptions(options).build();
}
Also used : ParserRunner(com.google.javascript.jscomp.parsing.ParserRunner) Node(com.google.javascript.rhino.Node) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 3 with SourceFile

use of com.google.javascript.jscomp.SourceFile in project structr by structr.

the class MinifiedJavaScriptFile method getSourceFileList.

static ArrayList<SourceFile> getSourceFileList(final MinifiedJavaScriptFile thisFile) throws FrameworkException, IOException {
    final Class<Relation> type = StructrApp.getConfiguration().getRelationshipEntityClass("AbstractMinifiedFileMINIFICATIONFile");
    final PropertyKey<Integer> key = StructrApp.key(type, "position");
    final ArrayList<SourceFile> sourceList = new ArrayList();
    int cnt = 0;
    for (Relation rel : AbstractMinifiedFile.getSortedRelationships(thisFile)) {
        final File src = (File) rel.getTargetNode();
        sourceList.add(SourceFile.fromCode(src.getProperty(File.name), FileUtils.readFileToString(src.getFileOnDisk(), "utf-8")));
        // compact the relationships (if necessary)
        if (rel.getProperty(key) != cnt) {
            rel.setProperty(key, cnt);
        }
        cnt++;
    }
    return sourceList;
}
Also used : Relation(org.structr.core.entity.Relation) ArrayList(java.util.ArrayList) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 4 with SourceFile

use of com.google.javascript.jscomp.SourceFile 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 5 with SourceFile

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

the class CheckMissingSemicolon method checkSemicolon.

private void checkSemicolon(NodeTraversal t, Node n) {
    StaticSourceFile staticSourceFile = n.getStaticSourceFile();
    if (staticSourceFile instanceof SourceFile) {
        SourceFile sourceFile = (SourceFile) staticSourceFile;
        String code;
        try {
            code = sourceFile.getCode();
        } catch (IOException e) {
            // We can't read the original source file. Just skip this check.
            return;
        }
        int length = n.getLength();
        if (length == 0) {
            // that information, so just skip the check.
            return;
        }
        int position = n.getSourceOffset() + length - 1;
        boolean endsWithSemicolon = code.charAt(position) == ';';
        if (!endsWithSemicolon) {
            t.report(n, MISSING_SEMICOLON);
        }
    }
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Aggregations

SourceFile (com.google.javascript.jscomp.SourceFile)33 Compiler (com.google.javascript.jscomp.Compiler)10 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Result (com.google.javascript.jscomp.Result)3 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)3 EntryPoint (com.google.gwt.core.client.EntryPoint)2 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)2 SourceMapInput (com.google.javascript.jscomp.SourceMapInput)2 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)2 Node (com.google.javascript.rhino.Node)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JSExtern (net.vtst.ow.closure.compiler.deps.JSExtern)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 Config (com.google.javascript.jscomp.parsing.Config)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1