/* ***** 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;
/**
* This class generates code for a given IR tree.
*
* @author Norris Boyd
* @author Roger Lawrence
*/
public class Codegen extends Interpreter
{
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;
mainClassSignature
= ClassFileWriter.classNameToSignature(mainClassName);
return generateCode(encodedSource);
}
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 = new Hashtable();
}
possibleDirectCalls.put(name, ofn);
}
}
}
}
}
if (possibleDirectCalls != null) {
directCallTargets = new ObjArray();
}
OptTransformer ot = new OptTransformer(possibleDirectCalls,
directCallTargets);
ot.transform(tree);
if (optLevel > 0) {
(new Optimizer()).optimize(tree);
}
}
private static void initOptFunctions_r(ScriptOrFnNode scriptOrFn)
{
for (int i = 0, N = scriptOrFn.getFunctionCount(); i != N; ++i) {
FunctionNode fn = scriptOrFn.getFunctionNode(i);
new OptFunctionNode(fn);
initOptFunctions_r(fn);
}
}
private void initScriptOrFnNodesData(ScriptOrFnNode scriptOrFn)
{
ObjArray x = new ObjArray();
collectScriptOrFnNodes_r(scriptOrFn, x);
int count = x.size();
scriptOrFnNodes = new ScriptOrFnNode[count];
x.toArray(scriptOrFnNodes);
scriptOrFnIndexes = new ObjToIntMap(count);
for (int i = 0; i != count; ++i) {
scriptOrFnIndexes.put(scriptOrFnNodes[i], i);
}
}
private
没有合适的资源?快使用搜索试试~ 我知道了~
FF的js引擎rhino源码,java编写
共258个文件
java:175个
html:55个
js:8个
5星 · 超过95%的资源 需积分: 9 81 下载量 43 浏览量
2009-02-09
11:20:10
上传
评论 1
收藏 1.67MB ZIP 举报
温馨提示
FF的js引擎rhino源码,java编写
资源推荐
资源详情
资源评论
收起资源包目录
FF的js引擎rhino源码,java编写 (258个子文件)
build-date 42B
stylesheet.css 1KB
inherit.gif 57B
Context.html 157KB
index-all.html 157KB
ScriptableObject.html 121KB
FunctionObject.html 46KB
ContextFactory.html 41KB
serialized-form.html 38KB
ImporterTopLevel.html 36KB
Scriptable.html 33KB
SecurityController.html 24KB
CompilerEnvirons.html 21KB
constant-values.html 20KB
RhinoException.html 20KB
EcmaError.html 19KB
ScriptableOutputStream.html 19KB
deprecated-list.html 19KB
EvaluatorException.html 18KB
WrapFactory.html 18KB
ClassCompiler.html 18KB
ClassCache.html 17KB
JavaScriptException.html 17KB
DebuggableScript.html 16KB
Function.html 16KB
Synchronizer.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 966B
package-frame.html 966B
js.jar 794KB
Codegen.java 158KB
Interpreter.java 151KB
ScriptRuntime.java 122KB
ClassFileWriter.java 106KB
SwingGui.java 105KB
NativeRegExp.java 100KB
Context.java 90KB
ScriptableObject.java 87KB
XML.java 77KB
Parser.java 69KB
NativeDate.java 55KB
TokenStream.java 48KB
Dim.java 47KB
IRFactory.java 46KB
DToA.java 46KB
NativeArray.java 45KB
JavaAdapter.java 41KB
Node.java 37KB
XMLList.java 37KB
Global.java 35KB
NativeJavaObject.java 33KB
JavaMembers.java 32KB
NativeString.java 31KB
JsDriver.java 29KB
Decompiler.java 28KB
NativeGlobal.java 27KB
XMLObjectImpl.java 27KB
XMLObjectImpl.java 25KB
XmlNode.java 25KB
IdScriptableObject.java 24KB
XMLLibImpl.java 23KB
Block.java 22KB
Main.java 22KB
NativeJavaMethod.java 21KB
FunctionObject.java 21KB
UintMap.java 21KB
ObjToIntMap.java 21KB
Optimizer.java 21KB
Main.java 20KB
RegExpImpl.java 20KB
XML.java 20KB
共 258 条
- 1
- 2
- 3
资源评论
- zhaohunantao2013-06-04已经找到官网地址了,不过还是谢谢了。
- w83080562014-06-05还是帮了挺大的忙的,谢谢!
- tinysmd2013-09-17比较有价值...
- qteafang2014-02-20没找到官网地址 谢谢
- jzcdj2013-06-21还是比较有参考意义的,虽然我是用VC的
szlwm
- 粉丝: 3
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于keras+fasterRCNN,在VOC格式的口罩数据集上训练,检测人群中有无戴口罩python源码+模型
- 基于opencv+qt5机器视觉的传统缺陷检测, 即采用标准图片和待测图片进行pixel to pixel的XOR操作源码+文档
- 管道内检测缺陷数据库管理系统源码+文档说明+sln
- 毕业设计-低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等)源码+文档说明+截图
- 基于yolov5-tensorRT检测+发动机缸体内壁缺陷检测系统源码+文档说明
- 基于C++实现的锂电池缺陷检测源码+文档说明
- push_version
- 软件自制图像批量压缩工具
- 经典缺陷检测算法源码整理包含PaDiM(2020ICPR)、PatchCore(2022CVPR)、SimpleNet+文档说明
- 基于深度学习的抗梯度噪声的缺陷检测器python源码+文档说明+模型的预训练
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功