Revision: 1886
          http://svn.sourceforge.net/vexi/?rev=1886&view=rev
Author:   mkpg2
Date:     2007-06-27 11:40:47 -0700 (Wed, 27 Jun 2007)

Log Message:
-----------
Backporting feature. Backtracing of non-js calls (init template, apply 
template).

Modified Paths:
--------------
    
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Interpreter.jpp
    core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/JSExn.java
    core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Thread.java
    
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Template.java
    
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
    core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Vexi.jpp

Added Paths:
-----------
    
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Backtraceable.java

Added: 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Backtraceable.java
===================================================================
--- 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Backtraceable.java
                            (rev 0)
+++ 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Backtraceable.java
    2007-06-27 18:40:47 UTC (rev 1886)
@@ -0,0 +1,5 @@
+package org.ibex.js;
+
+public interface Backtraceable {
+       public String traceLine();
+}

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Interpreter.jpp
===================================================================
--- 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Interpreter.jpp   
    2007-06-27 18:38:55 UTC (rev 1885)
+++ 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Interpreter.jpp   
    2007-06-27 18:40:47 UTC (rev 1886)
@@ -24,6 +24,7 @@
 
     Interpreter old = null;
    
+    public Interpreter(){}
     Interpreter(JSFunction f, boolean pauseable, JS[] args) {
         this.f = f;
         this.pausecount = pauseable ? 0 : -1;
@@ -611,7 +612,7 @@
 
     static class Marker {}
     
-    static class CallMarker extends Marker {
+    static class CallMarker extends Marker implements Backtraceable{
         final int pc;
         final Scope scope;
         final JSFunction f;
@@ -620,6 +621,15 @@
             scope = cx == null ? null : cx.scope;
             f = cx == null ? null : cx.f;
         }
+        
+        public String traceLine(){
+            if(f == null) return null;
+            String s = f.sourceName + ":" + f.line[pc-1];
+            if(this instanceof Interpreter.TrapMarker) 
+                s += " (trap on " + 
JSU.str(((Interpreter.TrapMarker)this).t.key()) + ")";
+            return s;
+            
+        }
     }
     
     static class TrapMarker extends CallMarker {
@@ -762,17 +772,21 @@
         
         void backtrace(JSExn e) {
             for(int i=sp-1;i>=0;i--) {
-                if (stack[i] instanceof CallMarker) {
-                    CallMarker cm = (CallMarker)stack[i];
-                    if(cm.f == null) break;
-                    String s = cm.f.sourceName + ":" + cm.f.line[cm.pc-1];
-                    if(cm instanceof Interpreter.TrapMarker) 
-                        s += " (trap on " + 
JSU.str(((Interpreter.TrapMarker)cm).t.key()) + ")";
-                    e.addBacktrace(s);
+                if (stack[i] instanceof Backtraceable) {
+                       Backtraceable cm = (Backtraceable)stack[i];
+                    e.addBacktrace(cm.traceLine());
                 }
             }
         }
     }
+
+       public void enterNonJSCall(Backtraceable call) throws JSExn{
+               stack.push(call);
+       }
+
+       public void exitNonJSCall(){
+               stack.pop();
+       }
 }
 
 /* FOOTNOTES

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/JSExn.java
===================================================================
--- core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/JSExn.java    
2007-06-27 18:38:55 UTC (rev 1885)
+++ core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/JSExn.java    
2007-06-27 18:40:47 UTC (rev 1886)
@@ -38,7 +38,8 @@
         if(cx == null) cx = Thread.getCurrentInterpreter();
         if(cx == null) return;
         while(cx!=null){
-               addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
+               if(cx.f!=null)
+                       addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
                cx.stack.backtrace(this);
                cx = cx.old;
         }
@@ -61,7 +62,10 @@
     public String toString() { return "JSExn: " + js.getMessage(); }
     public String getMessage() { return toString(); }
     
-    void addBacktrace(String line) { backtrace.add(line); }
+    void addBacktrace(String line) { 
+       if(line==null) return;
+       backtrace.add(line);
+    }
     
     public static class Wrapper extends RuntimeException {
         public final JSExn e;

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Thread.java
===================================================================
--- core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Thread.java   
2007-06-27 18:38:55 UTC (rev 1885)
+++ core/branches/PRE_GRIDBOX_REMOVAL/org.ibex.js/src/org/ibex/js/Thread.java   
2007-06-27 18:40:47 UTC (rev 1886)
@@ -38,7 +38,7 @@
                return current;
     }
 
-       static Interpreter getCurrentInterpreter(){
+       static public Interpreter getCurrentInterpreter(){
                return current==null?null:current.currentInterpreter;
        }
 
@@ -157,6 +157,21 @@
                }
        }
        
+       /** Creates a thread that doesn't immediately execute JS
+       (necessary for non-JS backtracing) */
+       static public void beforeNonJS() throws JSExn{
+               if(current!=null) throw new JSExn("Something a miss"); 
+               // FEATURE - reuse the thread object
+               setCurrent(new Thread(null,false,null));
+               // REMARK -This interpreter is just used for its stack for
+               // recording non-JS call entries (Box.apply) for backtracing 
later
+               current.currentInterpreter = new Interpreter();
+       }
+       
+       static public void afterNonJS() throws JSExn{
+               current.destroy();
+       }
+       
        /** Schedule JS code to execute in a background thread.*/
        static public void runInNew(JS function, JS[] args) throws JSExn{
                Scheduler.add(new Thread(function, true, args));
@@ -187,7 +202,7 @@
        
        /** Constructor */
        private Thread(JS f, boolean pauseable, JS[] args) throws JSExn{ 
-               if (f == null) throw new JSExn("attempted to create a null 
thread");
+               //if (f == null) throw new JSExn("attempted to create a null 
thread");
        this.f = (JSFunction)f;
        this.pauseable = pauseable;
        this.args = args==null?Constants.EMPTY_JS_ARRAY:args;

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Template.java
===================================================================
--- 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Template.java 
    2007-06-27 18:38:55 UTC (rev 1885)
+++ 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Template.java 
    2007-06-27 18:40:47 UTC (rev 1886)
@@ -6,10 +6,12 @@
 
 import java.io.IOException;
 
+import org.ibex.js.Backtraceable;
 import org.ibex.js.JS;
 import org.ibex.js.JSExn;
 import org.ibex.js.JSU;
 import org.ibex.js.Parser;
+import org.ibex.js.Thread;
 import org.ibex.util.Basket;
 import org.ibex.util.Tree;
 import org.ibex.util.Vec;
@@ -75,7 +77,7 @@
  *  @author amegacz
  *  @author dcrawshaw
  */
-public class Template extends CodeBlock implements Constants {
+public class Template extends CodeBlock implements Constants, Backtraceable{
 
     // Instance Members ///////////////////////////////////////////////////////
 
@@ -99,11 +101,13 @@
     // Static part - outer most templates have a static part 
     Static staticPart = null;
     class Static{ 
+       String sourceName;
                JS staticObject;
        Prefixes uriPrefixes;           // Uri prefixes->Blessings
-       public Static(Prefixes staticPrefixes) {
+       public Static(String sourceName, Prefixes staticPrefixes) {
                 staticObject = new JS.Obj();
-                uriPrefixes =staticPrefixes; 
+                uriPrefixes =staticPrefixes;
+                this.sourceName = sourceName;
                }
     }
 
@@ -147,50 +151,66 @@
 
     private static final JS[] callempty = new JS[0];
     private void apply(Box b, PerInstantiationScope parentPis) throws JSExn, 
IOException {
-        if (preapply != null) preapply.apply(b, null);
-        if (principal != null) principal.apply(b, null);
+       Thread.getCurrentInterpreter().enterNonJSCall( this);
+       try{
+               if (preapply != null) preapply.apply(b, null);
+               if (principal != null) principal.apply(b, null);
 
-        // FIXME this dollar stuff is all wrong
-        if (id != null && parentPis!=null) parentPis.putDollar(id, b);
+               // FIXME this dollar stuff is all wrong
+               if (id != null && parentPis!=null) parentPis.putDollar(id, b);
 
-        // Propagate the static part (and access to the static object
-        if(parentPis!=null)    staticPart = parentPis.getStaticPart();
-        // FEATURE only if necessary (i.e. if there is JS code)
-        PerInstantiationScope pis = new PerInstantiationScope(b, parentPis);
+               // Propagate the static part (and access to the static object
+               if(parentPis!=null)     staticPart = parentPis.getStaticPart();
+               // FEATURE only if necessary (i.e. if there is JS code)
+               PerInstantiationScope pis = new PerInstantiationScope(b, 
parentPis);
 
-        // FIXME needs to obey the new application-ordering rules
-        for (int i=0; children != null && i<children.size(); i++) {
-            Box kid = new Box();
-            ((Template)children.elementAt(i)).apply(kid, pis);
-            b.putAndTriggerTraps(b.get(SC_numchildren), kid);
-        }
+               // FIXME needs to obey the new application-ordering rules
+               for (int i=0; children != null && i<children.size(); i++) {
+                       Box kid = new Box();
+                       ((Template)children.elementAt(i)).apply(kid, pis);
+                       b.putAndTriggerTraps(b.get(SC_numchildren), kid);
+               }
 
-        if (script != null) JSU.cloneWithNewGlobalScope(script, 
pis).call(null, callempty);
+               if (script != null) JSU.cloneWithNewGlobalScope(script, 
pis).call(null, callempty);
 
-        for(int i=0; keys != null && i < keys.length; i++) {
-            if (keys[i] == null) continue;
-            JS key = keys[i];
-            JS val = vals[i];
+               for(int i=0; keys != null && i < keys.length; i++) {
+                       if (keys[i] == null) continue;
+                       JS key = keys[i];
+                       JS val = vals[i];
 
-            if ("null".equals(val)) val = null;
+                       if ("null".equals(val)) val = null;
 
-            if (JSU.isString(val) && (JSU.toString(val).length() > 0)) {
-                switch (JSU.toString(val).charAt(0)) {
-                    case '$':
-                        val = pis.get(val);
-                        if (val == null) throw new JSExn("unknown box id 
'"+JSU.str(vals[i])+"' referenced in XML attribute");
-                        break;
-                    case '.':
-                        val = Vexi.resolveString(vexi, 
JSU.toString(val).substring(1), false);
-                    // FIXME: url case
-                    // FIXME: should we be resolving all of these in the 
XML-parsing code?
-                }
-            }
-            b.putAndTriggerTraps(key, val);
+                       if (JSU.isString(val) && (JSU.toString(val).length() > 
0)) {
+                               switch (JSU.toString(val).charAt(0)) {
+                               case '$':
+                                       val = pis.get(val);
+                                       if (val == null) throw new 
JSExn("unknown box id '"+JSU.str(vals[i])+"' referenced in XML attribute");
+                                       break;
+                               case '.':
+                                       val = Vexi.resolveString(vexi, 
JSU.toString(val).substring(1), false);
+                                       // FIXME: url case
+                                       // FIXME: should we be resolving all of 
these in the XML-parsing code?
+                               }
+                       }
+                       b.putAndTriggerTraps(key, val);
+               }
+        }/*catch(JSExn e){
+               Log.uError("", fileName() +":" +startLine);
+               throw e;
+        }*/finally{
+               Thread.getCurrentInterpreter().exitNonJSCall();
         }
     }
+       public String traceLine() {
+               return fileName() +":" +startLine + "(apply)";
+       };
+    private String fileName(){
+       if(staticPart!=null){
+               return staticPart.sourceName;
+       }
+       return parent.fileName();
+    }
 
-
     // XML Parsing 
/////////////////////////////////////////////////////////////////
 
     /*public static Template buildTemplate(String sourceName, JS s, Vexi vexi) 
throws IOException, JSExn, XML.Exn {
@@ -382,7 +402,7 @@
         public void delTrap(JS key, JS f) throws JSExn { box.delTrap(key,f); }
         public Trap getTrap(JS key) throws JSExn { return box.getTrap(key); }
         
-    };
+    }
     
 }
 

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
===================================================================
--- 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
      2007-06-27 18:38:55 UTC (rev 1885)
+++ 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
      2007-06-27 18:40:47 UTC (rev 1886)
@@ -4,14 +4,13 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringReader;
-import java.util.HashSet;
-import java.util.Set;
 
+import org.ibex.js.Backtraceable;
 import org.ibex.js.JS;
 import org.ibex.js.JSExn;
-import org.ibex.js.JSString;
 import org.ibex.js.JSU;
 import org.ibex.js.Parser;
+import org.ibex.js.Thread;
 import org.ibex.js.Parser.GlobalsChecker;
 import org.ibex.util.Basket;
 import org.ibex.util.Log;
@@ -20,8 +19,6 @@
 import org.ibex.util.Vec;
 import org.ibex.util.XML;
 import org.vexi.core.Template.Prefixes;
-import org.vexi.core.Template.Static;
-import org.vexi.core.Template.StaticScope;
 
 
 public class TemplateBuilder{
@@ -53,7 +50,7 @@
        
        
 /** handles XML parsing; builds a Template tree as it goes */
-    final class TemplateHelper extends XML {
+    final class TemplateHelper extends XML implements Backtraceable {
 
         String sourceName;
         private int state = STATE_INITIAL;
@@ -80,7 +77,7 @@
                                
if(Log.sLevel==Log.DEBUG)Log.uDebug(TemplateHelper.class, "Parsing template: " 
+ sourceName);
                                JS staticScript = 
parseScript(staticCode.content, staticCode.content_start, 
createStaticChecker(staticPrefixes));
                                if (t != null) {
-                                       createStatic();
+                                       createStatic(sourceName);
                                        JS staticScope = t.new StaticScope();
                                        if (staticScript != null) 
JSU.cloneWithNewGlobalScope(staticScript, staticScope).call(null, callempty);
                                }else{
@@ -95,8 +92,8 @@
                }
         }
 
-        private void createStatic(){
-                       t.staticPart = t.new Static(staticPrefixes);
+        private void createStatic(String sourceName){
+                       t.staticPart = t.new Static(sourceName, staticPrefixes);
                        // Share static part with preapplies 
                Template t2 = t;
                while((t2=t2.preapply)!=null){
@@ -168,12 +165,19 @@
                     // GROSSER hack
                     // t.prev2 = (Template)t.vexi.resolveString(tagname, 
false).call(null, null, null, null, 9999);
                     if(t.principal != null) throw new Error("FIXME: 
t.principal != null");
-                    t.principal = 
((Blessing)Vexi.resolveString(t.vexi,tagname, false)).getTemplate();
+                               
Thread.getCurrentInterpreter().enterNonJSCall(this);
+                    try{
+                       t.principal = 
((Blessing)Vexi.resolveString(t.vexi,tagname, false)).getTemplate();
+                    }finally{
+                       Thread.getCurrentInterpreter().exitNonJSCall(); 
+                    }
                     if(t.principal == null) throw new SourceException("" + 
tagname + " not found", sourceName, getLine());
                 }catch(SourceException e){
                        Log.uError(e.getMessageSig(), e.getWhere() + "\n- "+ 
e.getMessage());
                        Log.debug(this, e);
-                } 
+                }catch(JSExn e){ 
+                       Log.uError(Template.class, e);
+                }
                 catch (Exception e) {
                     Log.uError(Template.class, e + " at " + this.sourceName + 
", line " + this.getLine() + ".");
                 }
@@ -328,5 +332,10 @@
                 return;
                }
                }
+
+               public String traceLine() {
+                       int l = getLine() ;
+                       return sourceName + ":" + getLine() + "(init)";
+               }
     }
 }
\ No newline at end of file

Modified: 
core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Vexi.jpp
===================================================================
--- core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Vexi.jpp  
2007-06-27 18:38:55 UTC (rev 1885)
+++ core/branches/PRE_GRIDBOX_REMOVAL/org.vexi.core/src/org/vexi/core/Vexi.jpp  
2007-06-27 18:40:47 UTC (rev 1886)
@@ -38,7 +38,12 @@
 
        public Object run(Object o) throws JSExn {
                Log.uInfo(Main.class, "invoking initial template: "+ 
Main.initialTemplate);
-               resolveString(this, Main.initialTemplate, false).call(null, new 
JS[]{new Box()});
+               Thread.beforeNonJS();
+               try{
+                       resolveString(this, Main.initialTemplate, 
false).call(null, new JS[]{new Box()});
+               }finally{
+                       Thread.afterNonJS();
+               }
                return null;
        }
     


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to