Search in sources :

Example 21 with ForeachIterator

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

the class ArrayMemory method unshift.

public void unshift(Memory value, int count) {
    checkCopied();
    if (size == 0) {
        for (int i = 0; i < count; i++) add(value);
    } else {
        if (list != null) {
            List<ReferenceMemory> tmp = new ArrayList<ReferenceMemory>();
            for (int i = 0; i < count; i++) tmp.add(new ReferenceMemory(value));
            list.addAll(0, tmp);
            size = list.size();
        } else {
            ArrayMemory tmp = new ArrayMemory();
            tmp.convertToMap();
            for (int i = 0; i < count; i++) tmp.add(value);
            ForeachIterator iterator = getNewIterator(null, false, false);
            while (iterator.next()) {
                Object key = iterator.getKey();
                if (key instanceof String)
                    tmp.put(key, iterator.getValue());
                else
                    add(iterator.getValue());
            }
            lastLongIndex = tmp.lastLongIndex;
            map = tmp.map;
            size = tmp.size;
        }
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject)

Example 22 with ForeachIterator

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

the class ArrayMemory method compare.

public int compare(ArrayMemory otherRef, boolean strict, Set<Integer> used) {
    int size1 = size(), size2 = otherRef.size();
    if (size1 < size2)
        return -1;
    else if (size1 > size2)
        return 1;
    ForeachIterator iterator = this.foreachIterator(false, false);
    ForeachIterator iterator2 = null;
    if (strict)
        iterator2 = otherRef.foreachIterator(false, false);
    if (used == null)
        used = new HashSet<Integer>();
    while (iterator.next()) {
        Memory value1 = iterator.getValue();
        Memory key = iterator.getMemoryKey();
        Memory value2;
        if (iterator2 == null)
            value2 = otherRef.get(key);
        else {
            if (!iterator2.next())
                return -2;
            Object key2 = iterator2.getKey();
            if (!iterator.getKey().equals(key2))
                return -2;
            value2 = iterator2.getValue();
        }
        if (value2 == null)
            return -2;
        if (value1.isArray() && value2.isArray()) {
            ArrayMemory arr1 = value1.toValue(ArrayMemory.class);
            if (used.add(value2.getPointer())) {
                int r = arr1.compare(value2.toValue(ArrayMemory.class), strict, used);
                if (r == 0) {
                    used.remove(value2.getPointer());
                    continue;
                }
                return r;
            }
            used.remove(value2.getPointer());
        } else if (value1.isObject() && value2.isObject()) {
            ObjectMemory o1 = value1.toValue(ObjectMemory.class);
            ObjectMemory o2 = value2.toValue(ObjectMemory.class);
            return o1.compare(o2.value, strict, used);
        } else {
            if ((strict && value1.identical(value2)) || (!strict && value1.equal(value2)))
                continue;
            if (value1.smaller(value2))
                return -1;
            else
                return 1;
        }
    }
    return 0;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ShortcutMemory(php.runtime.memory.helper.ShortcutMemory) Memory(php.runtime.Memory) ArrayKeyMemory(php.runtime.memory.helper.ArrayKeyMemory) ArrayValueMemory(php.runtime.memory.helper.ArrayValueMemory) IObject(php.runtime.lang.IObject)

Example 23 with ForeachIterator

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

the class PrintR method printArray.

@Override
protected void printArray(ArrayMemory value, int level, Set<Integer> used) {
    writeArrayHeader();
    if (used.contains(value.getPointer())) {
        printer.write(" *RECURSION*");
    } else {
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeOpen();
        level += 1;
        used.add(value.getPointer());
        ForeachIterator iterator = value.foreachIterator(false, false);
        int i = 0;
        int size = value.size();
        while (iterator.next()) {
            Memory el = iterator.getValue();
            if (el == Memory.UNDEFINED)
                continue;
            printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
            Memory key = iterator.getMemoryKey();
            printer.write('[');
            printer.write(key.toString());
            printer.write("] => ");
            print(el, level + 1, used);
            writeSeparator(i == size - 1);
            i++;
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeClose();
        used.remove(value.getPointer());
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory)

Example 24 with ForeachIterator

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

the class VarDump method printObject.

@Override
protected void printObject(ObjectMemory value, int level, Set<Integer> used) {
    ClassEntity classEntity = value.getReflection();
    if (used.contains(value.getPointer())) {
        printer.write("*RECURSION*\n");
    } else {
        ArrayMemory arr;
        if (classEntity.methodMagicDebugInfo != null) {
            try {
                Memory tmp = env.invokeMethod(value.value, classEntity.methodMagicDebugInfo.getName());
                if (tmp.isArray()) {
                    arr = tmp.toValue(ArrayMemory.class);
                } else {
                    arr = new ArrayMemory();
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        } else
            arr = value.getProperties();
        printer.write("object(");
        printer.write(classEntity.getName());
        printer.write(")#" + value.getPointer());
        printer.write(" (" + arr.size() + ") {\n");
        level += 1;
        used.add(value.getPointer());
        if (classEntity.methodMagicDebugInfo == null) {
            for (PropertyEntity entity : classEntity.getProperties()) {
                if (entity.getGetter() != null && !entity.isHiddenInDebugInfo()) {
                    printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
                    printer.write("[\"");
                    printer.write(entity.getName());
                    printer.write('"');
                    printer.write(":getter]=>\n");
                    printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
                    try {
                        print(entity.getValue(env, TraceInfo.UNKNOWN, value.value), level, used);
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Throwable throwable) {
                        throw new RuntimeException(throwable);
                    }
                }
            }
        }
        ForeachIterator iterator = arr.foreachIterator(false, false);
        while (iterator.next()) {
            printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
            Memory key = iterator.getMemoryKey();
            printer.write('[');
            if (key.isString()) {
                String realKey = key.toString();
                int pos;
                Modifier modifier = Modifier.PUBLIC;
                String className = "";
                if ((pos = realKey.lastIndexOf("\0")) > -1) {
                    if (realKey.startsWith("\0*\0")) {
                        modifier = Modifier.PROTECTED;
                    } else {
                        modifier = Modifier.PRIVATE;
                        className = realKey.substring(1, pos);
                    }
                    realKey = realKey.substring(pos + 1);
                }
                printer.write('"');
                printer.write(realKey);
                printer.write('"');
                switch(modifier) {
                    case PRIVATE:
                        printer.write(":\"" + className + "\":private");
                        break;
                    case PROTECTED:
                        printer.write(":protected");
                }
            } else {
                printer.write(key.toString());
            }
            printer.write("]=>\n");
            printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
            print(iterator.getValue(), level, used);
        //printer.write('\n');
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        printer.write("}\n");
        used.remove(value.getPointer());
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ForeachIterator(php.runtime.lang.ForeachIterator) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory) Modifier(php.runtime.common.Modifier)

Example 25 with ForeachIterator

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

the class VarExport method printArray.

protected void printArray(ArrayMemory value, int level, Set<Integer> used, boolean stripNulls) {
    if (!used.add(value.getPointer())) {
        recursionExists = true;
        printNull();
    } else {
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        printer.write("array (\n");
        ForeachIterator iterator = value.foreachIterator(false, false);
        level += 1;
        while (iterator.next()) {
            Memory el = iterator.getValue();
            if (el == Memory.UNDEFINED)
                continue;
            printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
            Memory key = iterator.getMemoryKey();
            if (key.isString()) {
                String k = key.toString();
                if (stripNulls) {
                    int pos = k.lastIndexOf('\0');
                    if (pos > -1)
                        k = k.substring(pos + 1);
                }
                printString(new StringMemory(k));
            } else {
                printer.write(key.toString());
            }
            printer.write(" =>");
            if (el.isArray()) {
                if (!used.contains(el.getPointer()))
                    printer.write("\n");
                else
                    printer.write(" ");
                print(el, level, used);
            } else {
                printer.write(" ");
                print(el, level + 1, used);
            }
            printer.append(",\n");
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        printer.append(")");
        used.remove(value.getPointer());
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) 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