upgraded xt some more
[org.ibex.xt.git] / src / org / ibex / xt / Prevalence.java
index ef87147..cd7546f 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.xt;
 import org.ibex.js.*;
 import org.ibex.util.*;
@@ -46,16 +50,19 @@ public class Prevalence {
                     String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
                     System.err.println("prevayling to " + base);
                     pf.configurePrevalenceBase(base);
-                    XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
+                    /*
+                    XmlSnapshotManager manager = new XmlSnapshotManager(new JS(), base, null); {
                             protected XStream createXStream() {
                                 XStream xstream = new XStream();
                                 xstream.alias("js", JS.class);
                                 xstream.alias("jsdate", JSDate.class);
                                 return xstream;
                             }
-                        };
+                            };
                     System.err.println("configuring with " + manager);
                     pf.configureSnapshotManager(manager);
+                    */
+                    pf.configureSnapshotManager(new SnapshotManager(new JS.Obj(), base));
                     //pf.configureClassLoader(JSTransaction.class.getClassLoader());
                     prevayler = pf.create();
                     prevaylers.put(cx, prevayler);
@@ -68,10 +75,24 @@ public class Prevalence {
     public static class JSTransaction implements Transaction {
         public static final long serialVersionUid = 0xfb2aa281;
         private JS js;
-        public JSTransaction(JS js) { this.js = js; }
+        Scope newscope;
+        String[] v;
+        public JSTransaction(JS js) throws JSExn {
+            newscope = new Scope(null);
+            this.js = JSU.cloneWithNewGlobalScope(js, newscope);
+            v = this.js.getFormalArgs();
+            for(int i=0; i<v.length; i++) {
+                if (JSU.S("prevalent").equals(v[i])) continue;
+                newscope.put(JSU.S(v[i]), js.get(JSU.S(v[i])));
+            }
+        }
         public void executeOn(Object o, Date now) {
             try {
-                js.call(o, new JSDate(now.getTime()), null, null, 2);
+                newscope.put(JSU.S("prevalent"), (JS)o);
+                newscope.put(JSU.S("now"), new JSDate(now.getTime()));
+                JS[] args = new JS[v.length];
+                for(int i=0; i<v.length; i++) args[i] = JSU.S(v[i]);
+                js.call(null, args);
             } catch (Exception e) { throw new RuntimeException(e); }
         }
     }
@@ -79,10 +100,11 @@ public class Prevalence {
     public static class JSQuery implements Query {
         public static final long serialVersionUid = 0xfb2aa282;
         private JS js;
-        public JSQuery(JS js) { this.js = js; }
+        private Object a;
+        public JSQuery(JS js, Object a) { this.js = JSU.cloneWithNewGlobalScope(js, null); this.a = a; }
         public Object query(Object o, Date now) {
             try {
-                return js.call(o, new JSDate(now.getTime()), null, null, 2);
+                return js.call(null, new JS[] { (JS)o, (JS)a, new JSDate(now.getTime()) });
             } catch (Exception e) { throw new RuntimeException(e); }
         }
     }