X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJSArray.java;fp=src%2Forg%2Fxwt%2Fjs%2FJSArray.java;h=0000000000000000000000000000000000000000;hb=3591b88b94a6bb378af3d4abe6eb5233ce583104;hp=ca2919dcf2fc83e5651d51c4d066bc0cc9c31123;hpb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;p=org.ibex.core.git diff --git a/src/org/xwt/js/JSArray.java b/src/org/xwt/js/JSArray.java deleted file mode 100644 index ca2919d..0000000 --- a/src/org/xwt/js/JSArray.java +++ /dev/null @@ -1,262 +0,0 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] -package org.xwt.js; - -import org.xwt.util.*; -import java.util.*; - -/** A JavaScript JSArray */ -public class JSArray extends JS { - private static final Object NULL = new Object(); - - public JSArray() { } - public JSArray(int size) { setSize(size); } - - private static int intVal(Object o) { - if (o instanceof Number) { - int intVal = ((Number)o).intValue(); - if (intVal == ((Number)o).doubleValue()) return intVal; - return Integer.MIN_VALUE; - } - if (!(o instanceof String)) return Integer.MIN_VALUE; - String s = (String)o; - for(int i=0; i '9') return Integer.MIN_VALUE; - return Integer.parseInt(s); - } - - public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - //#switch(method) - case "pop": { - int oldSize = size(); - if(oldSize == 0) return null; - return removeElementAt(oldSize-1); - } - case "reverse": return reverse(); - case "toString": return join(","); - case "shift": - if(length() == 0) return null; - return removeElementAt(0); - case "join": - return join(nargs == 0 ? "," : JS.toString(a0)); - case "sort": - return sort(nargs < 1 ? null : a0); - case "slice": - int start = toInt(nargs < 1 ? null : a0); - int end = nargs < 2 ? length() : toInt(a1); - return slice(start, end); - case "push": { - int oldSize = size(); - for(int i=0; i= size()) return null; - return elementAt(i); - } - //#switch(key) - case "pop": return METHOD; - case "reverse": return METHOD; - case "toString": return METHOD; - case "shift": return METHOD; - case "join": return METHOD; - case "sort": return METHOD; - case "slice": return METHOD; - case "push": return METHOD; - case "unshift": return METHOD; - case "splice": return METHOD; - case "length": return N(size()); - //#end - return super.get(key); - } - - public void put(Object key, Object val) throws JSExn { - if (key.equals("length")) setSize(toInt(val)); - int i = intVal(key); - if (i == Integer.MIN_VALUE) - super.put(key, val); - else { - int oldSize = size(); - if(i < oldSize) { - setElementAt(val,i); - } else { - if(i > oldSize) setSize(i); - insertElementAt(val,i); - } - } - } - - public Enumeration keys() { - return new Enumeration() { - int cur = 0; - public boolean hasMoreElements() { return cur >= size(); } - public Object nextElement() { - if (cur >= size()) throw new NoSuchElementException(); - return new Integer(cur++); - } - }; - } - - public final void setSize(int newSize) { - // FEATURE: This could be done a lot more efficiently in BalancedTree - int oldSize = size(); - for(int i=oldSize;i=newSize;i--) removeElementAt(i); - } - - public final int length() { return size(); } - public final Object elementAt(int i) { - if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i); - Object o = getNode(i); - return o == NULL ? null : o; - } - public final void addElement(Object o) { - insertNode(size(),o==null ? NULL : o); - } - public final void setElementAt(Object o, int i) { - if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i); - replaceNode(i,o==null ? NULL : o); - } - public final void insertElementAt(Object o, int i) { - if(i < 0 || i > size()) throw new ArrayIndexOutOfBoundsException(i); - insertNode(i,o==null ? NULL : o); - } - public final Object removeElementAt(int i) { - if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i); - Object o = deleteNode(i); - return o == NULL ? null : o; - } - - public final int size() { return treeSize(); } - public String typeName() { return "array"; } - - private Object join(String sep) { - int length = size(); - if(length == 0) return ""; - StringBuffer sb = new StringBuffer(64); - int i=0; - while(true) { - Object o = elementAt(i); - if(o != null) sb.append(JS.toString(o)); - if(++i == length) break; - sb.append(sep); - } - return sb.toString(); - } - - // FEATURE: Implement this more efficiently - private Object reverse() { - int size = size(); - if(size < 2) return this; - Vec vec = toVec(); - clear(); - for(int i=size-1,j=0;i>=0;i--,j++) insertElementAt(vec.elementAt(i),j); - return this; - } - - private Object slice(int start, int end) { - int length = length(); - if(start < 0) start = length+start; - if(end < 0) end = length+end; - if(start < 0) start = 0; - if(end < 0) end = 0; - if(start > length) start = length; - if(end > length) end = length; - JSArray a = new JSArray(end-start); - for(int i=0;i oldLength) start = oldLength; - if(deleteCount < 0) deleteCount = 0; - if(deleteCount > oldLength-start) deleteCount = oldLength-start; - int newLength = oldLength - deleteCount + newCount; - int lengthChange = newLength - oldLength; - JSArray ret = new JSArray(deleteCount); - for(int i=0;i 0) { - setSize(newLength); - for(int i=newLength-1;i>=start+newCount;i--) - setElementAt(elementAt(i-lengthChange),i); - } else if(lengthChange < 0) { - for(int i=start+newCount;i