Search in sources :

Example 46 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class Stream method register.

@Signature({ @Arg("protocol"), @Arg("className") })
public static Memory register(Environment env, Memory... args) {
    String protocol = args[0].toString();
    String className = args[1].toString();
    if (protocol.isEmpty()) {
        throw new IllegalArgumentException("Argument 1 (protocol) must be not empty");
    }
    if (!protocol.matches("^[A-Za-z0-9]+$")) {
        throw new IllegalArgumentException("Invalid Argument 1 (protocol)");
    }
    ClassEntity classEntity = env.fetchClass(className, true);
    if (classEntity == null) {
        throw new IllegalArgumentException(Messages.ERR_CLASS_NOT_FOUND.fetch(className));
    }
    env.setUserValue(Stream.class.getName() + "#" + protocol, classEntity.getName());
    return Memory.TRUE;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity)

Example 47 with ClassEntity

use of php.runtime.reflection.ClassEntity 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 48 with ClassEntity

use of php.runtime.reflection.ClassEntity 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 49 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class VarExport method printObject.

@Override
protected void printObject(ObjectMemory value, int level, Set<Integer> used) {
    if (used.contains(value.getPointer())) {
        recursionExists = true;
        printNull();
    } else {
        used.add(value.getPointer());
        ClassEntity entity = value.getReflection();
        printer.write(entity.getName());
        printer.write("::");
        printer.write("__set_state(");
        printArray(value.value.getProperties(), 0, used, true);
        printer.write(")");
        used.remove(value.getPointer());
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity)

Example 50 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class Deserializer method read.

public Memory read(String input, int offset) {
    int length = input.length();
    if (offset >= length) {
        error(offset, length);
        return Memory.NULL;
    }
    for (int i = offset; i < length; i++) {
        char ch = input.charAt(i);
        Memory.Type type = null;
        switch(ch) {
            case 'N':
                i++;
                if (i < length && input.charAt(i) == ';') {
                    this.pos = i + 1;
                    return Memory.NULL;
                } else
                    return error(i, length);
            case 'i':
                type = Memory.Type.INT;
            case 'd':
                if (type == null)
                    type = Memory.Type.DOUBLE;
            case 'b':
                if (type == null)
                    type = Memory.Type.BOOL;
            case 's':
                if (type == null)
                    type = Memory.Type.STRING;
            case 'a':
                if (type == null)
                    type = Memory.Type.ARRAY;
            case 'C':
            case 'O':
                if (type == null)
                    type = Memory.Type.OBJECT;
                boolean isSerializable = ch == 'C';
                i++;
                ch = input.charAt(i);
                if (ch != ':') {
                    error(i, length);
                    return Memory.NULL;
                }
                i++;
                switch(type) {
                    case INT:
                    case DOUBLE:
                    case BOOL:
                        boolean done = false;
                        int j;
                        for (j = i; j < length; j++) {
                            ch = input.charAt(j);
                            if (ch == ';') {
                                done = true;
                                break;
                            }
                        }
                        if (!done) {
                            error(i, length);
                            return Memory.NULL;
                        }
                        String what = input.substring(i, j);
                        this.pos = i = j + 1;
                        switch(type) {
                            case BOOL:
                                if ("1".equals(what))
                                    return Memory.TRUE;
                                else if ("0".equals(what))
                                    return Memory.FALSE;
                                else {
                                    error(i + 1, length);
                                    return Memory.NULL;
                                }
                            case INT:
                                try {
                                    return LongMemory.valueOf(Long.parseLong(what));
                                } catch (NumberFormatException e) {
                                    error(i + 1, length);
                                    return Memory.NULL;
                                }
                            case DOUBLE:
                                try {
                                    return DoubleMemory.valueOf(Double.valueOf(what));
                                } catch (NumberFormatException e) {
                                    error(i + 1, length);
                                    return Memory.NULL;
                                }
                        }
                        break;
                    case STRING:
                        return readString(input, i, ';');
                    case ARRAY:
                        return readArray(input, i);
                    case OBJECT:
                        Memory memory = readString(input, i, ':');
                        if (this.pos == -1) {
                            return Memory.NULL;
                        }
                        if (!memory.isString()) {
                            error(i, length);
                            return Memory.NULL;
                        }
                        i = this.pos;
                        ClassEntity classEntity = env.fetchClass(memory.toString(), true);
                        if (classEntity == null) {
                            env.error(trace, ErrorType.E_ERROR, Messages.ERR_CLASS_NOT_FOUND, memory.toString());
                            return Memory.NULL;
                        }
                        try {
                            IObject iObject = classEntity.newObjectWithoutConstruct(env);
                            if (iObject == null) {
                                env.exception(trace, new Messages.Item("Unserialization of '%s' is not allowed").fetch(classEntity.getName()));
                            }
                            if (isSerializable) {
                                if (!(iObject instanceof Serializable)) {
                                    env.warning(trace, "Class %s has no unserializer", classEntity.getName());
                                    return Memory.NULL;
                                } else {
                                    int size = readSize(input, i);
                                    if (size == -1)
                                        return error(i, length);
                                    i = this.pos + 1;
                                    what = input.substring(i + 1, i + size + 1);
                                    if (i >= length || input.charAt(i) != '{')
                                        return error(i, length);
                                    i += size;
                                    i++;
                                    if (i >= length || input.charAt(i) != '}')
                                        return error(i, length);
                                    env.pushCall(trace, iObject, "unserialize", new StringMemory(what));
                                    try {
                                        ((Serializable) iObject).unserialize(env, new StringMemory(what));
                                    } finally {
                                        env.popCall();
                                    }
                                }
                            } else {
                                ArrayMemory props = iObject.getProperties();
                                Memory serProps = readArray(input, i);
                                if (serProps.isArray()) {
                                    props.putAll(serProps.toValue(ArrayMemory.class));
                                    if (classEntity.methodMagicWakeup != null) {
                                        env.pushCall(trace, iObject, classEntity.methodMagicWakeup.getName());
                                        try {
                                            classEntity.methodMagicWakeup.invokeDynamic(iObject, env);
                                        } finally {
                                            env.popCall();
                                        }
                                    }
                                } else
                                    return Memory.NULL;
                            }
                            return new ObjectMemory(iObject);
                        } catch (RuntimeException e) {
                            throw e;
                        } catch (Throwable throwable) {
                            throw new RuntimeException(throwable);
                        }
                }
                break;
            default:
                error(i, length);
                return Memory.NULL;
        }
    }
    return Memory.NULL;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Serializable(php.runtime.lang.spl.Serializable) IObject(php.runtime.lang.IObject) Memory(php.runtime.Memory)

Aggregations

ClassEntity (php.runtime.reflection.ClassEntity)54 ObjectMemory (php.runtime.memory.ObjectMemory)21 Memory (php.runtime.Memory)14 ArrayMemory (php.runtime.memory.ArrayMemory)14 PropertyEntity (php.runtime.reflection.PropertyEntity)10 MethodEntity (php.runtime.reflection.MethodEntity)9 StringMemory (php.runtime.memory.StringMemory)8 ConstantEntity (php.runtime.reflection.ConstantEntity)5 FunctionEntity (php.runtime.reflection.FunctionEntity)5 CriticalException (php.runtime.exceptions.CriticalException)4 Closure (php.runtime.lang.Closure)4 IObject (php.runtime.lang.IObject)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2