Debian Codegraph @

rhino/src/org/mozilla/javascript/optimizer/ClassCompiler.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.optimizer;
8
9 import org.mozilla.javascript.*;
10 import org.mozilla.javascript.ast.AstRoot;
11 import org.mozilla.javascript.ast.FunctionNode;
12 import org.mozilla.javascript.ast.ScriptNode;
13
14 /**
15 * Generates class files from script sources.
16 *
17 * since 1.5 Release 5
18 * @author Igor Bukanov
19 */
20
21 public class ClassCompiler
22 {
23 /**
24 * Construct ClassCompiler that uses the specified compiler environment
25 * when generating classes.
26 */
27 public ClassCompiler(CompilerEnvirons compilerEnv)
28 {
29 if (compilerEnv == null) throw new IllegalArgumentException();
30 this.compilerEnv = compilerEnv;
31 this.mainMethodClassName = Codegen.DEFAULT_MAIN_METHOD_CLASS;
32 }
33
34 /**
35 * Set the class name to use for main method implementation.
36 * The class must have a method matching
37 * <tt>public static void main(Script sc, String[] args)</tt>, it will be
38 * called when <tt>main(String[] args)</tt> is called in the generated
39 * class. The class name should be fully qulified name and include the
40 * package name like in <tt>org.foo.Bar<tt>.
41 */
42 public void setMainMethodClass(String className)
43 {
44 // XXX Should this check for a valid class name?
45 mainMethodClassName = className;
46 }
47
48 /**
49 * Get the name of the class for main method implementation.
50 * @see #setMainMethodClass(String)
51 */
52 public String getMainMethodClass()
53 {
54 return mainMethodClassName;
55 }
56
57 /**
58 * Get the compiler environment the compiler uses.
59 */
60 public CompilerEnvirons getCompilerEnv()
61 {
62 return compilerEnv;
63 }
64
65 /**
66 * Get the class that the generated target will extend.
67 */
68 public Class<?> getTargetExtends()
69 {
70 return targetExtends;
71 }
72
73 /**
74 * Set the class that the generated target will extend.
75 *
76 * @param extendsClass the class it extends
77 */
78 public void setTargetExtends(Class<?> extendsClass)
79 {
80 targetExtends = extendsClass;
81 }
82
83 /**
84 * Get the interfaces that the generated target will implement.
85 */
86 public Class<?>[] getTargetImplements()
87 {
88 return targetImplements == null ? null : (Class[])targetImplements.clone();
89 }
90
91 /**
92 * Set the interfaces that the generated target will implement.
93 *
94 * @param implementsClasses an array of Class objects, one for each
95 * interface the target will extend
96 */
97 public void setTargetImplements(Class<?>[] implementsClasses)
98 {
99 targetImplements = implementsClasses == null ? null : (Class[])implementsClasses.clone();
100 }
101
102 /**
103 * Build class name for a auxiliary class generated by compiler.
104 * If the compiler needs to generate extra classes beyond the main class,
105 * it will call this function to build the auxiliary class name.
106 * The default implementation simply appends auxMarker to mainClassName
107 * but this can be overridden.
108 */
109 protected String makeAuxiliaryClassName(String mainClassName,
110 String auxMarker)
111 {
112 return mainClassName+auxMarker;
113 }
114
115 /**
116 * Compile JavaScript source into one or more Java class files.
117 * The first compiled class will have name mainClassName.
118 * If the results of {@link #getTargetExtends()} or
119 * {@link #getTargetImplements()} are not null, then the first compiled
120 * class will extend the specified super class and implement
121 * specified interfaces.
122 *
123 * @return array where elements with even indexes specifies class name
124 * and the following odd index gives class file body as byte[]
125 * array. The initial element of the array always holds
126 * mainClassName and array[1] holds its byte code.
127 */
128 public Object[] compileToClassFiles(String source,
129 String sourceLocation,
130 int lineno,
131 String mainClassName)
132 {
133 Parser p = new Parser(compilerEnv);
134 AstRoot ast = p.parse(source, sourceLocation, lineno);
135 IRFactory irf = new IRFactory(compilerEnv);
136 ScriptNode tree = irf.transformTree(ast);
137
138 // release reference to original parse tree & parser
139 irf = null;
140 ast = null;
141 p = null;
142
143 Class<?> superClass = getTargetExtends();
144 Class<?>[] interfaces = getTargetImplements();
145 String scriptClassName;
146 boolean isPrimary = (interfaces == null && superClass == null);
147 if (isPrimary) {
148 scriptClassName = mainClassName;
149 } else {
150 scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
151 }
152
153 Codegen codegen = new Codegen();
154 codegen.setMainMethodClass(mainMethodClassName);
155 byte[] scriptClassBytes
156 = codegen.compileToClassFile(compilerEnv, scriptClassName,
157 tree, tree.getEncodedSource(),
158 false);
159
160 if (isPrimary) {
161 return new Object[] { scriptClassName, scriptClassBytes };
162 }
163 int functionCount = tree.getFunctionCount();
164 ObjToIntMap functionNames = new ObjToIntMap(functionCount);
165 for (int i = 0; i != functionCount; ++i) {
166 FunctionNode ofn = tree.getFunctionNode(i);
167 String name = ofn.getName();
168 if (name != null && name.length() != 0) {
169 functionNames.put(name, ofn.getParamCount());
170 }
171 }
172 if (superClass == null) {
173 superClass = ScriptRuntime.ObjectClass;
174 }
175 byte[] mainClassBytes
176 = JavaAdapter.createAdapterCode(
177 functionNames, mainClassName,
178 superClass, interfaces, scriptClassName);
179
180 return new Object[] { mainClassName, mainClassBytes,
181 scriptClassName, scriptClassBytes };
182 }
183
184 private String mainMethodClassName;
185 private CompilerEnvirons compilerEnv;
186 private Class<?> targetExtends;
187 private Class<?>[] targetImplements;
188
189 }
190