X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSArray.java;h=e8529c6d1b9b215eb8231a0a87f0afceb8ddffd0;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=8e2fdd2cbcc645e63e4e7f0cc09119e163f1bf8b;hpb=a1e6b7e9307319c0195b0efbe5e5354c128be481;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSArray.java b/src/org/ibex/js/JSArray.java index 8e2fdd2..e8529c6 100644 --- a/src/org/ibex/js/JSArray.java +++ b/src/org/ibex/js/JSArray.java @@ -8,14 +8,14 @@ import java.io.InputStream; import org.ibex.util.*; /** A JavaScript JSArray */ -class JSArray extends Basket.Array implements JS, Basket.CompareFunc { +public 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() { } - JSArray(int size) { super(size); } - JSArray(JS[] args) { super(args.length); addAll(args); } - JSArray(JS arg) { super(1); add(arg); } + public JSArray() { } + public JSArray(int size) { super(size); } + public JSArray(JS[] args) { super(args.length); addAll(args); } + public JSArray(JS arg) { super(1); add(arg); } public JS unclone() { return this; } public JS.Enumeration keys() throws JSExn { @@ -51,23 +51,14 @@ class JSArray extends Basket.Array implements JS, Basket.CompareFunc { "arrays only support positive integer keys, can not use: "+JSU.str(key)); int i = ((JSNumber.I)key).toInt(); if (i < 0) throw new JSExn("arrays can not use negative integer keys "+i); - size(i + 1); while (size() < i) add(null); + if (size() < i+1) size(i + 1); + //while (size() < i) add(null); set(i, val); } - public InputStream getInputStream() { return null; } public String[] getFormalArgs() { return empty; } - public String coerceToString() { return "array"; } // FIXME + public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); } - public boolean hasKey(JS key) { - if (key == null || !(key instanceof JSNumber.I)) return false; - int i = ((JSNumber.I)key).toInt(); - return 0 <= i && i < size(); - } - - public Object run(Object o) throws JSExn { return call(null); } - public void pause() throws NotPausableException { throw new NotPausableException(); } - public JS call(JS[] args) throws JSExn { throw new JSExn("can not call an array as a function"); } public JS call(JS method, JS[] args) throws JSExn { //#switch(JSU.str(method)) case "pop": return size() == 0 ? null : (JS)remove(size() - 1); @@ -86,7 +77,6 @@ class JSArray extends Basket.Array implements JS, Basket.CompareFunc { return JSU.N(size()); case "splice": return splice(args); //#end - throw new JSExn("arrays have no function: "+JSU.str(method)); } @@ -96,9 +86,9 @@ class JSArray extends Basket.Array implements JS, Basket.CompareFunc { public void addTrap(JS k, JS f) throws JSExn { throw new JSExn("arrays do not support traps"); } public void delTrap(JS k, JS f) throws JSExn { throw new JSExn("arrays do not support traps"); } - public JS.Trap getTrap(JS k) throws JSExn { throw new JSExn("arrays do not support traps"); } + public JS.Trap getTrap(JS k) throws JSExn { return null; } - /** FIXME: move to specialised ArrayStore superclass. */ + /** FEATURE: move to specialised ArrayStore superclass. */ public void addAll(JS[] entries) { for (int i=0; i < entries.length; i++) add(entries[i]); } public void setSize(int newSize) { @@ -133,7 +123,7 @@ class JSArray extends Basket.Array implements JS, Basket.CompareFunc { if(start > length) start = length; if(end > length) end = length; JSArray a = new JSArray(end-start); - for(int i=0;i 0) { setSize(newLength); @@ -188,7 +178,7 @@ class JSArray extends Basket.Array implements JS, Basket.CompareFunc { public int compare(Object a, Object b) throws JSExn.Wrapper { try { sortargs[0] = (JS)a; sortargs[1] = (JS)b; - return JSU.toInt(sort.call(sortargs)); + return JSU.toInt(sort.call(null, sortargs)); } catch (JSExn e) { throw new JSExn.Wrapper(e); } finally { sortargs[0] = null; sortargs[1] = null; } }