/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1997-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Norris Boyd
* Kemal Bayram
* Igor Bukanov
* Bob Jervis
* Roger Lawrence
* Andi Vajda
* Hannes Wallnoefer
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which
* case the provisions of the GPL are applicable instead of those above. If
* you wish to allow use of your version of this file only under the terms of
* the GPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replacing
* them with the notice and other provisions required by the GPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the GPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.javascript.optimizer;
import org.mozilla.javascript.*;
import org.mozilla.classfile.*;
import java.util.*;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
/**
* This class generates code for a given IR tree.
*
* @author Norris Boyd
* @author Roger Lawrence
*/
public class Codegen implements Evaluator
{
public void captureStackInfo(RhinoException ex) {
throw new UnsupportedOperationException();
}
public String getSourcePositionFromStack(Context cx, int[] linep) {
throw new UnsupportedOperationException();
}
public String getPatchedStack(RhinoException ex, String nativeStackTrace) {
throw new UnsupportedOperationException();
}
public List getScriptStack(RhinoException ex) {
throw new UnsupportedOperationException();
}
public void setEvalScriptFlag(Script script) {
throw new UnsupportedOperationException();
}
public Object compile(CompilerEnvirons compilerEnv,
ScriptOrFnNode tree,
String encodedSource,
boolean returnFunction)
{
int serial;
synchronized (globalLock) {
serial = ++globalSerialClassCounter;
}
String mainClassName = "org.mozilla.javascript.gen.c"+serial;
byte[] mainClassBytes = compileToClassFile(compilerEnv, mainClassName,
tree, encodedSource,
returnFunction);
return new Object[] { mainClassName, mainClassBytes };
}
public Script createScriptObject(Object bytecode,
Object staticSecurityDomain)
{
Class cl = defineClass(bytecode, staticSecurityDomain);
Script script;
try {
script = (Script)cl.newInstance();
} catch (Exception ex) {
throw new RuntimeException
("Unable to instantiate compiled class:"+ex.toString());
}
return script;
}
public Function createFunctionObject(Context cx, Scriptable scope,
Object bytecode,
Object staticSecurityDomain)
{
Class cl = defineClass(bytecode, staticSecurityDomain);
NativeFunction f;
try {
Constructor ctor = cl.getConstructors()[0];
Object[] initArgs = { scope, cx, new Integer(0) };
f = (NativeFunction)ctor.newInstance(initArgs);
} catch (Exception ex) {
throw new RuntimeException
("Unable to instantiate compiled class:"+ex.toString());
}
return f;
}
private Class defineClass(Object bytecode,
Object staticSecurityDomain)
{
Object[] nameBytesPair = (Object[])bytecode;
String className = (String)nameBytesPair[0];
byte[] classBytes = (byte[])nameBytesPair[1];
// The generated classes in this case refer only to Rhino classes
// which must be accessible through this class loader
ClassLoader rhinoLoader = getClass().getClassLoader();
GeneratedClassLoader loader;
loader = SecurityController.createLoader(rhinoLoader,
staticSecurityDomain);
Exception e;
try {
Class cl = loader.defineClass(className, classBytes);
loader.linkClass(cl);
return cl;
} catch (SecurityException x) {
e = x;
} catch (IllegalArgumentException x) {
e = x;
}
throw new RuntimeException("Malformed optimizer package " + e);
}
byte[] compileToClassFile(CompilerEnvirons compilerEnv,
String mainClassName,
ScriptOrFnNode scriptOrFn,
String encodedSource,
boolean returnFunction)
{
this.compilerEnv = compilerEnv;
transform(scriptOrFn);
if (Token.printTrees) {
System.out.println(scriptOrFn.toStringTree(scriptOrFn));
}
if (returnFunction) {
scriptOrFn = scriptOrFn.getFunctionNode(0);
}
initScriptOrFnNodesData(scriptOrFn);
this.mainClassName = mainClassName;
this.mainClassSignature
= ClassFileWriter.classNameToSignature(mainClassName);
try {
return generateCode(encodedSource);
} catch (ClassFileWriter.ClassFileFormatException e) {
throw reportClassFileFormatException(scriptOrFn, e.getMessage());
}
}
private RuntimeException reportClassFileFormatException(
ScriptOrFnNode scriptOrFn,
String message)
{
String msg = scriptOrFn instanceof FunctionNode
? ScriptRuntime.getMessage2("msg.while.compiling.fn",
((FunctionNode)scriptOrFn).getFunctionName(), message)
: ScriptRuntime.getMessage1("msg.while.compiling.script", message);
return Context.reportRuntimeError(msg, scriptOrFn.getSourceName(),
scriptOrFn.getLineno(), null, 0);
}
private void transform(ScriptOrFnNode tree)
{
initOptFunctions_r(tree);
int optLevel = compilerEnv.getOptimizationLevel();
Hashtable possibleDirectCalls = null;
if (optLevel > 0) {
/*
* Collect all of the contained functions into a hashtable
* so that the call optimizer can access the class name & parameter
* count for any call it encounters
*/
if (tree.getType() == Token.SCRIPT) {
int functionCount = tree.getFunctionCount();
for (int i = 0; i != functionCount; ++i) {
OptFunctionNode ofn = OptFunctionNode.get(tree, i);
if (ofn.fnode.getFunctionType()
== FunctionNode.FUNCTION_STATEMENT)
{
String name = ofn.fnode.getFunctionName();
if (name.length() != 0) {
if (possibleDirectCalls == null) {
possibleDirectCalls
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
rhino1_7R1 (265个子文件)
build-date 40B
stylesheet.css 1KB
inherit.gif 57B
Context.html 162KB
index-all.html 161KB
ScriptableObject.html 123KB
FunctionObject.html 47KB
ContextFactory.html 44KB
serialized-form.html 41KB
ImporterTopLevel.html 36KB
Scriptable.html 33KB
SecurityController.html 25KB
CompilerEnvirons.html 23KB
deprecated-list.html 22KB
constant-values.html 21KB
RhinoException.html 20KB
EcmaError.html 19KB
ScriptableOutputStream.html 19KB
EvaluatorException.html 18KB
WrapFactory.html 18KB
ClassCompiler.html 18KB
ClassCache.html 17KB
JavaScriptException.html 17KB
Synchronizer.html 16KB
DebuggableScript.html 16KB
Function.html 16KB
ScriptableInputStream.html 16KB
WrappedException.html 15KB
package-summary.html 14KB
ErrorReporter.html 12KB
overview-tree.html 12KB
package-tree.html 11KB
RefCallable.html 10KB
ClassShutter.html 10KB
ContextFactory.Listener.html 10KB
Callable.html 9KB
Script.html 9KB
GeneratedClassLoader.html 9KB
help-doc.html 9KB
ContextAction.html 8KB
Wrapper.html 7KB
package-tree.html 6KB
package-summary.html 6KB
package-summary.html 6KB
package-summary.html 6KB
package-tree.html 6KB
package-tree.html 6KB
overview-summary.html 6KB
allclasses-frame.html 5KB
allclasses-noframe.html 4KB
package-frame.html 4KB
NervousText.html 2KB
results.html 2KB
overview-frame.html 1KB
index.html 1KB
package-frame.html 1KB
package-frame.html 933B
package-frame.html 929B
js-14.jar 963KB
js.jar 739KB
Codegen.java 187KB
Interpreter.java 163KB
ScriptRuntime.java 130KB
ClassFileWriter.java 107KB
SwingGui.java 106KB
NativeRegExp.java 100KB
Context.java 89KB
ScriptableObject.java 89KB
Parser.java 83KB
XML.java 77KB
NativeArray.java 62KB
NativeDate.java 55KB
IRFactory.java 53KB
TokenStream.java 49KB
Dim.java 47KB
DToA.java 46KB
Node.java 43KB
JavaAdapter.java 43KB
XMLList.java 37KB
NativeString.java 36KB
JavaMembers.java 36KB
Global.java 35KB
NativeJavaObject.java 33KB
XMLObjectImpl.java 31KB
XmlNode.java 30KB
JsDriver.java 29KB
Decompiler.java 28KB
NativeGlobal.java 27KB
XMLObjectImpl.java 27KB
IdScriptableObject.java 24KB
NodeTransformer.java 23KB
XMLLibImpl.java 23KB
XMLList.java 22KB
XML.java 22KB
NativeJavaMethod.java 22KB
Main.java 22KB
Block.java 22KB
FunctionObject.java 21KB
XMLLibImpl.java 21KB
UintMap.java 21KB
共 265 条
- 1
- 2
- 3
xieyaa
- 粉丝: 13
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0