Revision: 3503
          http://vexi.svn.sourceforge.net/vexi/?rev=3503&view=rev
Author:   mkpg2
Date:     2009-05-20 10:11:51 +0000 (Wed, 20 May 2009)

Log Message:
-----------
Feature. vexi.js.deepcopy()

Modified Paths:
--------------
    trunk/core/org.ibex.js/src/org/ibex/js/JS.jpp
    trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp
    trunk/core/org.ibex.js/src/org/ibex/js/JSPrimitive.jpp
    trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp
    trunk/core/org.ibex.js/src/org/vexi/js/VexiJS.jpp
    trunk/core/org.ibex.js/src_junit/test/js/exec/general/TestGeneral.java

Added Paths:
-----------
    trunk/core/org.ibex.js/src_junit/test/js/exec/general/deepcopy.js

Modified: trunk/core/org.ibex.js/src/org/ibex/js/JS.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JS.jpp       2009-05-19 22:53:56 UTC 
(rev 3502)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JS.jpp       2009-05-20 10:11:51 UTC 
(rev 3503)
@@ -125,6 +125,12 @@
         // need to act on the clone not the clonee. Thus the 
         // requirement to pass in the target.
     }
+    public interface Copyable extends JS {
+        // REMARK - semantically certain methods (read vexi.bless)
+        // need to act on the clone not the clonee. Thus the 
+        // requirement to pass in the target.
+       public JS deepcopy() throws JSExn;
+    }
     
     public class Constructor extends JS.Immutable {
        final String name;
@@ -171,7 +177,7 @@
     
     /** Standard JS object implementation that can store arbitrarily named 
properties
      *  and can have traps dynamically placed upon properties. */
-    public static class Obj implements Cloneable {
+    public static class Obj implements Cloneable, Copyable {
        static public JS.Constructor Constructor = new JS.Constructor("Object"){
                public JS new_(JS[] args) throws JSExn { 
                        return new JS.Obj();
@@ -210,7 +216,18 @@
 //             return r;
 //        }    
         
-        
+        public JS deepcopy() throws JSExn {
+               if(getClass()!=Obj.class) throw new JSExn("deepcopy not 
supported for: "+getClass().getName());
+               if(traps!=null && traps.size()>0) throw new JSExn("cannot 
deepcopy object with traps");
+               JS.Obj r = new JS.Obj();
+               Enumeration E = keys().iterator();
+               while(E.hasNext()){
+                       JS key = E.next();
+                       JS val = get(key);
+                       r.put(key,JSU.deepcopy(val));
+               }
+               return r;
+        }
         public JS unclone() { return this; }
         public String[] getFormalArgs() { return EMPTY_STRING_ARRAY; }
         public int callType(){ return CALLTYPE_METHOD; }

Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp  2009-05-19 22:53:56 UTC 
(rev 3502)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp  2009-05-20 10:11:51 UTC 
(rev 3503)
@@ -7,7 +7,7 @@
 import org.ibex.util.*; 
 
 /** A JavaScript array implementation */
-public class JSArray extends Basket.Array implements Basket.CompareFunc, 
Constants, JSArrayLike {
+public class JSArray extends Basket.Array implements Basket.CompareFunc, 
Constants, JSArrayLike, JS.Copyable {
 
        static public JS Constructor = new JS.Constructor("Array"){
                public JS new_(JS[] args) throws JSExn { 
@@ -218,9 +218,16 @@
        r.size = size;
         r.o = new Object[size];
         System.arraycopy(o, 0, r.o, 0, size);
-        if (r.size>r.o.length) throw new Error("Size and o.length mismatch: 
"+size+">"+o.length);
-       return r;
+        return r;
     }
+    public JS deepcopy() throws JSExn {
+       JSArray r = new JSArray(0);
+       r.size = size;
+        r.o = new Object[size];
+       for(int i=0; i<size(); i++)
+               r.o[i] = JSU.deepcopy((JS)o[i]);
+        return r;
+    }
     
     public JS getElement(int i) throws JSExn { return (JS) get(i); }
     public int length() { return size(); }

Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSPrimitive.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSPrimitive.jpp      2009-05-19 
22:53:56 UTC (rev 3502)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSPrimitive.jpp      2009-05-20 
10:11:51 UTC (rev 3503)
@@ -4,8 +4,11 @@
 
 package org.ibex.js;
 
-public class JSPrimitive extends JS.Immutable implements Constants{
+public class JSPrimitive extends JS.Immutable implements Constants, 
JS.Copyable{
 
+       // REMARK primitives are immutable
+       public JS deepcopy() { return this; }
+       
        public JS callMethod(JS this_, JS method, JS[] args) throws JSExn {
         String s = coerceToString();
         int slength = s.length();

Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp      2009-05-19 22:53:56 UTC 
(rev 3502)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp      2009-05-20 10:11:51 UTC 
(rev 3503)
@@ -17,6 +17,14 @@
     }
        
 
+    public static JS deepcopy(JS obj) throws JSExn{
+       if(obj==null) return obj;
+       else if(obj instanceof JS.Copyable){
+               return ((JS.Copyable)obj).deepcopy();
+       }else{
+               throw new JSExn("deepcopy not supported for: 
"+obj.getClass().getName());
+       }
+    }
     ////////////////////
     // CLONE stuff  
     

Modified: trunk/core/org.ibex.js/src/org/vexi/js/VexiJS.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/vexi/js/VexiJS.jpp   2009-05-19 22:53:56 UTC 
(rev 3502)
+++ trunk/core/org.ibex.js/src/org/vexi/js/VexiJS.jpp   2009-05-20 10:11:51 UTC 
(rev 3503)
@@ -234,7 +234,8 @@
                case "constructorsOf": return METHOD;
                case "stringify": return METHOD;
                case "eval": return METHOD;
-            //#end
+               case "deepcopy": return METHOD;
+               //#end
             return super.get(key);
         }
         public JS callMethod(JS this_, JS method, JS[] args) throws JSExn {
@@ -247,6 +248,7 @@
                //#switch(JSU.toString(method))
                case "constructorsOf": return JSU.constructorsOf(args[0]);
                case "stringify": return JSON.marshal(args[0]);
+               case "deepcopy": return JSU.deepcopy(args[0]);
                    //#end
             }
                }

Modified: trunk/core/org.ibex.js/src_junit/test/js/exec/general/TestGeneral.java
===================================================================
--- trunk/core/org.ibex.js/src_junit/test/js/exec/general/TestGeneral.java      
2009-05-19 22:53:56 UTC (rev 3502)
+++ trunk/core/org.ibex.js/src_junit/test/js/exec/general/TestGeneral.java      
2009-05-20 10:11:51 UTC (rev 3503)
@@ -15,7 +15,7 @@
     
     public static void main(String[] args) throws Throwable {
        JSTestSuite jts = new JSTestSuite(TestGeneral.class);
-       TestCase t = jts.createTestCase(jts.getResourceDirs(), "new.js");
+       TestCase t = jts.createTestCase(jts.getResourceDirs(), "deepcopy.js");
        t.runBare();
     }
 }

Added: trunk/core/org.ibex.js/src_junit/test/js/exec/general/deepcopy.js
===================================================================
--- trunk/core/org.ibex.js/src_junit/test/js/exec/general/deepcopy.js           
                (rev 0)
+++ trunk/core/org.ibex.js/src_junit/test/js/exec/general/deepcopy.js   
2009-05-20 10:11:51 UTC (rev 3503)
@@ -0,0 +1,8 @@
+//////
+sys.import("shared");
+
+var x1 = {a:"1",b:"2",c:"3",arr:[2,3]};
+var x2 = sys.js.deepcopy(x1);
+
+assertObjEquals(x1,x2);
+sys.trace(x2);


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

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to