Search in sources :

Example 16 with IObject

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

the class ObjectInvokeHelper method GetAndIncProperty.

public static Memory GetAndIncProperty(Memory object, String property, Environment env, TraceInfo trace, PropertyCallCache callCache, int cacheIndex) throws Throwable {
    IObject iObject = fetchObject(object, property, env, trace);
    if (iObject == null)
        return Memory.NULL;
    ReferenceMemory ref = new ReferenceMemory();
    iObject.getReflection().plusProperty(env, trace, iObject, property, Memory.CONST_INT_1, ref);
    return ref.value;
}
Also used : ReferenceMemory(php.runtime.memory.ReferenceMemory) IObject(php.runtime.lang.IObject)

Example 17 with IObject

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

the class InvokeHelper method callStatic.

public static Memory callStatic(Environment env, TraceInfo trace, String className, String methodName, String originClassName, String originMethodName, Memory[] args, MethodCallCache callCache, int cacheIndex) throws Throwable {
    if (callCache != null) {
        MethodEntity entity = callCache.get(env, cacheIndex);
        if (entity != null) {
            return callStatic(env, trace, entity, originClassName, args, false);
        }
    }
    ClassEntity classEntity = env.fetchClass(originClassName, className, true);
    MethodEntity method = classEntity == null ? null : classEntity.findMethod(methodName);
    Memory[] passed = null;
    boolean isMagic = false;
    if (method == null) {
        IObject maybeObject = env.getLateObject();
        if (maybeObject != null && maybeObject.getReflection().isInstanceOf(classEntity))
            return ObjectInvokeHelper.invokeMethod(new ObjectMemory(maybeObject), originMethodName, methodName, env, trace, args);
        if (classEntity != null && classEntity.methodMagicCallStatic != null) {
            method = classEntity.methodMagicCallStatic;
            isMagic = true;
            passed = new Memory[] { new StringMemory(originMethodName), ArrayMemory.of(args) };
        } else {
            if (classEntity == null) {
                env.error(trace, Messages.ERR_CLASS_NOT_FOUND.fetch(originClassName));
                return Memory.NULL;
            }
        }
    }
    if (method == null) {
        env.error(trace, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(originClassName + "::" + originMethodName));
        return Memory.NULL;
    }
    if (!method.isStatic()) {
        IObject maybeObject = env.getLateObject();
        if (maybeObject != null && maybeObject.getReflection().isInstanceOf(classEntity))
            return ObjectInvokeHelper.invokeMethod(maybeObject, method, env, trace, args, true);
        env.error(trace, ErrorType.E_STRICT, Messages.ERR_NON_STATIC_METHOD_CALLED_DYNAMICALLY, originClassName, originMethodName);
    }
    if (callCache != null && !isMagic) {
        callCache.put(env, cacheIndex, method);
    }
    checkAccess(env, trace, method);
    if (passed == null)
        passed = makeArguments(env, args, method.getParameters(), originClassName, originMethodName, trace);
    Memory result = method.getImmutableResult();
    if (result != null)
        return result;
    try {
        if (trace != null)
            env.pushCall(trace, null, args, originMethodName, method.getClazz().getName(), originClassName);
        return method.invokeStatic(env, trace, passed);
    } finally {
        if (trace != null)
            env.popCall();
    }
}
Also used : IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) StringMemory(php.runtime.memory.StringMemory)

Example 18 with IObject

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

the class SPLFunctions method iterator_count.

public static long iterator_count(Environment env, TraceInfo trace, Memory object) {
    if (expectingImplement(env, trace, 1, object, Traversable.class)) {
        IObject tmp = object.toValue(ObjectMemory.class).value;
        if (tmp instanceof Countable) {
            return ((Countable) tmp).count(env).toLong();
        } else {
            ForeachIterator iterator = object.getNewIterator(env, true, false);
            int i = 0;
            while (iterator.next()) i++;
            return i;
        }
    } else
        return 0;
}
Also used : Countable(php.runtime.lang.spl.Countable) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 19 with IObject

use of php.runtime.lang.IObject 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 20 with IObject

use of php.runtime.lang.IObject 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

IObject (php.runtime.lang.IObject)23 ObjectMemory (php.runtime.memory.ObjectMemory)13 Memory (php.runtime.Memory)9 ArrayMemory (php.runtime.memory.ArrayMemory)8 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 StringMemory (php.runtime.memory.StringMemory)4 ClassEntity (php.runtime.reflection.ClassEntity)4 CriticalException (php.runtime.exceptions.CriticalException)3 ForeachIterator (php.runtime.lang.ForeachIterator)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.Test)2 Serializable (php.runtime.lang.spl.Serializable)2 MethodEntity (php.runtime.reflection.MethodEntity)2 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 LinkedHashSet (java.util.LinkedHashSet)1 NativeObject (org.develnext.jphp.ext.pna.classes.NativeObject)1 Environment (php.runtime.env.Environment)1 TraceInfo (php.runtime.env.TraceInfo)1 BaseWrapper (php.runtime.lang.BaseWrapper)1