use of org.mozilla.javascript.ast.AstRoot in project hackpad by dropbox.
the class Context method compileImpl.
private Object compileImpl(Scriptable scope, Reader sourceReader, String sourceString, String sourceName, int lineno, Object securityDomain, boolean returnFunction, Evaluator compiler, ErrorReporter compilationErrorReporter) throws IOException {
if (sourceName == null) {
sourceName = "unnamed script";
}
if (securityDomain != null && getSecurityController() == null) {
throw new IllegalArgumentException("securityDomain should be null if setSecurityController() was never called");
}
// One of sourceReader or sourceString has to be null
if (!(sourceReader == null ^ sourceString == null))
Kit.codeBug();
// scope should be given if and only if compiling function
if (!(scope == null ^ returnFunction))
Kit.codeBug();
CompilerEnvirons compilerEnv = new CompilerEnvirons();
compilerEnv.initFromContext(this);
if (compilationErrorReporter == null) {
compilationErrorReporter = compilerEnv.getErrorReporter();
}
if (debugger != null) {
if (sourceReader != null) {
sourceString = Kit.readReader(sourceReader);
sourceReader = null;
}
}
Parser p = new Parser(compilerEnv, compilationErrorReporter);
if (returnFunction) {
p.calledByCompileFunction = true;
}
AstRoot ast;
if (sourceString != null) {
ast = p.parse(sourceString, sourceName, lineno);
} else {
ast = p.parse(sourceReader, sourceName, lineno);
}
if (returnFunction) {
// parser no longer adds function to script node
if (!(ast.getFirstChild() != null && ast.getFirstChild().getType() == Token.FUNCTION)) {
// with sources like function() {};;;
throw new IllegalArgumentException("compileFunction only accepts source with single JS function: " + sourceString);
}
}
IRFactory irf = new IRFactory(compilerEnv, compilationErrorReporter);
ScriptNode tree = irf.transformTree(ast);
// discard everything but the IR tree
p = null;
ast = null;
irf = null;
if (compiler == null) {
compiler = createCompiler();
}
Object bytecode = compiler.compile(compilerEnv, tree, tree.getEncodedSource(), returnFunction);
if (debugger != null) {
if (sourceString == null)
Kit.codeBug();
if (bytecode instanceof DebuggableScript) {
DebuggableScript dscript = (DebuggableScript) bytecode;
notifyDebugger_r(this, dscript, sourceString);
} else {
throw new RuntimeException("NOT SUPPORTED");
}
}
Object result;
if (returnFunction) {
result = compiler.createFunctionObject(this, scope, bytecode, securityDomain);
} else {
result = compiler.createScriptObject(bytecode, securityDomain);
}
return result;
}
use of org.mozilla.javascript.ast.AstRoot in project hackpad by dropbox.
the class ClassCompiler method compileToClassFiles.
/**
* Compile JavaScript source into one or more Java class files.
* The first compiled class will have name mainClassName.
* If the results of {@link #getTargetExtends()} or
* {@link #getTargetImplements()} are not null, then the first compiled
* class will extend the specified super class and implement
* specified interfaces.
*
* @return array where elements with even indexes specifies class name
* and the following odd index gives class file body as byte[]
* array. The initial element of the array always holds
* mainClassName and array[1] holds its byte code.
*/
public Object[] compileToClassFiles(String source, String sourceLocation, int lineno, String mainClassName) {
Parser p = new Parser(compilerEnv);
AstRoot ast = p.parse(source, sourceLocation, lineno);
IRFactory irf = new IRFactory(compilerEnv);
ScriptNode tree = irf.transformTree(ast);
// release reference to original parse tree & parser
irf = null;
ast = null;
p = null;
Class<?> superClass = getTargetExtends();
Class<?>[] interfaces = getTargetImplements();
String scriptClassName;
boolean isPrimary = (interfaces == null && superClass == null);
if (isPrimary) {
scriptClassName = mainClassName;
} else {
scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
}
Codegen codegen = new Codegen();
codegen.setMainMethodClass(mainMethodClassName);
byte[] scriptClassBytes = codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(), false);
if (isPrimary) {
return new Object[] { scriptClassName, scriptClassBytes };
}
int functionCount = tree.getFunctionCount();
ObjToIntMap functionNames = new ObjToIntMap(functionCount);
for (int i = 0; i != functionCount; ++i) {
FunctionNode ofn = tree.getFunctionNode(i);
String name = ofn.getName();
if (name != null && name.length() != 0) {
functionNames.put(name, ofn.getParamCount());
}
}
if (superClass == null) {
superClass = ScriptRuntime.ObjectClass;
}
byte[] mainClassBytes = JavaAdapter.createAdapterCode(functionNames, mainClassName, superClass, interfaces, scriptClassName);
return new Object[] { mainClassName, mainClassBytes, scriptClassName, scriptClassBytes };
}
use of org.mozilla.javascript.ast.AstRoot in project pmd by pmd.
the class EcmascriptParserTest method testLineNumbers.
/**
* https://sourceforge.net/p/pmd/bugs/1043/
*/
@Test
public void testLineNumbers() {
final String SOURCE_CODE = "function a() {" + PMD.EOL + " alert('hello');" + PMD.EOL + "}" + PMD.EOL;
EcmascriptNode<AstRoot> node = parse(SOURCE_CODE);
assertEquals(1, node.getBeginLine());
assertEquals(1, node.getBeginColumn());
assertEquals(3, node.getEndLine());
assertEquals(1, node.getEndColumn());
Node child = node.getFirstChildOfType(ASTFunctionNode.class);
assertEquals(1, child.getBeginLine());
assertEquals(1, child.getBeginColumn());
assertEquals(3, child.getEndLine());
assertEquals(1, child.getEndColumn());
child = node.getFirstDescendantOfType(ASTFunctionCall.class);
assertEquals(2, child.getBeginLine());
assertEquals(3, child.getBeginColumn());
assertEquals(2, child.getEndLine());
assertEquals(16, child.getEndColumn());
}
use of org.mozilla.javascript.ast.AstRoot in project pmd by pmd.
the class EcmascriptParserTest method testArrayMethod.
/**
* Test for bug #1136 ECAMScript: NullPointerException in getLeft() and
* getRight()
*/
@Test
public void testArrayMethod() {
EcmascriptNode<AstRoot> rootNode = parse("function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n" + " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}");
List<ASTFunctionCall> calls = rootNode.findDescendantsOfType(ASTFunctionCall.class);
List<String> results = new ArrayList<>();
for (ASTFunctionCall f : calls) {
Node node = f.getTarget();
results.add(getName(node));
}
assertEquals("[a, b.c, d[], e[].f, y.z[]]", results.toString());
}
use of org.mozilla.javascript.ast.AstRoot in project pmd by pmd.
the class EcmascriptParser method parse.
public EcmascriptNode<AstRoot> parse(final Reader reader) {
try {
final List<ParseProblem> parseProblems = new ArrayList<>();
final String sourceCode = IOUtils.toString(reader);
final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
EcmascriptNode<AstRoot> tree = treeBuilder.build(astRoot);
suppressMap = new HashMap<>();
if (astRoot.getComments() != null) {
for (Comment comment : astRoot.getComments()) {
int nopmd = comment.getValue().indexOf(suppressMarker);
if (nopmd > -1) {
String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
EcmascriptNode<Comment> node = treeBuilder.build(comment);
suppressMap.put(node.getBeginLine(), suppression);
}
}
}
return tree;
} catch (IOException e) {
throw new ParseException(e);
}
}
Aggregations