Revision: 3705 http://vexi.svn.sourceforge.net/vexi/?rev=3705&view=rev Author: mkpg2 Date: 2009-10-12 13:41:27 +0000 (Mon, 12 Oct 2009)
Log Message: ----------- Fix. Implemented keysof(clone). 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_junit/test/js/exec/general/clone.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-10-09 17:14:21 UTC (rev 3704) +++ trunk/core/org.ibex.js/src/org/ibex/js/JS.jpp 2009-10-12 13:41:27 UTC (rev 3705) @@ -227,7 +227,8 @@ /** Returns a 'keys object'. */ public JS.Keys keys() throws JSExn; - + public Set keySet() throws JSExn; + /** Return the value associated with the given key. */ public JS get(JS key) throws JSExn; @@ -276,6 +277,7 @@ public JS unclone() { return this; } public JS type() { return SC_immutable; } public Keys keys() throws JSExn { throw new JSExn(type()+" has no key set"); } + public Set keySet() throws JSExn { throw new JSExn(type()+" has no key set"); } public JS get(JS key) throws JSExn { //#noswitch - leave, needed for jsdoc /*...@page(varname=Immutable,humanname=Immutable JS Object) @@ -357,7 +359,49 @@ } // Non-Trap methods forward to the clonee public JS unclone() { return clonee.unclone(); } - public Keys keys() throws JSExn { return keys(); } + public Keys keys() throws JSExn { + final Set ourkeys = keySet(); + final Set allkeys = new HashSet(ourkeys); + JS c = this.clonee; + while(true) { + allkeys.addAll(c.keySet()); + if(c instanceof Clone) c = ((Clone)c).clonee; + else break; + } + + // HACK - not done very well. + // - keys object should not be a snapshot + // - Keys code should be more reuseable. + return new Keys(this) { + public Enumeration iterator() throws JSExn { + return new Enumeration(null) { + private int cur = 0; + private Object[] keys = allkeys.toArray(); + public boolean _hasNext() { return cur < keys.length; } + public JS _next() throws JSExn { + if (cur >= keys.length) { + throw new NoSuchElementException(); + } + return (JS)keys[cur++]; + } + }; + } + protected JS remove(JS key) throws JSExn { + if(ourkeys.contains(key)){ + JS r = Clone.this.get(key); + Clone.this.remove(key); + return r; + }else if(allkeys.contains(key)){ + throw new JSExn("Cannot remove key from clonee: " +key); + } + return null; + } + public boolean contains(JS key) throws JSExn { + return allkeys.contains(key); + } + public int size() throws JSExn { return allkeys.size(); } + }; + } public JS callMethod(JS this_, JS method, JS[] args) throws JSExn { return clonee.callMethod(this_, method, args); } @@ -453,6 +497,7 @@ } return r; } + public Set keySet() throws JSExn { return props.keySet(); } public JS unclone() { return this; } public String[] getFormalArgs() { return EMPTY_STRING_ARRAY; } public int callType(){ return CALLTYPE_METHOD; } @@ -485,7 +530,7 @@ public Enumeration iterator() throws JSExn { return new Enumeration(null) { private int cur = 0; - private Object[] keys = props==null?EMPTY_STRING_ARRAY:props.keySet().toArray(); + private Object[] keys = props==null?EMPTY_JS_ARRAY:props.keySet().toArray(); public boolean _hasNext() { return cur < keys.length; } public JS _next() throws JSExn { if (cur >= keys.length) { @@ -552,7 +597,7 @@ } /** Removes a key and any value associated with it. */ - private void remove(JS key) { + protected void remove(JS key) { if (props==null) return; props.remove(key); } Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp =================================================================== --- trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp 2009-10-09 17:14:21 UTC (rev 3704) +++ trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp 2009-10-12 13:41:27 UTC (rev 3705) @@ -4,6 +4,7 @@ package org.ibex.js; +import java.util.Set; import org.ibex.util.*; /** A JavaScript array implementation */ @@ -24,6 +25,7 @@ public JS type() { return SC_array; } public JS unclone() { return this; } public JS.Keys keys() throws JSExn { return new JSArrayLike.Keys(this); } + public Set keySet() throws JSExn { throw new RuntimeException("!"); } public JS get(JS key) throws JSExn { if (key == null || !JSU.isInt(key)) { Modified: trunk/core/org.ibex.js/src_junit/test/js/exec/general/clone.js =================================================================== --- trunk/core/org.ibex.js/src_junit/test/js/exec/general/clone.js 2009-10-09 17:14:21 UTC (rev 3704) +++ trunk/core/org.ibex.js/src_junit/test/js/exec/general/clone.js 2009-10-12 13:41:27 UTC (rev 3705) @@ -1,6 +1,6 @@ +// MAY split this test up, give cloning its own test suite - var sword = { damage: 4 , image: "sword.png", @@ -17,6 +17,7 @@ }; var magicsword = charm(sword); + assert(magicsword!=sword); assert(magicsword instanceof sword); @@ -24,4 +25,32 @@ assert("sparkle,swing"==magicsword.attack); assert(4==sword.damage); -assert("swing"==sword.attack); \ No newline at end of file +assert("swing"==sword.attack); + +var copy = {}; +for(var k,p in magicsword){ + copy[k] = p; +} +assert(magicsword.damage==copy.damage); +assert(magicsword.image==copy.image); +assert(magicsword.attack==copy.attack); + +var keys = keysof(magicsword); +keys.contains("damage"); +keys.contains("image"); +keys.contains("attack"); + +var exn = false; +try{ + keys.remove("image"); +}catch(e){ + exn = true; +} +assert(exn); +keys.remove("damage"); +assert(4==magicsword.damage); + + + + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn