ESBase slice(Call eval, int length) throws Throwable { ESObject obj = eval.getArg(-1).toObject(); ESBase lenObj = obj.getProperty(LENGTH); int len = lenObj.toInt32(); ESArray array = Global.getGlobalProto().createArray(); if (len <= 0) return array; int start = 0; if (length > 0) start = eval.getArg(0).toInt32(); if (start < 0) start += len; if (start < 0) start = 0; if (start > len) return array; int end = len; if (length > 1) end = eval.getArg(1).toInt32(); if (end < 0) end += len; if (end < 0) return array; if (end > len) end = len; if (start >= end) return array; for (int i = 0; i < end - start; i++) { ESBase value = obj.hasProperty(start + i); if (value != null) array.setProperty(i, value); } array.setProperty(LENGTH, ESNumber.create(end - start)); return array; }
/** Creates the native Array object */ static ESObject create(Global resin) { Native nativeArray = new NativeArray("Array", NEW, 1); ESArray proto = new ESArray(); proto.prototype = resin.objProto; NativeWrapper array = new NativeWrapper(resin, nativeArray, proto, ESThunk.ARRAY_THUNK); resin.arrayProto = proto; put(proto, "join", JOIN, 1); put(proto, "toString", TO_STRING, 0); put(proto, "reverse", REVERSE, 0); put(proto, "sort", SORT, 0); // js1.2 put(proto, "concat", CONCAT, 0); put(proto, "pop", POP, 0); put(proto, "push", PUSH, 0); put(proto, "shift", SHIFT, 0); put(proto, "unshift", UNSHIFT, 0); put(proto, "slice", SLICE, 2); put(proto, "splice", SPLICE, 0); proto.setClean(); array.setClean(); return array; }
ESBase concat(Call eval, int length) throws Throwable { ESArray array = Global.getGlobalProto().createArray(); int k = 0; for (int i = -1; i < length; i++) { ESBase arg = eval.getArg(i); if (arg == esNull || arg == esUndefined || arg == esEmpty) continue; ESBase arglen = arg.hasProperty(LENGTH); if (arglen == null) { array.setProperty(k++, arg); continue; } int len = (int) arglen.toInt32(); if (len < 0) { array.setProperty(k++, arg); continue; } for (int j = 0; j < len; j++) { ESBase obj = arg.hasProperty(j); if (obj != null) array.setProperty(k, obj); k++; } } array.setProperty(LENGTH, ESNumber.create(k)); return array; }