And here is a patch to alter cocoon-2.1.5.1 to use Rhino from mozilla.org.
The patch assumes that rhino1.5r4-continuations-20040228.jar is replaced by rhino1_6R1pre.jar which should be produced by checking out the very latest CVS sources from mozilla.org, building js.jar and moving that to lib/core as rhino1_6R1pre.jar.
That patch was not enough to make the calculator example to work, use the one attached instead. Strictly speaking changes to ScriptablePropertyHandler.java are backward compatible with Rhino from cocoondev.org and probably should go to the first patch but for now they are here.
Regards, Igor
Sat Sep 25 00:35:28 CEST 2004 [EMAIL PROTECTED] * Using Rhino from mozilla.org
Fri Sep 24 19:08:03 CEST 2004 [EMAIL PROTECTED]
* init
diff -rN -ud cocoon-2.1.5.1-old/src/blocks/petstore/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java cocoon-2.1.5.1-new/src/blocks/petstore/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java
--- cocoon-2.1.5.1-old/src/blocks/petstore/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java 2004-09-25 12:36:41.000000000 +0200
+++ cocoon-2.1.5.1-new/src/blocks/petstore/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java 2004-09-24 23:27:45.000000000 +0200
@@ -21,7 +21,6 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.JavaScriptException;
-import org.mozilla.javascript.NotAFunctionException;
import org.mozilla.javascript.PropertyException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
@@ -60,7 +59,7 @@
ScriptableResult(Scriptable scope,
ResultSet rs, int startRow, int maxRows)
- throws SQLException, PropertyException, NotAFunctionException, JavaScriptException {
+ throws SQLException, PropertyException, JavaScriptException {
Context cx = Context.getCurrentContext();
Scriptable rowMap = cx.newObject(scope, "Array");
put("rows", this, rowMap);
diff -rN -ud cocoon-2.1.5.1-old/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/AO_FOM_JavaScriptInterpreter.java cocoon-2.1.5.1-new/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/AO_FOM_JavaScriptInterpreter.java
--- cocoon-2.1.5.1-old/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/AO_FOM_JavaScriptInterpreter.java 2004-09-25 12:36:42.000000000 +0200
+++ cocoon-2.1.5.1-new/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/AO_FOM_JavaScriptInterpreter.java 2004-09-24 23:15:58.000000000 +0200
@@ -52,6 +52,7 @@
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
@@ -107,9 +108,12 @@
*/
public static final String USER_GLOBAL_SCOPE = "FOM JavaScript GLOBAL SCOPE";
- // This is the only optimization level that supports continuations
- // in the Christoper Oliver's Rhino JavaScript implementation
- static int OPTIMIZATION_LEVEL = -2;
+ private static final MyContextFactory
+ contextFactory = new MyContextFactory();
+
+ static {
+ ContextFactory.initGlobal(contextFactory);
+ }
// the postfix of the resulting file streamed into the same directory as
// the basescript
@@ -142,6 +146,30 @@
*/
List topLevelScripts = new ArrayList();
+ private static class MyContextFactory extends ContextFactory
+ {
+ protected boolean hasFeature(Context cx, int featureIndex)
+ {
+ switch (featureIndex) {
+ case Context.FEATURE_INTERPRETER_CONTINUATIONS:
+ return true;
+ }
+ return super.hasFeature(cx, featureIndex);
+ }
+
+ protected void onContextCreated(Context cx)
+ {
+ super.onContextCreated(cx);
+
+ // Force interpretation mode
+ cx.setOptimizationLevel(-1);
+
+ cx.setGeneratingDebug(true);
+
+ cx.setCompileFunctionsWithDynamicScope(true);
+ }
+ }
+
class ScriptSourceEntry {
final private Source source;
private Script script;
@@ -204,18 +232,15 @@
db.pack();
java.awt.Dimension size =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
- size.width *= 0.75;
- size.height *= 0.75;
- db.setSize(size);
+ db.setSize(size.width * 3 / 4, size.height * 3 / 4);
db.setExitAction(new Runnable() {
public void run() {
db.setVisible(false);
}
});
- db.setOptimizationLevel(OPTIMIZATION_LEVEL);
db.setVisible(true);
debugger = db;
- Context.addContextListener(debugger);
+ db.attachTo(contextFactory);
}
return debugger;
}
@@ -267,9 +292,6 @@
getDebugger().doBreak();
}
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setCompileFunctionsWithDynamicScope(true);
- context.setGeneratingDebug(true);
// add support for Rhino objects to JXPath
JXPathIntrospector.registerDynamicClass(org.mozilla.javascript.Scriptable.class,
ScriptablePropertyHandler.class);
@@ -587,9 +609,6 @@
throws Exception
{
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setGeneratingDebug(true);
- context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
AO_FOM_Cocoon cocoon = null;
Scriptable thrScope = getSessionScope();
@@ -667,9 +686,6 @@
}
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setGeneratingDebug(true);
- context.setCompileFunctionsWithDynamicScope(true);
// Obtain the continuation object from it, and setup the
// FOM_Cocoon object associated in the dynamic scope of the saved
diff -rN -ud cocoon-2.1.5.1-old/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java cocoon-2.1.5.1-new/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java
--- cocoon-2.1.5.1-old/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java 2004-09-25 12:36:44.000000000 +0200
+++ cocoon-2.1.5.1-new/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java 2004-09-25 12:01:09.000000000 +0200
@@ -58,6 +58,7 @@
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.source.SourceValidity;
import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
@@ -99,9 +100,12 @@
*/
public static final String USER_GLOBAL_SCOPE = "FOM JavaScript GLOBAL SCOPE";
- // This is the only optimization level that supports continuations
- // in the Christoper Oliver's Rhino JavaScript implementation
- static int OPTIMIZATION_LEVEL = -2;
+ private static final MyContextFactory
+ contextFactory = new MyContextFactory();
+
+ static {
+ ContextFactory.initGlobal(contextFactory);
+ }
/**
* When was the last time we checked for script modifications. Used
@@ -136,6 +140,30 @@
protected ServiceManager getServiceManager() {
return manager;
}
+
+ private static class MyContextFactory extends ContextFactory
+ {
+ protected boolean hasFeature(Context cx, int featureIndex)
+ {
+ switch (featureIndex) {
+ case Context.FEATURE_INTERPRETER_CONTINUATIONS:
+ return true;
+ }
+ return super.hasFeature(cx, featureIndex);
+ }
+
+ protected void onContextCreated(Context cx)
+ {
+ super.onContextCreated(cx);
+
+ // Force interpretation mode
+ cx.setOptimizationLevel(-1);
+
+ cx.setGeneratingDebug(true);
+
+ cx.setCompileFunctionsWithDynamicScope(true);
+ }
+ }
class MyClassRepository implements CompilingClassLoader.ClassRepository {
@@ -224,10 +252,9 @@
db.setVisible(false);
}
});
- db.setOptimizationLevel(OPTIMIZATION_LEVEL);
db.setVisible(true);
debugger = db;
- Context.addContextListener(debugger);
+ db.attachTo(contextFactory);
}
return debugger;
}
@@ -273,9 +300,6 @@
getDebugger().doBreak();
}
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setCompileFunctionsWithDynamicScope(true);
- context.setGeneratingDebug(true);
// add support for Rhino objects to JXPath
JXPathIntrospector.registerDynamicClass(Scriptable.class,
ScriptablePropertyHandler.class);
@@ -657,9 +681,6 @@
public void callFunction(String funName, List params,
Redirector redirector) throws Exception {
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setGeneratingDebug(true);
- context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
ThreadScope thrScope = getSessionScope();
synchronized (thrScope) {
@@ -743,9 +764,6 @@
}
Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setGeneratingDebug(true);
- context.setCompileFunctionsWithDynamicScope(true);
// Obtain the continuation object from it, and setup the
// FOM_Cocoon object associated in the dynamic scope of the saved
diff -rN -ud cocoon-2.1.5.1-old/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyHandler.java cocoon-2.1.5.1-new/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyHandler.java
--- cocoon-2.1.5.1-old/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyHandler.java 2004-07-09 10:03:47.000000000 +0200
+++ cocoon-2.1.5.1-new/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyHandler.java 2004-09-25 12:22:59.000000000 +0200
@@ -31,72 +31,50 @@
public class ScriptablePropertyHandler implements DynamicPropertyHandler {
public Object getProperty(Object obj, String propertyName) {
- Context cx = null;
- try {
- cx = Context.enter();
- Scriptable s = (Scriptable)obj;
- Object result = ScriptableObject.getProperty(s, propertyName);
- if (result == Scriptable.NOT_FOUND) {
- result = ScriptableObject.getProperty(s, "get" + propertyName.substring(0, 1).toUpperCase() + (propertyName.length() > 1 ? propertyName.substring(1) : ""));
- if (result != Scriptable.NOT_FOUND &&
- result instanceof Function) {
- try {
- result = ((Function)result).call(cx,
- ScriptableObject.getTopLevelScope(s), s, new Object[] {});
- } catch (JavaScriptException exc) {
- exc.printStackTrace();
- result = Undefined.instance;
- }
- }
- if (result == Undefined.instance ||
- result == Scriptable.NOT_FOUND) {
- result = null;
- }
- } else if (result instanceof Wrapper) {
- result = ((Wrapper)result).unwrap();
- } else if (result == Undefined.instance) {
+ Scriptable s = (Scriptable)obj;
+ Object result = ScriptableObject.getProperty(s, propertyName);
+ if (result == Scriptable.NOT_FOUND) {
+ String getter = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
+ try {
+ result = ScriptableObject.callMethod(s, getter, new Object[] {});
+ } catch (JavaScriptException exc) {
+ exc.printStackTrace();
+ result = Undefined.instance;
+ }
+ if (result == Undefined.instance) {
result = null;
}
- return result;
- } finally {
- Context.exit();
+ } else if (result instanceof Wrapper) {
+ result = ((Wrapper)result).unwrap();
+ } else if (result == Undefined.instance) {
+ result = null;
}
+ return result;
}
public String[] getPropertyNames(Object obj) {
- Context.enter();
- try {
- Object[] ids;
- if (obj instanceof ScriptableObject) {
- ids = ((ScriptableObject)obj).getAllIds();
- } else {
- ids = ((Scriptable)obj).getIds();
- }
- String[] result = new String[ids.length];
- for (int i = 0; i < result.length; i++) {
- result[i] = (String)ids[i];
- }
- return result;
- } finally {
- Context.exit();
+ Object[] ids;
+ if (obj instanceof ScriptableObject) {
+ ids = ((ScriptableObject)obj).getAllIds();
+ } else {
+ ids = ((Scriptable)obj).getIds();
+ }
+ String[] result = new String[ids.length];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = (String)ids[i];
}
+ return result;
}
public void setProperty(Object obj, String propertyName,
Object value) {
- Context.enter();
- try {
- if (!(value == null
- || value instanceof String
- || value instanceof Number
- || value instanceof Boolean)) {
- value = Context.toObject(value,
- (Scriptable)obj);
- }
- ScriptableObject.putProperty((Scriptable)obj,
- propertyName, value);
- } finally {
- Context.exit();
+ if (!(value == null
+ || value instanceof String
+ || value instanceof Number
+ || value instanceof Boolean)) {
+ value = Context.toObject(value,
+ (Scriptable)obj);
}
+ ScriptableObject.putProperty((Scriptable)obj, propertyName, value);
}
}
