2003/05/12 05:31:50
[org.ibex.core.git] / src / org / xwt / Template.java
index 22c994c..a9ea9b9 100644 (file)
@@ -5,7 +5,7 @@ import java.io.*;
 import java.util.zip.*;
 import java.util.*;
 import java.lang.*;
-import org.mozilla.javascript.*;
+import org.xwt.js.*;
 import org.xwt.util.*;
 
 /**
@@ -73,10 +73,10 @@ public class Template {
     private boolean changed = false;
 
     /** the script on the static node of this template, null if it has already been executed */
-    private Script staticscript = null;
+    private JS.Script staticscript = null;
 
     /** the script on this node */
-    private Script script = null;
+    private JS.Script script = null;
 
     /** during XML parsing, this holds the list of currently-parsed children; null otherwise */
     private Vec childvect = new Vec();
@@ -165,7 +165,7 @@ public class Template {
      *  @param pboxes a vector of all box parents on which to put $-references
      *  @param ptemplates a vector of the nodeNames to recieve private references on the pboxes
      */
-    void apply(Box b, Vec pboxes, Vec ptemplates, Function callback, int numerator, int denominator) {
+    void apply(Box b, Vec pboxes, Vec ptemplates, JS.Function callback, int numerator, int denominator) {
 
         int original_numerator = numerator;
 
@@ -178,7 +178,7 @@ public class Template {
             for(int i=0; i<pboxes.size(); i++) {
                 Box parent = (Box)pboxes.elementAt(i);
                 String parentNodeName = (String)ptemplates.elementAt(i);
-                parent.putPrivately("$" + id, b, parentNodeName);
+                parent.put("$" + id, b);
             }
 
         if (script != null || (redirect != null && !"self".equals(redirect))) {
@@ -197,37 +197,32 @@ public class Template {
             }
 
         for (int i=0; children != null && i<children.length; i++) {
-            b.put(Integer.MAX_VALUE, null, new Box(children[i], pboxes, ptemplates, callback, numerator, denominator));
+            b.put(Integer.MAX_VALUE, new Box(children[i], pboxes, ptemplates, callback, numerator, denominator));
             numerator += children[i].numUnits();
         }
 
         // whom to redirect to; doesn't take effect until after script runs
         Box redir = null;
-        if (redirect != null && !"self".equals(redirect))
-            redir = (Box)b.getPrivately("$" + redirect, nodeName);
+        if (redirect != null && !"self".equals(redirect)) redir = (Box)b.get("$" + redirect);
 
         if (script != null) try {
-            Context cx = Context.enter();
-            script.exec(cx, b);
-        } catch (EcmaError e) {
-            if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown while instantiating " + nodeName + " at " + e.getSourceName() + ":" + e.getLineNumber());
-        } catch (JavaScriptException e) {
+           JS.Array jsa = new JS.Array();
+           jsa.addElement(b);
+            script.call(jsa);
+        } catch (JS.Exn e) {
             if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown while instantiating " + nodeName + " at " + e.sourceFile + ":" + e.line);
         }
 
         for(int i=0; keys != null && i<keys.length; i++) {
-            Context.enter().interpreterSourceFile = nodeName;
-            Context.enter().interpreterLine = startLine;
+           // FIXME: make sure that if exceptions are thrown in here, the line number of the offending XML is logged
             if (keys[i] == null) { }
             else if (keys[i].equals("border") || keys[i].equals("image") &&
                      !vals[i].toString().startsWith("http://") && !vals[i].toString().startsWith("https://")) {
                 String s = Resources.resolve(vals[i].toString() + ".png", importlist);
-                if (s != null) b.put(keys[i], null, s.substring(0, s.length() - 4));
+                if (s != null) b.put(keys[i], s.substring(0, s.length() - 4));
                 else if (Log.on) Log.log(this, "unable to resolve image " + vals[i].toString() + " referenced in attributes of " + nodeName); 
             }
-            else b.put(keys[i], null, vals[i]);
+            else b.put(keys[i], vals[i]);
         }
 
         if (redirect != null && !"self".equals(redirect)) b.redirect = redir;
@@ -245,20 +240,18 @@ public class Template {
 
         if (callback != null)
             try {
-                callback.call(Context.enter(), null, null, new Object[] { new Double(numerator), new Double(denominator) });
-            } catch (EcmaError e) {
-                if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
-                if (Log.on) Log.log(this, "         thrown from within progress callback at " + e.getSourceName() + ":" + e.getLineNumber());
-            } catch (JavaScriptException e) {
-                if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
-                if (Log.on) Log.log(this, "         thrown from within progress callback at " + e.sourceFile + ":" + e.line);
+               JS.Array args = new JS.Array();
+               args.addElement(new Double(numerator));
+               args.addElement(new Double(denominator));
+                callback.call(args);
+            } catch (JS.Exn e) {
+                if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e);
             }
 
         if (Thread.currentThread() instanceof ThreadMessage) try {
-            XWT.yield.call(Context.enter(), null, null, null);
-        } catch (JavaScriptException e) {
-            if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown from within yield at " + e.sourceFile + ":" + e.line);
+           XWT.sleep(0);
+        } catch (JS.Exn e) {
+           if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e);
         }
     }
 
@@ -273,7 +266,7 @@ public class Template {
     }
 
     /** adds a theme mapping, retemplatizing as needed */
-    public static void retheme(Function callback) {
+    public static void retheme(JS.Function callback) {
         XWF.flushXWFs();
 
         // clear changed marker and relink
@@ -293,19 +286,20 @@ public class Template {
 
         if (callback != null)
             try {
-                callback.call(Context.enter(), null, null, new Object[] { new Double(1.0), new Double(1.0) });
-            } catch (EcmaError ex) {
-                if (Log.on) Log.log(Template.class, "WARNING: uncaught interpreter exception: " + ex.getMessage());
-                if (Log.on) Log.log(Template.class, "         thrown from within progress callback at " + ex.getSourceName() + ":" + ex.getLineNumber());
-            } catch (JavaScriptException ex) {
+               JS.Array args = new JS.Array();
+               args.addElement(new Double(1.0));
+               args.addElement(new Double(1.0));
+                callback.call(args);
+            } catch (JS.Exn ex) {
                 if (Log.on) Log.log(Template.class, "WARNING: uncaught ecmascript exception: " + ex.getMessage());
-                if (Log.on) Log.log(Template.class, "         thrown from within progress callback at " + ex.sourceFile + ":" + ex.line);
             }
     }
 
     /** template reapplication procedure */
     private static void reapply(Box b) {
 
+       throw new Error("not implemented");
+       /*
         // Ref 7.5.1: check if we need to retemplatize
         boolean retemplatize = false;
         if (b.templatename != null) {
@@ -359,6 +353,7 @@ public class Template {
 
         // Recurse
         for(Box j = b.getChild(0); j != null; j = j.nextSibling()) reapply(j);
+       */
     }
 
     /** runs statics, resolves string references to other templates into actual Template instance references, and sets <tt>change</tt> as needed */
@@ -368,20 +363,25 @@ public class Template {
     private void link(boolean force) {
 
         if (staticscript != null) try { 
-            Scriptable s = Static.createStatic(nodeName, false);
+            JS.Scope s = Static.createStatic(nodeName, false);
             if (staticscript != null) {
-                Script temp = staticscript;
-                ((InterpretedScript)temp).setParentScope(s);     // so we know how to handle Static.get("xwt")
+                JS.Script temp = staticscript;
                 staticscript = null;
-                temp.exec(Context.enter(), s);
+
+               // we layer a transparent scope over the Static so that we can catch requests for the xwt object
+               // yet not screw up paths that include a package called xwt (ie xwt.static.org.xwt.foo)
+               JS.Scope varScope = new JS.Scope(s) {
+                       public boolean isTransparent() { return true; }
+                       public Object get(Object key) {
+                           if ("xwt".equals(key)) return XWT.singleton; else return super.get(key);
+                       } };
+
+               JS.Array args = new JS.Array();
+               args.addElement(varScope);
+                temp.call(args);
             }
-        } catch (EcmaError e) {
-            if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown while executing <static/> block for " + nodeName +
-                                      " at " + e.getSourceName() + ":" + e.getLineNumber());
-        } catch (JavaScriptException e) {
+        } catch (JS.Exn e) {
             if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown while executing <static/> block for " + nodeName + " at " + e.sourceFile + ":" + e.line);
         }
 
         if (!(force || (preapply != null && _preapply == null) || (postapply != null && _postapply == null))) return;
@@ -654,17 +654,11 @@ public class Template {
             }
         }
 
-        private Script genscript(boolean isstatic) {
-            Script thisscript = null;
-            Context cx = Context.enter();
-            cx.setOptimizationLevel(-1);
-
+        private JS.Script genscript(boolean isstatic) {
+            JS.Script thisscript = null;
             try {
-                thisscript = cx.compileReader(null, new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start, null);
-            } catch (EcmaError ee) {
-                if (Log.on) Log.log(this, ee.getMessage() + " at " + ee.getSourceName() + ":" + ee.getLineNumber());
-                thisscript = null;
-            } catch (EvaluatorException ee) {
+                thisscript = JS.Script.parse(new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start);
+            } catch (JS.Exn ee) {
                 if (Log.on) Log.log(this, "  ERROR: " + ee.getMessage());
                 thisscript = null;
             } catch (IOException ioe) {