Search in sources :

Example 71 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method _asort_impl.

protected static boolean _asort_impl(Environment env, TraceInfo trace, @Reference Memory array, Comparator comparator) {
    if (expecting(env, trace, 1, array, ARRAY)) {
        ArrayMemory arrayMemory = array.toValue(ArrayMemory.class);
        Memory[] values = new Memory[arrayMemory.size()];
        ForeachIterator iterator = arrayMemory.getNewIterator(env);
        int i = 0;
        while (iterator.next()) {
            values[i++] = new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue());
        }
        arrayMemory.clear();
        try {
            Arrays.sort(values, comparator);
        } catch (IllegalArgumentException e) {
            return false;
        }
        for (Memory value : values) {
            arrayMemory.add(value);
        }
        return true;
    } else {
        return false;
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 72 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method _array_walk_recursive.

public static boolean _array_walk_recursive(Environment env, TraceInfo trace, Memory input, Invoker invoker, Memory userData, Set<Integer> used) throws Throwable {
    if (used == null)
        used = new HashSet<Integer>();
    ForeachIterator iterator = input.getNewIterator(env, true, false);
    while (iterator.next()) {
        Memory item = iterator.getValue();
        if (item.isArray()) {
            if (used.add(item.getPointer())) {
                boolean result = _array_walk_recursive(env, trace, item, invoker, userData, used);
                used.remove(item.getPointer());
                if (!result)
                    return false;
            } else {
                env.warning(trace, "array_walk_recursive(): recursion detected");
            }
        } else {
            Memory key = iterator.getMemoryKey();
            invoker.call(item, key, userData);
        }
    }
    return true;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 73 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class UISlider method __setLabelTable.

@Signature(@Arg(value = "value", type = HintType.ARRAY))
protected Memory __setLabelTable(Environment env, Memory... args) {
    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
    ForeachIterator iterator = args[0].getNewIterator(env, false, false);
    while (iterator.next()) {
        int index = iterator.getMemoryKey().toInteger();
        table.put(index, new JLabel(iterator.getValue().toString()));
    }
    component.setLabelTable(table);
    return Memory.NULL;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Hashtable(java.util.Hashtable)

Example 74 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_combine.

public static Memory array_combine(Environment env, TraceInfo trace, Memory keys, Memory values) {
    if (expecting(env, trace, 1, keys, ARRAY) && expecting(env, trace, 2, values, ARRAY)) {
        ArrayMemory _keys = keys.toValue(ArrayMemory.class);
        ArrayMemory _values = values.toValue(ArrayMemory.class);
        int size1 = _keys.size();
        int size2 = _values.size();
        if (size1 != size2) {
            env.warning(trace, "array_combine(): Both parameters should have an equal number of elements");
            return Memory.FALSE;
        }
        ArrayMemory result = new ArrayMemory();
        if (size1 == 0)
            return result.toConstant();
        ForeachIterator iteratorKeys = _keys.getNewIterator(env, false, false);
        ForeachIterator iteratorValues = _values.getNewIterator(env, false, false);
        while (iteratorKeys.next()) {
            iteratorValues.next();
            result.refOfIndex(iteratorKeys.getValue()).assign(iteratorValues.getValue().toImmutable());
        }
        return result.toConstant();
    } else
        return Memory.FALSE;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 75 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_fill_keys.

public static Memory array_fill_keys(Environment env, TraceInfo trace, Memory keys, Memory value) {
    if (expecting(env, trace, 1, keys, ARRAY)) {
        ForeachIterator iterator = keys.getNewIterator(env);
        ArrayMemory result = new ArrayMemory();
        while (iterator.next()) {
            result.refOfIndex(iterator.getValue()).assign(value.toImmutable());
        }
        return result.toConstant();
    } else {
        return new ArrayMemory().toConstant();
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

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