ESBase unshift(Call eval, int length) throws Throwable { ESObject obj = eval.getArg(-1).toObject(); ESBase lenObj = obj.getProperty(LENGTH); int len = lenObj.toInt32(); if (len < 0) len = 0; if (length == 0) return ESNumber.create(0); for (int i = len - 1; i >= 0; i--) { ESBase value = obj.getProperty(i); if (value == null) obj.delete(ESString.create(length + i)); else obj.setProperty(length + i, value); } for (int i = 0; i < length; i++) { ESBase value = eval.getArg(i); if (value == null) obj.delete(ESString.create(i)); else obj.setProperty(i, value); } ESNumber numLen = ESNumber.create(len + length); obj.setProperty(LENGTH, numLen); return numLen; }
ESBase splice(Call eval, int length) throws Throwable { if (length < 2) return esUndefined; ESObject obj = eval.getArg(-1).toObject(); int index = eval.getArg(0).toInt32(); int count = eval.getArg(1).toInt32(); boolean single = count == 1; ESBase lenObj = obj.getProperty(LENGTH); int len = lenObj.toInt32(); if (index < 0) index += len; if (index < 0) index = 0; if (count < 0) count = 0; if (index + count > len) count = len - index; ESBase value; if (count < 1) value = esUndefined; else { value = Global.getGlobalProto().createArray(); for (int i = 0; i < count; i++) value.setProperty(i, obj.getProperty(index + i)); } int delta = length - 2 - count; if (delta < 0) { for (int i = 0; i < len - count; i++) { ESBase temp = obj.getProperty(i + index + count); if (temp == null) obj.delete(ESString.create(i + index + count + delta)); else obj.setProperty(i + index + count + delta, temp); } } else if (delta > 0) { for (int i = len - count - 1; i >= 0; i--) { ESBase temp = obj.getProperty(i + index + count); if (temp == null) obj.delete(ESString.create(i + index + count + delta)); else obj.setProperty(i + index + count + delta, temp); } } for (int i = 0; i < length - 2; i++) obj.setProperty(i + index, eval.getArg(i + 2)); obj.setProperty(LENGTH, ESNumber.create(len - count + length - 2)); return value; }
static ESBase join(ESObject array, String separator) throws Throwable { if (array.mark != 0) { return ESString.create("..."); } array.mark = -1; try { int len = array.getProperty(LENGTH).toInt32(); StringBuffer sbuf = new StringBuffer(); for (int i = 0; i < len; i++) { if (i != 0) sbuf.append(separator); ESBase value = array.hasProperty(i); if (value != null && value != esNull && value != esUndefined) sbuf.append(value.toString()); } return ESString.create(sbuf.toString()); } finally { array.mark = 0; } }
ESBase shift(Call eval, int length) throws Throwable { ESObject obj = eval.getArg(-1).toObject(); ESBase lenObj = obj.hasProperty(LENGTH); int len; if (lenObj == null || (len = (int) lenObj.toInt32()) <= 0) return esUndefined; ESBase value = obj.getProperty(0); for (int i = 1; i < len; i++) { ESBase temp = obj.hasProperty(i); if (temp == null) obj.delete(ESString.create(i - 1)); else obj.setProperty(i - 1, temp); } obj.setProperty(LENGTH, ESNumber.create(len - 1)); return value; }
public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable { CharBuffer cb = new CharBuffer(); Global resin = Global.getGlobalProto(); int mark = map.get(this); if (mark > 0 && isLoopPass) return null; else if (mark > 0) { cb.append("#" + mark + "="); map.put(this, -mark); } else if (mark == 0 && isLoopPass) { map.put(this, resin.addMark()); return null; } else if (mark < 0 && !isLoopPass) { return ESString.create("#" + -mark + "#"); } cb.append("{"); if (isLoopPass) map.put(this, 0); Iterator e = keys(); boolean isFirst = true; while (e.hasNext()) { if (!isFirst) cb.append(", "); isFirst = false; ESString key = (ESString) e.next(); cb.append(key); cb.append(":"); ESBase value = getProperty(key); if (isLoopPass) value.toSource(map, isLoopPass); else cb.append(value.toSource(map, isLoopPass)); } cb.append("}"); return new ESString(cb.toString()); }
public ESBase typeof() throws ESException { return ESString.create("object"); }
/** Sets the named property */ public void put(int i, ESBase value, int flags) { put(ESString.create(i), value, flags); }
/** returns the string representation */ public ESString toStr() throws ESException { return ESString.create("[global]"); }
/** Returns the string representation of the type. */ public ESBase typeof() throws ESException { return ESString.create("function"); }