package org.ibex.js;
import java.io.InputStream;
-import java.util.*;
import org.ibex.util.*;
-import org.ibex.util.Collections;
/** A JavaScript JSArray */
-class JSArray extends ArrayList implements JS, Comparator {
+class JSArray extends Basket.Array implements JS, Basket.CompareFunc {
private static final JS.Method METHOD = new JS.Method();
private static final String[] empty = new String[0];
JSArray(JS arg) { super(1); add(arg); }
public JS unclone() { return this; }
- public JS.Enumeration keys() throws JSExn { return new Enumeration.RandomAccessList(null, this); }
+ public JS.Enumeration keys() throws JSExn {
+ return new Enumeration(null) {
+ private int pos = 0;
+ public boolean _hasNext() { return pos < size(); }
+ public JS _next() { return (JS)JSArray.this.get(pos++); }
+ };
+ }
public JS get(JS key) throws JSExn {
if (key == null || !(key instanceof JSNumber.I)) {
//#switch(Script.str(key))
"arrays only support positive integer keys, can not use: "+Script.str(key));
int i = ((JSNumber.I)key).toInt();
if (i < 0) throw new JSExn("arrays can not use negative integer keys "+i);
- ensureCapacity(i + 1); while (size() < i) add(null);
+ size(i + 1); while (size() < i) add(null);
set(i, val);
}
public InputStream getInputStream() { return null; }
//#switch(Script.str(method))
case "pop": return size() == 0 ? null : (JS)remove(size() - 1);
case "push": addAll(args); return Script.N(size());
- case "reverse": Collections.reverse(this); return this;
+ case "reverse": reverse(); return this;
case "toString": return join(",");
case "shift": return size() == 0 ? null : (JS)remove(0);
case "join": return join(args.length == 0 ? "," : Script.str(args[0]));
public void addAll(JS[] entries) { for (int i=0; i < entries.length; i++) add(entries[i]); }
public void setSize(int newSize) {
- ensureCapacity(newSize);
+ size(newSize);
for (int i=size(); i < newSize; i++) add(null);
for (int i=size() - 1; i >= newSize; i--) remove(i);
}
return ret;
}
+ private static final Basket.CompareFunc defaultSort = new Basket.CompareFunc() {
+ public int compare(Object a, Object b) {
+ try { return Script.toString((JS)a).compareTo(Script.toString((JS)b)); }
+ catch (JSExn e) { throw new JSExn.Wrapper(e); }
+ }
+ };
private JS sort(JS comparator) throws JSExn {
try {
- if (comparator == null) Collections.sort(this);
- else { sort = comparator; Collections.sort(this, this); }
+ if (comparator == null) sort(defaultSort);
+ else { sort = comparator; sort((CompareFunc)this); }
return this;
- } catch (JSExn.Wrapper w) { throw w.unwrap(); }
+ } catch (JSExn.Wrapper w) { throw w.unwrap();
+ } finally { sort = null; }
}
private JS sort = null;
private final JS[] sortargs = new JS[2];
- public int compare(java.lang.Object a, java.lang.Object b) throws JSExn.Wrapper {
+ public int compare(Object a, Object b) throws JSExn.Wrapper {
try {
sortargs[0] = (JS)a; sortargs[1] = (JS)b;
return Script.toInt(sort.call(sortargs));