Revision: 4400
          http://vexi.svn.sourceforge.net/vexi/?rev=4400&view=rev
Author:   clrg
Date:     2012-06-01 17:18:44 +0000 (Fri, 01 Jun 2012)
Log Message:
-----------
Add vexi.js.copy()

Modified Paths:
--------------
    trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JS.jpp
    trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSArray.jpp
    trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSPrimitive.jpp
    trunk/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp

Added Paths:
-----------
    trunk/org.vexi-library.js/src/test/java/test/js/exec/object/
    trunk/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java
    trunk/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js

Modified: trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JS.jpp
===================================================================
--- trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JS.jpp   2012-06-01 
03:44:17 UTC (rev 4399)
+++ trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JS.jpp   2012-06-01 
17:18:44 UTC (rev 4400)
@@ -378,8 +378,8 @@
         public Trap getTrap(JS key) { return null; }
         
         public JS type() { return SC_immutable; }
-        public boolean isTruthy(){ return true; }        
-        public boolean instanceOf(JS constructor){ return false; } 
+        public boolean isTruthy() { return true; }
+        public boolean instanceOf(JS constructor) { return false; } 
         public void addConstructor(JS constructor) throws JSExn {
             throw new JSExn("Attemted to add constructor to immutable");
         }
@@ -402,6 +402,7 @@
         // need to act on the clone not the clonee. Thus the 
         // requirement to pass in the target.
         public JS deepcopy() throws JSExn;
+        public JS copy() throws JSExn;
     }
     
     public class Constructor extends JS.Immutable {
@@ -559,6 +560,28 @@
         //    return r;
         //}
         
+        public JS copy() throws JSExn {
+            if (getClass()!=Obj.class) {
+                throw new JSExn("copy not supported for: 
"+getClass().getName());
+            }
+            JS.Obj r = new JS.Obj();
+            if (traps!=null && traps.size()>0) {
+               Iterator I = traps.keySet().iterator();
+               while (I.hasNext()) {
+                       JS key = (JS)I.next();
+                       Trap t = (Trap) traps.get(key);
+                       r.addTrap(key, (JS)t);
+               }
+            }
+            Enumeration E = keys().iterator();
+            while (E.hasNext()) {
+                JS key = E.next();
+                JS val = get(key);
+                r.put(key,val);
+            }
+            return r;
+        }
+        
         public JS deepcopy() throws JSExn {
             if (getClass()!=Obj.class) {
                 throw new JSExn("deepcopy not supported for: 
"+getClass().getName());

Modified: trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSArray.jpp
===================================================================
--- trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSArray.jpp      
2012-06-01 03:44:17 UTC (rev 4399)
+++ trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSArray.jpp      
2012-06-01 17:18:44 UTC (rev 4400)
@@ -355,7 +355,7 @@
         }
     }
     
-    private JSArray copy() {
+    public JS copy() {
         JSArray r = new JSArray(0);
         r.size = size;
         r.o = new Object[size];

Modified: trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSPrimitive.jpp
===================================================================
--- trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSPrimitive.jpp  
2012-06-01 03:44:17 UTC (rev 4399)
+++ trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSPrimitive.jpp  
2012-06-01 17:18:44 UTC (rev 4400)
@@ -8,6 +8,7 @@
 
     // REMARK primitives are immutable
     public JS deepcopy() { return this; }
+    public JS copy() { return this; }
     
     public JS callMethod(JS this_, JS method, JS[] args) throws JSExn {
         String s = coerceToString();

Modified: trunk/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp
===================================================================
--- trunk/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp       
2012-06-01 03:44:17 UTC (rev 4399)
+++ trunk/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp       
2012-06-01 17:18:44 UTC (rev 4400)
@@ -573,6 +573,14 @@
              * */
             case "deepcopy": return METHOD;
             
+            /* <p>Creates a copy of the input, containing the same references 
to arrays/objects
+             * as the original.</p>
+             * @method
+             * @param(name=js)
+             * @return(<i>varies</i>)
+             * */
+            case "copy": return METHOD;
+            
             /* <p>Evaluates the given string as JS.</p>
              * 
              * <p><b>Hint:</b> if you want the evaluated JS to return its 
result then
@@ -622,6 +630,7 @@
                 case "hashOf": return 
args[0]==null?null:JSU.N(args[0].hashCode());
                 case "stringify": return JSON.marshal(args[0]);
                 case "deepcopy": return JSU.deepcopy(args[0]);
+                case "copy": return JSU.copy(args[0]);
                 //#end
             }
             }

Added: 
trunk/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java
===================================================================
--- trunk/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java 
                        (rev 0)
+++ trunk/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java 
2012-06-01 17:18:44 UTC (rev 4400)
@@ -0,0 +1,21 @@
+package test.js.exec.object;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import test.js.exec.JSTestSuite;
+
+/**
+ * @author mike
+ */
+public class TestObject{
+
+    public static Test suite() {
+       return JSTestSuite.suite(TestObject.class);
+    }
+    
+    public static void main(String[] args) throws Throwable {
+       JSTestSuite jts = new JSTestSuite(TestObject.class);
+       TestCase t = jts.createTestCase(jts.getResourceDirs(), "copy.js");
+       t.runBare();
+       }
+}


Property changes on: 
trunk/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: trunk/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js
===================================================================
--- trunk/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js         
                (rev 0)
+++ trunk/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js 
2012-06-01 17:18:44 UTC (rev 4400)
@@ -0,0 +1,8 @@
+
+var aref = { };
+var orig = { key1:123, foo:"bar", ref:aref };
+var copy = orig.copy();
+assert(copy != orig);
+assert(copy.key == 123);
+assert(copy.bar == "bar");
+assert(copy.ref == aref);

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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to