ovidiu      02/03/19 01:36:08

  Modified:    src/scratchpad/schecoon/src/org/apache/cocoon/components/flow
                        JavaScriptInterpreter.java
  Log:
  Do not use initStandardObjects() to initialize the global scope, use
  instead JSGlobal directly.
  
  Add additional InvokeContext argument to enterContext. Pass both the
  Environment and the InvokeContext to the JSCocoon object, to be used
  later in processPipeline().
  
  Call top level functions through a special defined function
  "callFunction". This captures the continuation in the "suicide"
  variable, which is later used to exit non-locally from
  "sendPage". Need to solve bug in Christopher's continuations code,
  which makes the function return twice.
  
  Implement processPipeline(), which calls an internally defined sitemap
  resource. Still need to implement the ability to invoke an arbitrary
  pipeline. TODO: figure out how to pass information from the flow layer
  down the pipeline.
  
  Revision  Changes    Path
  1.7       +46 -15    
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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JavaScriptInterpreter.java        18 Mar 2002 06:39:21 -0000      1.6
  +++ JavaScriptInterpreter.java        19 Mar 2002 09:36:08 -0000      1.7
  @@ -17,9 +17,12 @@
   import org.apache.cocoon.environment.Source;
   import org.mozilla.javascript.Context;
   import org.mozilla.javascript.Function;
  +import org.mozilla.javascript.NativeArray;
   import org.mozilla.javascript.PropertyException;
  +import org.mozilla.javascript.ScriptRuntime;
   import org.mozilla.javascript.Scriptable;
   import org.mozilla.javascript.ScriptableObject;
  +import org.mozilla.javascript.tools.shell.Global;
   
   public class JavaScriptInterpreter extends AbstractInterpreter
     implements Initializable
  @@ -33,11 +36,11 @@
     public void initialize()
       throws Exception
     {
  -    Context context = org.mozilla.javascript.Context.enter();
  +    Context context = Context.enter();
       context.setOptimizationLevel(OPTIMIZATION_LEVEL);
   
       try {
  -      scope = context.initStandardObjects(new JSGlobal());
  +      scope = new JSGlobal(context);
   
         // Register some handy classes with JavaScript, so we can make
         // use of them from the flow layer.
  @@ -71,7 +74,7 @@
       }
     }
   
  -  protected Scriptable enterContext(Environment environment)
  +  protected Scriptable enterContext(Environment environment, InvokeContext ctx)
       throws Exception
     {
       Context context = Context.enter();
  @@ -91,7 +94,7 @@
       Object args[] = {};
       Scriptable cocoon = context.newObject(scope, "Cocoon", args);
       ((JSCocoon)cocoon).setInterpreter(this);
  -    ((JSCocoon)cocoon).setEnvironment(environment);
  +    ((JSCocoon)cocoon).setContext(environment, ctx);
       thrScope.put("cocoon", scope, cocoon);
   
       return thrScope;
  @@ -119,7 +122,7 @@
       System.out.println("Reading JavaScript script " + sourceName);
   
       try {
  -      thrScope = enterContext(environment);
  +      thrScope = enterContext(environment, null);
         source = environment.resolve(sourceName);
         InputStream inputStream = source.getInputStream();
         Reader reader = new BufferedReader(new InputStreamReader(inputStream));
  @@ -147,36 +150,64 @@
                          + ", params = " + params);
   
       try {
  -      thrScope = enterContext(environment);
  +      thrScope = enterContext(environment, ctx);
  +
  +      Object callFunction = scope.get("callFunction", thrScope);
  +      if (callFunction == Scriptable.NOT_FOUND)
  +          throw new RuntimeException("Cannot find 'callFunction' "
  +                                     + "(system.js not loaded?)");
  +
         Object fun = scope.get(funName, thrScope);
  -      
         if (fun == Scriptable.NOT_FOUND)
           throw new RuntimeException("'" + funName + "' is undefined!");
         if (!(fun instanceof Function))
           throw new RuntimeException("'" + funName + "' is not a function!");
   
  +      Context cx = Context.getCurrentContext();
  +
         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;
  +        for (int i = 0; i < size; i++) {
  +          Object obj = ((Interpreter.Argument)params.get(i)).value;
  +          funArgs[i] = ScriptRuntime.toObject(cx, thrScope, obj);
  +        }
         }
  +      NativeArray funArgsArray = new NativeArray(funArgs);
  +      Object callFunArgs[] = { fun, funArgsArray };
   
  -      Context cx = Context.getCurrentContext();
  -      ((Function) fun).call(cx, thrScope, thrScope, funArgs);
  +      ((Function) callFunction).call(cx, thrScope, thrScope, callFunArgs);
       }
       finally {
         exitContext(thrScope);
       }
     }
   
  -  public void processPipeline(String name, Map pipelineArgs,
  -                              Object bizData)
  +  public void processPipeline(String name, Map pipelineArgs, Object bizData,
  +                              Environment environment, InvokeContext ctx)
       throws Exception
     {
  +    if (ctx == null) {
  +      String msg = "Cannot invoke pipeline with a null InvokeContext! Make sure"
  +        + " you're calling the pipeline during the execution of a request.";
  +      throw new RuntimeException(msg);
  +    }
  +
       ProcessingNode pipeline
         = resources.getNodeByName(MapStackResolver.unescape(name));
  -    System.out.println("processPipeline: found pipeline named " + name
  -                       + " to be " + pipeline);
  +
  +    if (pipelineArgs != null)
  +      ctx.pushMap(pipelineArgs);
  +    try {
  +      pipeline.invoke(environment, ctx);
  +    }
  +    catch (Exception ex) {
  +      ex.printStackTrace();
  +      throw ex;
  +    }
  +    finally {
  +      if (pipelineArgs != null)
  +        ctx.popMap();
  +    }
     }
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to