Search in sources :

Example 1 with PropertyEntity

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

the class PropertyDumperTest method testBasic.

@Test
public void testBasic() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    PropertyEntity propertyEntity = new PropertyEntity(context);
    propertyEntity.setModifier(Modifier.PROTECTED);
    propertyEntity.setName("foobar");
    propertyEntity.setStatic(true);
    propertyEntity.setDefaultValue(Memory.CONST_INT_5);
    propertyEntity.setClazz(null);
    propertyDumper.save(propertyEntity, output);
    PropertyEntity copyEntity = propertyDumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("foobar", copyEntity.getName());
    Assert.assertEquals(Memory.CONST_INT_5, copyEntity.getDefaultValue(null));
    Assert.assertTrue(copyEntity.isStatic());
    Assert.assertEquals(Modifier.PROTECTED, copyEntity.getModifier());
}
Also used : PropertyEntity(php.runtime.reflection.PropertyEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with PropertyEntity

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

the class ObjectMemory method toArray.

@Override
public Memory toArray() {
    ArrayMemory result = new ArrayMemory();
    ArrayMemory props = value.getProperties();
    ForeachIterator iterator = props == null ? null : props.foreachIterator(false, false);
    if (iterator == null) {
        return new ArrayMemory().toConstant();
    }
    ClassEntity reflection = value.getReflection();
    while (iterator.next()) {
        Object key = iterator.getKey();
        Memory value = iterator.getValue().fast_toImmutable();
        if (key instanceof String) {
            String keyS = (String) key;
            PropertyEntity prop = reflection.properties.get(keyS);
            if (prop == null || prop.getModifier() == Modifier.PUBLIC) {
                result.refOfIndex(keyS).assign(iterator.getValue().fast_toImmutable());
            } else {
                if (prop.getModifier() == Modifier.PROTECTED) {
                    result.refOfIndex("\0*\0" + keyS).assign(value);
                } else {
                    result.refOfIndex("\0" + prop.getClazz().getName() + "\0" + keyS).assign(value);
                }
            }
        } else {
            result.refOfIndex(null, iterator.getMemoryKey()).assign(value);
        }
    }
    return result.toConstant();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory) IComparableObject(php.runtime.lang.support.IComparableObject) ICloneableObject(php.runtime.lang.support.ICloneableObject)

Example 3 with PropertyEntity

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

the class ObjectMemory method getNewIterator.

@Override
public ForeachIterator getNewIterator(final Environment env, boolean getReferences, boolean getKeyReferences) {
    if (value instanceof IteratorAggregate) {
        return env.invokeMethodNoThrow(value, "getIterator").getNewIterator(env, getReferences, getKeyReferences);
    } else if (value instanceof Iterator) {
        final Iterator iterator = (Iterator) value;
        final String className = value.getReflection().getName();
        final boolean isNative = value.getReflection().isInternal();
        return new ForeachIterator(getReferences, getKeyReferences, false) {

            private boolean keyInit = false;

            private boolean needNext = false;

            private boolean rewind = false;

            @Override
            public void reset() {
                rewind();
            }

            @Override
            protected boolean init() {
                return rewind();
            }

            protected boolean rewind() {
                if (getReferences && value instanceof Generator) {
                    if (!((Generator) value).isReturnReferences()) {
                        env.exception(trace, "You can only iterate a generator by-reference if it declared that it yields by-reference");
                    }
                }
                if (!rewind) {
                    if (!isNative)
                        env.pushCall(trace, ObjectMemory.this.value, null, "rewind", className, null);
                    try {
                        return iterator.rewind(env).toValue() != FALSE;
                    } finally {
                        rewind = true;
                        if (!isNative)
                            env.popCall();
                    }
                }
                return true;
            }

            @Override
            protected boolean prevValue() {
                return false;
            }

            @Override
            protected boolean nextValue() {
                return true;
            }

            @Override
            public boolean next() {
                if (!rewind())
                    return false;
                boolean valid = false;
                keyInit = false;
                if (needNext) {
                    if (!isNative)
                        env.pushCall(trace, ObjectMemory.this.value, null, "next", className, null);
                    try {
                        iterator.next(env);
                    } finally {
                        if (!isNative)
                            env.popCall();
                    }
                }
                needNext = true;
                if (!isNative)
                    env.pushCall(trace, ObjectMemory.this.value, null, "valid", className, null);
                try {
                    valid = iterator.valid(env).toBoolean();
                    if (valid) {
                        if (!isNative)
                            env.pushCall(trace, ObjectMemory.this.value, null, "current", className, null);
                        try {
                            currentValue = iterator.current(env);
                            if (!getReferences)
                                currentValue = currentValue.fast_toImmutable();
                        } finally {
                            if (!isNative)
                                env.popCall();
                        }
                    } else {
                        rewind = false;
                    }
                } finally {
                    if (!isNative)
                        env.popCall();
                }
                return valid;
            }

            @Override
            public Object getKey() {
                return getMemoryKey();
            }

            @Override
            public Memory getMemoryKey() {
                if (keyInit)
                    return (Memory) currentKey;
                if (!isNative)
                    env.pushCall(trace, ObjectMemory.this.value, null, "key", className, null);
                try {
                    currentKey = iterator.key(env).fast_toImmutable();
                    keyInit = true;
                    return (Memory) currentKey;
                } finally {
                    if (!isNative)
                        env.popCall();
                }
            }
        };
    } else if (value instanceof Traversable) {
        return ((Traversable) value).getNewIterator(env, getReferences, getKeyReferences);
    } else {
        return new ForeachIterator(getReferences, getKeyReferences, false) {

            private ForeachIterator child;

            private ClassEntity reflection;

            private ClassEntity context;

            @Override
            protected boolean init() {
                context = env.getLastClassOnStack();
                reflection = value.getReflection();
                child = value.getProperties().foreachIterator(getReferences, getKeyReferences);
                return true;
            }

            @Override
            public void reset() {
                child.reset();
            }

            @Override
            protected boolean nextValue() {
                while (true) {
                    if (!child.next())
                        return false;
                    if (child.getValue().isUninitialized()) {
                        boolean found = false;
                        while (child.next()) {
                            if (!child.getValue().isUninitialized()) {
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                            return false;
                    }
                    Object key = child.getKey();
                    if (key instanceof String) {
                        String keyS = (String) key;
                        int pos = keyS.lastIndexOf('\0');
                        if (pos > -1)
                            keyS = keyS.substring(pos + 1);
                        PropertyEntity entity = reflection.isInstanceOf(context) ? context.properties.get(keyS) : reflection.properties.get(keyS);
                        int accessFlag = entity == null ? 0 : entity.canAccess(env);
                        if (accessFlag == 0) {
                            currentKey = entity == null ? keyS : entity.getName();
                            break;
                        } else
                            continue;
                    }
                    break;
                }
                // currentKey = child.getKey();
                currentValue = child.getValue();
                return true;
            }

            @Override
            protected boolean prevValue() {
                return false;
            }
        };
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Memory(php.runtime.Memory) IteratorAggregate(php.runtime.lang.spl.iterator.IteratorAggregate) PropertyEntity(php.runtime.reflection.PropertyEntity) Iterator(php.runtime.lang.spl.iterator.Iterator) IComparableObject(php.runtime.lang.support.IComparableObject) ICloneableObject(php.runtime.lang.support.ICloneableObject) Traversable(php.runtime.lang.spl.Traversable)

Example 4 with PropertyEntity

use of php.runtime.reflection.PropertyEntity 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());
        int size = arr.size();
        for (ReferenceMemory referenceMemory : arr) {
            if (referenceMemory.isUninitialized()) {
                size--;
            }
        }
        printer.write(" (" + 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 5 with PropertyEntity

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

the class ReflectionProperty method __construct.

@Signature({ @Arg("class"), @Arg("name") })
public Memory __construct(Environment env, Memory... args) {
    ClassEntity classEntity;
    Memory arg = args[0];
    if (arg.isObject()) {
        classEntity = arg.toValue(ObjectMemory.class).getReflection();
    } else
        classEntity = env.fetchClass(arg.toString());
    if (classEntity == null) {
        exception(env, Messages.ERR_CLASS_NOT_FOUND.fetch(arg));
        return Memory.NULL;
    }
    String prop = args[1].toString();
    PropertyEntity entity = classEntity.findProperty(prop);
    if (entity == null)
        entity = classEntity.findStaticProperty(prop);
    if (entity == null) {
        exception(env, Messages.ERR_UNDEFINED_PROPERTY.fetch(classEntity.getName(), prop));
        return Memory.NULL;
    }
    setEntity(entity);
    return Memory.NULL;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory) LongMemory(php.runtime.memory.LongMemory) StringMemory(php.runtime.memory.StringMemory) ObjectMemory(php.runtime.memory.ObjectMemory)

Aggregations

PropertyEntity (php.runtime.reflection.PropertyEntity)13 ClassEntity (php.runtime.reflection.ClassEntity)10 Memory (php.runtime.Memory)6 ObjectMemory (php.runtime.memory.ObjectMemory)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 Modifier (php.runtime.common.Modifier)2 ICloneableObject (php.runtime.lang.support.ICloneableObject)2 IComparableObject (php.runtime.lang.support.IComparableObject)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 LinkedHashSet (java.util.LinkedHashSet)1 IObject (php.runtime.lang.IObject)1 Serializable (php.runtime.lang.spl.Serializable)1 Traversable (php.runtime.lang.spl.Traversable)1 Iterator (php.runtime.lang.spl.iterator.Iterator)1 IteratorAggregate (php.runtime.lang.spl.iterator.IteratorAggregate)1 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)1 LongMemory (php.runtime.memory.LongMemory)1