Search in sources :

Example 36 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) HashSet(java.util.HashSet)

Example 37 with ForeachIterator

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;
    }
}
Also used : DebugTick(org.develnext.jphp.debug.impl.DebugTick) ForeachIterator(php.runtime.lang.ForeachIterator) ContextValueProvider(org.develnext.jphp.debug.impl.command.support.ContextValueProvider) Memory(php.runtime.Memory) Element(org.w3c.dom.Element)

Example 38 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 39 with ForeachIterator

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();
            }
        }
    }));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Stream(php.runtime.ext.core.classes.stream.Stream) IOException(java.io.IOException) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 40 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3