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);
}
}
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();
}
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;
}
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;
}
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);
}
}
}
Aggregations