use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayFunctions method recursive_count.
private static int recursive_count(Environment env, TraceInfo trace, ArrayMemory array, Set<Integer> used) {
ForeachIterator iterator = array.foreachIterator(false, false);
int size = array.size();
while (iterator.next()) {
Memory el = iterator.getValue();
if (el.isArray()) {
if (used == null)
used = new HashSet<>();
int pointer = el.getPointer();
if (!used.add(pointer)) {
env.warning(trace, "recursion detected");
} else {
size += recursive_count(env, trace, array, used);
}
used.remove(pointer);
}
}
return size;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ContextGetCommand method run.
@Override
public void run(Debugger context, CommandArguments args, Document result) {
Element response = createResponse(args, result);
String contextId = args.get("c");
response.setAttribute("context", contextId);
ContextValueProvider contextValueProvider = new ContextValueProvider(context, result);
switch(contextId) {
case "0":
DebugTick tick = context.getRegisteredTick();
ForeachIterator iterator = tick.getLocals().foreachIterator(true, false);
while (iterator.next()) {
Memory value = iterator.getValue().toValue();
if (value.isUndefined()) {
continue;
}
response.appendChild(contextValueProvider.getProperty(iterator.getKey().toString(), value));
}
break;
}
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method has.
@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "value"), @Arg(value = "strict", optional = @Optional("false")) })
public static Memory has(Environment env, Memory... args) {
ForeachIterator iterator = args[0].getNewIterator(env);
if (iterator == null) {
return Memory.NULL;
}
Memory needle = args[1];
boolean strict = args[2].toBoolean();
while (iterator.next()) {
if (strict) {
if (needle.identical(iterator.getValue()))
return Memory.TRUE;
} else {
if (needle.equal(iterator.getValue()))
return Memory.TRUE;
}
}
return Memory.FALSE;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method ofStream.
@FastMethod
@Signature({ @Arg(value = "stream", typeClass = Stream.CLASS_NAME), @Arg(value = "chunkSize", optional = @Optional("1")) })
public static Memory ofStream(final Environment env, Memory... args) {
final Stream stream = args[0].toObject(Stream.class);
final int chunkSize = args[1].toInteger() < 1 ? 1 : args[1].toInteger();
return new ObjectMemory(new WrapFlow(env, new ForeachIterator(false, false, false) {
protected boolean eof() {
env.pushCall(stream, "eof");
try {
return stream.eof(env).toBoolean();
} finally {
env.popCall();
}
}
@Override
protected boolean init() {
currentKey = Memory.CONST_INT_M1;
return !eof();
}
@Override
protected boolean nextValue() {
if (eof())
return false;
env.pushCall(stream, "read", LongMemory.valueOf(chunkSize));
try {
currentValue = stream.read(env, LongMemory.valueOf(chunkSize));
currentKey = ((LongMemory) currentKey).inc();
} catch (IOException e) {
env.catchUncaught(e);
} finally {
env.popCall();
}
return true;
}
@Override
protected boolean prevValue() {
return false;
}
@Override
public void reset() {
currentKey = Memory.CONST_INT_M1;
env.pushCall(stream, "seek", Memory.CONST_INT_0);
try {
stream.seek(env, Memory.CONST_INT_0);
} catch (IOException e) {
env.catchUncaught(e);
} finally {
env.popCall();
}
}
}));
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method reduce.
@Signature(@Arg(value = "callback", type = HintType.CALLABLE))
public Memory reduce(Environment env, Memory... args) {
Invoker invoker = Invoker.valueOf(env, null, args[0]);
ForeachIterator iterator = getSelfIterator(env);
Memory r = Memory.NULL;
int argCount = invoker.getArgumentCount();
while (iterator.next()) {
if (argCount < 3)
r = invoker.callNoThrow(r, iterator.getValue());
else
r = invoker.callNoThrow(r, iterator.getValue(), iterator.getMemoryKey());
}
return r;
}
Aggregations