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