finished updating xt for new JS API
[org.ibex.xt.git] / src / org / ibex / xt / Template.java
index b585033..3a492af 100644 (file)
@@ -41,30 +41,32 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
     }
 
     public static Node transform(Node n, Scope scope) {
-        if (n.cdata != null) n.cdata = eval(n.cdata, scope).toString();
-        else for(int i=1; i<n.numattrs*2; i+=2) n.attrs[i] = eval(n.attrs[i], scope).toString();
-        return n;
+        try {
+            if (n.cdata != null) n.cdata = eval(n.cdata, scope).toString();
+            else for(int i=1; i<n.numattrs*2; i+=2) n.attrs[i] = eval(n.attrs[i], scope).toString();
+            return n;
+        } catch (JSExn e) { throw new RuntimeException(e); }
     }
 
-    private static Object eval(String s, Scope scope) {
+    private static Object eval(String s, Scope scope) throws JSExn {
         if (s == null) return null;
         StringBuffer ret = new StringBuffer();
         for(boolean first = true; s.indexOf("${") != -1; first = false) {
             ret.append(s.substring(0, s.indexOf("${")));
             String s2 = s.substring(s.indexOf("${")+2);
-            Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
+            JS app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
             s = s.substring(s.indexOf('}') + 1);
             //if (first && s.trim().length() == 0) return app;
-            if (!(app == null || app instanceof String || app instanceof Number || app instanceof Boolean))
+            if (!(app == null || app instanceof JSPrimitive))
                 throw new RuntimeException("javascripts within ${...} can only return strings, numbers, and booleans; not a " +
                                            app.getClass().getName());
-            ret.append(app == null ? "null" : app.toString());
+            ret.append(app == null ? "null" : JSU.toString(app));
         }
         ret.append(s);
         return ret.toString();
     }
 
-    public static Object exec(String s, Scope scope) {
+    public static JS exec(String s, Scope scope) {
         try {
             return JSU.cloneWithNewGlobalScope(JSU.fromReader("input", 0, new StringReader(s)), scope).call(null,null);
         } catch (Exception e) {
@@ -156,7 +158,7 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
         }
     }
 
-    public static class Scope extends JS.Immutable {
+    public static class Scope extends JS.Obj {
         private final JS parent;
         private final Hash declared = new Hash();
         public Scope(JS parent) { this.parent = parent; }