*
* @return The compiled version of the code.
*/
private String slim(String name, String code, boolean isLib, CompilationLevel compLevel)
{
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
if (compLevel != null) {
// Advanced mode is used here, but additional options could be set, too.
compLevel.setOptionsForCompilationLevel(options);
}
// To get the complete set of externs, the logic in
// CompilerRunner.getDefaultExterns() should be used here.
JSSourceFile extern[] = {JSSourceFile.fromCode("externs.js", "")};
// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
JSSourceFile input[] = {JSSourceFile.fromCode(name, code)};
if (m_formattingOptions != null) {
m_formattingOptions.applyToOptions(options);
}
compiler.init(extern, input, options);
compiler.parse();
m_errMgr = compiler.getErrorManager();
if (m_errMgr.getErrorCount() > 0) {
/*
Then there were errors parsing the file and we can't
prune anything.
*/
return "";
}
Node node = compiler.getRoot();
if (m_printTree) {
System.out.println("Tree before pruning:");
System.out.println(node.toStringTree());
}
//System.out.println("node before change: " + compiler.toSource());
LOGGER.log(Level.INFO, "starting process...");
Node n = process(node, isLib);
LOGGER.log(Level.INFO, "Done processing...");
LOGGER.log(Level.FINE, "m_calls: " + m_calls);
m_funcCount = m_libFuncs.size();
if (isLib) {
LOGGER.log(Level.INFO, "Starting pruneTree phase 1.");
pruneTree();
LOGGER.log(Level.INFO, "Starting pruneTree phase 2.");
pruneTree();
}
if (m_funcCount > 0) {
System.out.println("Removed " + (m_funcCount - m_keepers.size()) + " out of " + m_funcCount + " named functions.");
}
if (m_printTree) {
System.out.println("Tree after pruning:");
System.out.println(node.toStringTree());
}
// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
return compiler.toSource();
}