ovidiu 02/03/15 15:38:51 Modified: src/scratchpad/schecoon/src/org/apache/cocoon/components/flow JavaScriptInterpreter.java Log: Remember the context object. Implement readScript. Correctly obtain the request and response objects. Partial implementation of callFunction. Revision Changes Path 1.4 +92 -30 xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow/JavaScriptInterpreter.java Index: JavaScriptInterpreter.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow/JavaScriptInterpreter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JavaScriptInterpreter.java 14 Mar 2002 20:24:08 -0000 1.3 +++ JavaScriptInterpreter.java 15 Mar 2002 23:38:51 -0000 1.4 @@ -1,36 +1,46 @@ package org.apache.cocoon.components.flow; +import java.io.BufferedReader; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.Reader; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.component.Component; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; +import org.apache.avalon.framework.context.ContextException; +import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.Constants; import org.apache.cocoon.components.treeprocessor.InvokeContext; import org.apache.cocoon.environment.Environment; +import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.Response; import org.apache.cocoon.environment.http.HttpEnvironment; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Function; import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; public class JavaScriptInterpreter extends AbstractLoggable - implements Component, Interpreter, Initializable, Composable, ThreadSafe + implements Component, Interpreter, Initializable, Contextualizable, + Composable, ThreadSafe { Scriptable scope; + org.apache.cocoon.environment.Context context; ComponentManager manager; public void initialize() throws Exception { - org.mozilla.javascript.Context context - = org.mozilla.javascript.Context.enter(); - context.setCompileFunctionsWithDynamicScope(true); + Context context = org.mozilla.javascript.Context.enter(); try { scope = context.initStandardObjects(null); @@ -40,59 +50,103 @@ } } + public void contextualize(org.apache.avalon.framework.context.Context context) + throws ContextException + { + this.context = (org.apache.cocoon.environment.Context) + context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); + } + public void compose(ComponentManager manager) { this.manager = manager; } - public void readScript(InputStream in) + public synchronized void readScript(String source) throws Exception { + Context ctx = Context.enter(); + try { + InputStream is = context.getResourceAsStream(source); + Reader reader = new BufferedReader(new InputStreamReader(is)); + + System.out.println("evaluating JavaScript " + source + + ", scope = " + scope); + + Object result = ctx.evaluateReader(scope, reader, source, 1, null); + System.out.println("JavaScript result = " + result); + result = scope.get("myf", scope); + System.out.println("myf = " + result); + } + finally { + ctx.exit(); + } } - public void callFunction(String funName, Parameters params, + public void callFunction(String funName, List params, Environment environment, InvokeContext ctx) throws Exception { - org.mozilla.javascript.Context context - = org.mozilla.javascript.Context.enter(); - context.setCompileFunctionsWithDynamicScope(true); - Scriptable threadScope = context.newObject(scope); + Context context = Context.enter(); +// context.setCompileFunctionsWithDynamicScope(true); + Scriptable thrScope = context.newObject(scope); + + System.out.println("JavaScript: invoking " + funName + + ", params = " + params); try { - threadScope.setPrototype(scope); - // We want 'threadScope' to be a new top-level scope, so set its + thrScope.setPrototype(scope); + // We want 'thrScope' to be a new top-level scope, so set its // parent scope to null. This means that any variables created - // by assignments will be properties of "threadScope". - threadScope.setParentScope(null); + // by assignments will be properties of "thrScope". + thrScope.setParentScope(scope); // Put in the thread scope the environment, the request, the // response, and the interpreter objects. Map objectModel = environment.getObjectModel(); Request request - = (Request)objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); + = (Request)objectModel.get(ObjectModelHelper.REQUEST_OBJECT); Response response - = (Response)objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); + = (Response)objectModel.get(ObjectModelHelper.RESPONSE_OBJECT); - threadScope.put("environment", threadScope, environment); - threadScope.put("request", threadScope, request); - threadScope.put("response", threadScope, response); - threadScope.put("interpreter", threadScope, this); - - String funCall = funName + "();"; - - context.evaluateString(threadScope, funCall, - "org.apache.cocoon.flow.JavaScriptInterpreter", - 1, null); + thrScope.put("environment", thrScope, environment); + thrScope.put("request", thrScope, request); + thrScope.put("response", thrScope, response); + thrScope.put("interpreter", thrScope, this); + + Object fun = thrScope.get(funName, scope); + System.out.println("function " + funName + " = " + fun); + + if (fun == Scriptable.NOT_FOUND) + throw new RuntimeException("'" + funName + "' is undefined!"); + if (!(fun instanceof Function)) + throw new RuntimeException("'" + funName + "' is not a function!"); + + int size = (params != null ? params.size() : 0); + Object[] funArgs = new Object[size]; + if (size != 0) { + for (int i = 0; i < size; i++) + funArgs[i] = ((Interpreter.Argument)params.get(i)).value; + } + + Object result = ((Function) fun).call(context, thrScope, thrScope, funArgs); + if (result != context.getUndefinedValue()) + System.out.println("result = " + context.toString(result)); + +// String funCall = funName + "();"; +// Object result = context.evaluateString(thrScope, funCall, +// "org.apache.cocoon.flow.JavaScriptInterpreter", +// 1, null); +// System.out.println("result = " + result); } finally { // Remove the environment, request, response and interpreter // objects from the JavaScript thread scope so they can be // garbage collected. - threadScope.delete("environment"); - threadScope.delete("request"); - threadScope.delete("response"); - threadScope.delete("interpreter"); + thrScope.delete("environment"); + thrScope.delete("request"); + thrScope.delete("response"); + thrScope.delete("interpreter"); context.exit(); } } @@ -100,5 +154,13 @@ public void processPipeline(String name, HashMap pipelineArgs, Object bizData) throws Exception { + } + + class JSShell extends ScriptableObject + { + public String getClassName() + { + return "global"; + } } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]