Revision: 4766
          http://sourceforge.net/p/vexi/code/4766
Author:   mkpg2
Date:     2015-02-26 02:07:11 +0000 (Thu, 26 Feb 2015)
Log Message:
-----------
Refactoring. Removed redundant arguments and tidied up thread initialisation a 
little.

Modified Paths:
--------------
    
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Interpreter.java
    branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java
    branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Thread.java

Modified: 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Interpreter.java
===================================================================
--- 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Interpreter.java   
    2015-02-25 20:11:16 UTC (rev 4765)
+++ 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Interpreter.java   
    2015-02-26 02:07:11 UTC (rev 4766)
@@ -5,7 +5,10 @@
 
 package org.ibex.js;
 
-import org.ibex.js.parse.*;
+import org.ibex.js.JS.Trap;
+import org.ibex.js.parse.ByteCodes;
+import org.ibex.js.parse.Function;
+import org.ibex.js.parse.Tokens;
 
 /*@PAGE(concept=Special Variables)
  * 
@@ -106,14 +109,16 @@
     }
     
     Interpreter(Thread thread, JS.Trap t, JS val, boolean pauseOnCascade, JS 
trapname) {
-        this(thread);
-        this.pausecount = -1;
-        try {
-            setupTrap(t,val,new 
TrapMarker(null,t,val,pauseOnCascade),trapname);
-        } catch(JSExn e) {
-            throw new Error("should never happen");
-        }
+       this(thread);
+       this.pausecount = -1;
+        setupTrapSafe(new TrapMarker(this, t,val,pauseOnCascade), trapname);   
 
     }
+//    
+//    Interpreter(Thread thread, TrapMarker marker, JS trapname) {
+//        this(thread);
+//        this.pausecount = thread.pauseable ? 0 : -1;
+//        setupTrapSafe(marker,trapname);        
+//    }
 
     private boolean get = false;
     // FIXME: split this stuff out into a Script instance control object
@@ -349,7 +354,7 @@
                     stack.push(val);
                 }
                 if(t != null) {
-                    setupTrap(t,val,new 
TrapMarker(this,t,val,tm.pauseOnCascade), tm.trapargs.trapname);
+                    setupTrap(new TrapMarker(this,t,val,tm.pauseOnCascade), 
tm.trapargs.trapname);
                     pc--; // we increment later
                 } else {
                        thread.faction.cascadedTo = tm.trapargs.trapname;
@@ -386,7 +391,7 @@
                 stack.push(val);
                 
                 if(t != null) {
-                    setupTrap(t,val,new TrapMarker(this,t,val,false),null);
+                    setupTrap(new TrapMarker(this,t,val,false),null);
                     pc--; // we increment later
                 } else {
                     target.put(key,val);
@@ -421,7 +426,7 @@
                 if(t != null) {
                        JS f = t.function();
                        if(f instanceof JSFunction){
-                               setupTrap(t,null,new 
TrapMarker(this,t,null,false),null);
+                               setupTrap(new 
TrapMarker(this,t,null,false),null);
                                pc--; // we increment later
                                break;
                        }
@@ -712,9 +717,10 @@
         throw e;
     }
 
-    void setupTrap(JS.Trap t, JS val, TrapMarker cm, JS trapname) throws JSExn 
{
+    void setupTrap(TrapMarker cm, JS trapname) throws JSExn{
+       Trap t = cm.t;
         stack.push(cm);
-        JSArgsTrap ta =new JSArgsTrap(t, val, 
trapname==null?t.key():trapname); 
+        JSArgsTrap ta =new JSArgsTrap(t, cm.val, 
trapname==null?t.key():trapname); 
         cm.trapargs = ta;
         stack.push(ta);
         JSFunction jsfunc = (JSFunction)t.function();
@@ -722,8 +728,15 @@
         scope = jsfunc.parentScope;
         pc = 0;
     }
+    void setupTrapSafe(TrapMarker cm, JS trapname) {
+        try{
+               setupTrap(cm, trapname);
+        }catch(JSExn e){
+               // should never happen, jsexn would be js stack overflow but 
stack is 0 size at this stage
+               throw new Error(e);
+        }
+    }
 
-
     // Markers 
//////////////////////////////////////////////////////////////////////
 
     static class Marker {}

Modified: 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java 
2015-02-25 20:11:16 UTC (rev 4765)
+++ branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java 
2015-02-26 02:07:11 UTC (rev 4766)
@@ -166,10 +166,20 @@
     
     public void runInNew(JS function) throws JSExn { runInNew(function, null, 
null); }
     public void runInNew(JS function, JS[] args, Callable<Object, JS> 
callback) throws JSExn {
-        add(new Thread(this, "background", function, args, true, callback));
+       Thread thread = new Thread(this, "background", function, true, 
callback); 
+               thread.currentInterpreter = new Interpreter(thread, 
(JSFunction)function, true, args);
+       add(thread);
     }
+//    public void runInNew(Trap trap, JS put, Callable<Object, JS> onPut, 
Callable<Object, JS> callback) throws JSExn {
+//     Thread thread = new Thread(this, "background/wtrap", trap.function(), 
true, callback); 
+//     thread.currentInterpreter = new Interpreter(thread, 
(JSFunction)trap.function(), true);
+//     
+//     add(new Thread(this, "background/trap", function, args, true, 
callback));
+//    }
     
     
+    
+    
     /** Execute read traps */
     public JS runInCurrent(Trap t) throws JSExn {
         if (!(t.function() instanceof JSFunction)) {
@@ -180,7 +190,7 @@
         // REMARK - isFirst <-> old == null?
         boolean isFirst = jsthread==null; 
         if (isFirst) {
-            setJSThread(new Thread(this, "current/trap", trapf, null));
+            setJSThread(new Thread(this, "current/trap", trapf));
         }
         Interpreter old = jsthread.currentInterpreter;
         jsthread.currentInterpreter = new Interpreter(jsthread, t, null, 
false, null);
@@ -214,7 +224,7 @@
     /** Execute write traps, part 1 */
     public JS runBeforePut(Trap t, JS val, JS trapname) throws JSExn {
         if (jsthread==null) {
-            setJSThread(new Thread(this, "current/trap/wpause", t.function(), 
null));
+            setJSThread(new Thread(this, "current/trap/wpause", t.function()));
         }
         Interpreter I = new Interpreter(jsthread, t, val, true, trapname);
         // REMARK - this thread is unpausable, so setting this static variable
@@ -270,7 +280,7 @@
     /** Execute read traps, part 1 */
     public JS runBeforeGet(Trap t, JS trapname) throws JSExn {
         if (jsthread==null) {
-            setJSThread(new Thread(this, "current/trap/rpause", t.function(), 
null));
+            setJSThread(new Thread(this, "current/trap/rpause", t.function()));
         }
         Interpreter I = new Interpreter(jsthread, t, null, true, trapname);
         // REMARK - this thread is unpausable, so setting this static variable
@@ -308,7 +318,7 @@
         boolean isFirst = jsthread==null; 
         // FEATURE - reuse the thread object
         if (isFirst) {
-            setJSThread(new Thread(this, "current", function, args));
+            setJSThread(new Thread(this, "current", function));
         }
         Interpreter old = jsthread.currentInterpreter;
         // Always false. Restarting paused nested Interpreters not supported.
@@ -332,7 +342,7 @@
             throw new JSExn("Something a miss"); 
         }
         // FEATURE - reuse the thread object
-        setJSThread(new Thread(this, "non-js", null, null));
+        setJSThread(new Thread(this, "non-js", null));
         // REMARK - This interpreter is just used for its stack for
         // recording non-JS call entries (Box.apply) for backtracing later
         jsthread.currentInterpreter = new Interpreter(jsthread);

Modified: 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Thread.java
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Thread.java    
2015-02-25 20:11:16 UTC (rev 4765)
+++ branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Thread.java    
2015-02-26 02:07:11 UTC (rev 4766)
@@ -22,7 +22,6 @@
 
        final String type;
        final private JSFunction f;           // Root function (id's thread)
-       final private JS[] args;              // Args passed to root function
        final boolean pauseable;              // Is thread pausable or not.
        final Scheduler faction;
        final Callable<Object,JS> callback;   // callback run when thread 
completes
@@ -31,18 +30,18 @@
        
        
        /** Constructor */
-       Thread(Scheduler faction, String name, JS f, JS[] args) { this(faction, 
name, f, args, false, null); }
-       Thread(Scheduler faction, String type, JS f, JS[] args, boolean 
pauseable, Callable<Object,JS> callback)  {
+       Thread(Scheduler faction, String name, JS f) { this(faction, name, f, 
false, null); }
+       Thread(Scheduler faction, String type, JS f, boolean pauseable, 
Callable<Object,JS> callback)  {
                this.type = type;
        this.faction = faction;
            this.f = (JSFunction)f;
        this.pauseable = pauseable;
-       this.args = args==null?Constants.EMPTY_JS_ARRAY:args;
        faction.jsthreads.addElement(this);
        this.callback = callback;
        //Log.info("thread created "+threadCount+"     "+id +"     "+ this);
        }
 
+       
        /** Destructor */
        void destroy() {
            faction.setJSThread(null);
@@ -58,10 +57,7 @@
         * @throws JSExn */
        public Object run(Object o) throws Exception  {
                faction.setJSThread(this);
-               if (currentInterpreter==null) {
-                       //First time this thread has been run (i.e. not paused 
or yielded yet)
-                       currentInterpreter = new Interpreter(this, f, 
this.pauseable, args);
-               }
+
                try {
                        JS ret = this.currentInterpreter.run(o);
                        if(callback!=null && 
this.currentInterpreter.pausecount<=0) {

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


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to