Search in sources :

Example 86 with ForeachIterator

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

the class Serializer method writeObject.

public void writeObject(ObjectMemory memory, Set<Integer> used) {
    if (used.add(memory.getPointer())) {
        IObject object = memory.value;
        ClassEntity reflection = object.getReflection();
        if (object instanceof Serializable) {
            Memory result;
            env.pushCall(trace, object, "serialize");
            try {
                result = ((Serializable) object).serialize(env);
                if (result.isNull()) {
                    writeNull();
                    return;
                }
                if (result.isString()) {
                    String value = result.toString();
                    printer.append("C:").append(reflection.getName().length()).append(":\"").append(reflection.getName()).append("\":").append(value.length()).append(":{").append(value).append("}");
                    return;
                } else {
                    env.exception(trace, reflection.getName() + "::serialize() must return a string or NULL");
                }
            } finally {
                env.popCall();
            }
        }
        ArrayMemory only = null;
        if (reflection.methodMagicSleep != null) {
            env.pushCall(trace, object, reflection.methodMagicSleep.getName());
            try {
                Memory result = reflection.methodMagicSleep.invokeDynamic(object, env);
                if (!result.isArray()) {
                    env.error(ErrorType.E_NOTICE, "serialize(): __sleep() should return an array only containing the names of instance-variables to serialize");
                    writeNull();
                    return;
                } else {
                    ForeachIterator iterator = result.getNewIterator(env, false, false);
                    only = new ArrayMemory(true);
                    ArrayMemory props = memory.getProperties();
                    Set<String> need = new LinkedHashSet<String>();
                    while (iterator.next()) {
                        if (iterator.getValue().isNumber())
                            continue;
                        need.add(iterator.getValue().toString());
                    }
                    for (PropertyEntity e : reflection.getProperties()) {
                        if (need.contains(e.getName())) {
                            props.refOfIndex(e.getSpecificName());
                        }
                    }
                    iterator = result.getNewIterator(env, false, false);
                    while (iterator.next()) {
                        Memory value = iterator.getValue().toValue();
                        PropertyEntity entity = reflection.findProperty(value.toString());
                        value = entity == null ? props.valueOfIndex(value).toValue() : props.valueOfIndex(entity.getSpecificName()).toValue();
                        if (value == Memory.UNDEFINED) {
                            env.error(trace, ErrorType.E_NOTICE, "serialize(): \"%s\" returned as member variable from __sleep() but does not exist", iterator.getValue().toString());
                        }
                        if (entity != null)
                            only.put(entity.getSpecificName(), value);
                        else
                            only.refOfIndex(iterator.getValue()).assign(value);
                    }
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            } finally {
                env.popCall();
            }
        }
        printer.append("O:");
        printer.append(String.valueOf(reflection.getName().length()));
        printer.append(":\"");
        printer.append(reflection.getName());
        printer.append("\":");
        if (reflection.getProperties() == null)
            writeArray(new ArrayMemory(), used, false);
        else
            writeArray(only == null ? object.getProperties() : only, used, false);
    } else
        writeNull();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) LinkedHashSet(java.util.LinkedHashSet) Serializable(php.runtime.lang.spl.Serializable) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory)

Example 87 with ForeachIterator

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

the class MapMemoryOperation method convert.

@Override
@SuppressWarnings("unchecked")
public Map convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
    Map result = this.createHashMap();
    ForeachIterator iterator = arg.getNewIterator(env);
    while (iterator.next()) {
        result.put(operations[0].convert(env, trace, iterator.getMemoryKey()), operations[1].convert(env, trace, iterator.getValue()));
    }
    return result;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 88 with ForeachIterator

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

the class PrintR method printObject.

@Override
protected void printObject(ObjectMemory value, int level, Set<Integer> used) {
    ClassEntity classEntity = value.getReflection();
    writeObjectHeader(classEntity.getName());
    if (used.contains(value.getPointer())) {
        printer.write(" *RECURSION*");
    } else {
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeOpen();
        level += 1;
        used.add(value.getPointer());
        ArrayMemory props;
        if (env != null && classEntity.methodMagicDebugInfo != null) {
            try {
                Memory tmp = env.invokeMethod(value.value, classEntity.methodMagicDebugInfo.getName());
                if (tmp.isArray()) {
                    props = tmp.toValue(ArrayMemory.class);
                } else {
                    props = new ArrayMemory();
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        } else {
            props = value.getProperties();
        }
        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(":getter] => ");
                    try {
                        print(entity.getValue(env, TraceInfo.UNKNOWN, value.value));
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Throwable throwable) {
                        throw new RuntimeException(throwable);
                    }
                    writeSeparator(false);
                }
            }
        }
        if (props != null) {
            ForeachIterator iterator = props.foreachIterator(false, false);
            int i = 0;
            int size = classEntity.properties.size();
            while (iterator.next()) {
                printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
                Object key = iterator.getKey();
                printer.write('[');
                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(realKey);
                switch(modifier) {
                    case PRIVATE:
                        printer.write(":" + className + ":private");
                        break;
                    case PROTECTED:
                        printer.write(":protected");
                }
                printer.write("] => ");
                print(iterator.getValue(), level + 1, used);
                writeSeparator(i == size - 1);
                i++;
            }
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeClose();
        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 89 with ForeachIterator

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

the class VarDump method printArray.

@Override
protected void printArray(ArrayMemory value, int level, Set<Integer> used) {
    if (used.contains(value.getPointer())) {
        printer.write("*RECURSION*\n");
    } else {
        printer.write("array(");
        printer.write(value.size() + "");
        printer.write(") {\n");
        level += 1;
        used.add(value.getPointer());
        ForeachIterator iterator = value.foreachIterator(false, false);
        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('[');
            if (key.isString()) {
                printer.write('"');
                printer.write(key.toString());
                printer.write('"');
            } else {
                printer.write(key.toString());
            }
            printer.write("]=>\n");
            printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
            print(el, level, used);
        //printer.write('\n');
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        printer.write("}\n");
        used.remove(value.getPointer());
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory)

Example 90 with ForeachIterator

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

the class ArrayMemory method resetCurrentIterator.

public Memory resetCurrentIterator() {
    reset();
    ForeachIterator iterator = getCurrentIterator();
    if (size == 0)
        return FALSE;
    else {
        iterator.next();
        Memory tmp = iterator.getValue();
        iterator.prev();
        return tmp;
    }
}
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)

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