update core for recent js changes
[org.ibex.core.git] / src / org / ibex / core / Template.java
index efa3bbb..a3d0e4e 100644 (file)
@@ -33,7 +33,6 @@ public class Template {
     private JS script = null;           ///< the script on this node
     Template prev;
     Template prev2;
-    JSScope staticScope = null;         ///< the scope in which the static block is executed
     JS staticObject = null;
 
 
@@ -86,8 +85,9 @@ public class Template {
         for(int i=0; i<urikeys.length; i++) {
             if (urikeys[i] == null) continue;
             // FEATURE: Cache urikeys and resolved resources
-            pis.declare(JS.S(urikeys[i]));
-            pis.put(JS.S(urikeys[i]), ibex.resolveString(urivals[i], true));
+            //pis.declare(JS.S(urikeys[i]));
+            // JS:FIXME: ugly
+            pis.sput(JS.S(urikeys[i]), ibex.resolveString(urivals[i], true));
         }
 
         // FIXME needs to obey the new application-ordering rules
@@ -97,7 +97,7 @@ public class Template {
             b.putAndTriggerTraps(b.get(JS.S("numchildren")), kid);
         }
 
-        if (script != null) JS.cloneWithNewParentScope(script, pis).call(null, null, null, null, 0);
+        if (script != null) JS.cloneWithNewGlobalScope(script, pis).call(null, null, null, null, 0);
 
         for(int i=0; keys != null && i < keys.length; i++) {
             if (keys[i] == null) continue;
@@ -167,8 +167,8 @@ public class Template {
             parse(new InputStreamReader(is));
             JS staticScript = parseScript(static_content, static_content_start);
             t.staticObject = new JS.O();
-            t.staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject);
-            if (staticScript != null) JS.cloneWithNewParentScope(staticScript, t.staticScope).call(null, null, null, null, 0);
+            JS staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject);
+            if (staticScript != null) JS.cloneWithNewGlobalScope(staticScript, staticScope).call(null, null, null, null, 0);
         }
 
         private JS parseScript(StringBuffer content, int content_start) throws IOException {
@@ -211,6 +211,7 @@ public class Template {
                     // t.prev2 = (Template)t.ibex.resolveString(tagname, false).call(null, null, null, null, 9999);
                     if(t.prev2 != null) throw new Error("FIXME: t.prev2 != null");
                     t.prev2 = ((Ibex.Blessing)t.ibex.resolveString(tagname, false)).getTemplate();
+                    if(t.prev2 == null) throw new Exception("" + tagname + " not found");
                 } catch (Exception e) {
                     Log.error(Template.class, e);
                 }
@@ -326,32 +327,41 @@ public class Template {
         public void whitespace(char[] ch, int start, int length) throws XML.Exn { }
     }
 
-    private static class PerInstantiationScope extends JSScope {
+    private static class PerInstantiationScope extends JS.O {
         Ibex ibex = null;
         PerInstantiationScope parentBoxPis = null;
         JS myStatic = null;
+        JS box;
         void putDollar(String key, Box target) throws JSExn {
             if (parentBoxPis != null) parentBoxPis.putDollar(key, target);
             JS jskey = JS.S("$" + key);
-            declare(jskey);
-            put(jskey, target);
+            //declare(jskey);
+            sput(jskey, target);
         }
-        public PerInstantiationScope(JSScope parentScope, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) {
-            super(parentScope);
+        // JS:FIXME: ugly
+        void sput(JS key, JS val) throws JSExn { super.put(key,val); }
+        public PerInstantiationScope(JS box, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) {
             this.parentBoxPis = parentBoxPis;
             this.ibex = ibex;
             this.myStatic = myStatic;
+            this.box = box;
         }
         public JS get(JS key) throws JSExn {
-            if (super.has(key)) return super.get(key);
             if(JS.isString(key)) {
                 String s = JS.toString(key);
+                // JS:FIXME This is a hack
+                if (super.get(key) != null) return super.get(key);
                 if (s.equals("ibex")) return ibex;
                 if (s.equals("")) return ibex.get(key);
                 if (s.equals("static")) return myStatic;
             }
-            return super.get(key);
+            // JS:FIXME: This won't work with traps that do blocking operations
+            return box.getAndTriggerTraps(key);
         }
+        // JS:FIXME: Everything below here should come from js.scope or something
+        public void put(JS key, JS val) throws JSExn { if(box != null) box.putAndTriggerTraps(key,val); else super.put(key,val); }
+        public void addTrap(JS key, JSFunction f) throws JSExn { box.addTrap(key,f); }
+        public void delTrap(JS key, JSFunction f) throws JSExn { box.delTrap(key,f); }
     }
 
 }