Search in sources :

Example 96 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class ClassEntity method unsetProperty.

public Memory unsetProperty(Environment env, TraceInfo trace, IObject object, String property, PropertyCallCache callCache, int index) throws Throwable {
    ClassEntity context = env.getLastClassOnStack();
    PropertyEntity entity = isInstanceOf(context) ? context.properties.get(property) : properties.get(property);
    int accessFlag = entity == null ? 0 : entity.canAccess(env);
    if (entity == null) {
        PropertyEntity staticEntity = staticProperties.get(property);
        if (staticEntity != null) {
            invalidAccessToProperty(env, trace, staticEntity, staticEntity.canAccess(env));
        }
    }
    ArrayMemory props = object.getProperties();
    if (props == null || accessFlag != 0 || props.removeByScalar(entity == null ? property : entity.specificName) == null) {
        if (methodMagicUnset != null) {
            if (context != null && context.getId() == methodMagicUnset.getClazz().getId()) {
                if (env.peekCall(0).flags == FLAG_UNSET) {
                    return Memory.NULL;
                }
            }
            try {
                Memory[] args = new Memory[] { new StringMemory(property) };
                env.pushCall(trace, object, args, methodMagicUnset.getName(), methodMagicUnset.getClazz().getName(), name);
                env.peekCall(0).flags = FLAG_UNSET;
                InvokeArgumentHelper.checkType(env, trace, methodMagicUnset, args);
                methodMagicUnset.invokeDynamic(object, env, args);
            } finally {
                env.popCall();
            }
            return Memory.NULL;
        }
    }
    if (accessFlag != 0)
        invalidAccessToProperty(env, trace, entity, accessFlag);
    entity = staticProperties.get(property);
    if (entity != null) {
        env.error(trace, ErrorType.E_STRICT, Messages.ERR_ACCESSING_STATIC_PROPERTY_AS_NON_STATIC, entity.getClazz().getName(), entity.getName());
    }
    return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ArrayMemory(php.runtime.memory.ArrayMemory) Memory(php.runtime.Memory) ReferenceMemory(php.runtime.memory.ReferenceMemory) StringMemory(php.runtime.memory.StringMemory) StringMemory(php.runtime.memory.StringMemory)

Example 97 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class CompileMethodEntity method invokeDynamic.

public Memory invokeDynamic(Object _this, Environment env, Memory... arguments) throws Throwable {
    try {
        if (isAbstract) {
            env.error(ErrorType.E_ERROR, "Cannot call abstract method %s", getSignatureString(false));
            return Memory.NULL;
        }
        if (_this == null && !isStatic) {
            _this = clazz.newMock(env);
            if (_this == null)
                env.error(ErrorType.E_ERROR, Messages.ERR_STATIC_METHOD_CALLED_DYNAMICALLY.fetch(getClazz().getName() + "::" + getName()));
        }
        CompileMethod.Method method = function.find(arguments == null ? 0 : arguments.length);
        if (method == null) {
            env.warning(env.trace(), Messages.ERR_EXPECT_LEAST_PARAMS.fetch(name, function.getMinArgs(), arguments == null ? 0 : arguments.length));
            return Memory.NULL;
        } else {
            if (arguments != null && arguments.length > method.argsCount && !method.isVarArg()) {
                env.error(env.trace(), ErrorType.E_ERROR, Messages.ERR_EXPECT_EXACTLY_PARAMS, name, method.argsCount, arguments.length);
                return Memory.NULL;
            }
        }
        Class<?>[] types = method.parameterTypes;
        Object[] passed = new Object[types.length];
        int i = 0;
        int j = 0;
        for (Class<?> clazz : types) {
            boolean isRef = method.references[i];
            boolean mutableValue = method.mutableValues[i];
            MemoryOperation<?> operation = method.argumentOperations[i];
            if (clazz == Memory.class) {
                passed[i] = isRef ? arguments[j] : (mutableValue ? arguments[j].toValue() : arguments[j].toImmutable());
                j++;
            } else if (operation != null) {
                if (operation instanceof InjectMemoryOperation) {
                    passed[i] = operation.convert(env, trace, null);
                } else {
                    passed[i] = operation.convert(env, trace, arguments[j]);
                    j++;
                }
            } else if (i == types.length - 1 && types[i] == Memory[].class) {
                Memory[] arg = new Memory[arguments.length - i + 1];
                if (!isRef) {
                    for (int k = 0; k < arg.length; k++) arg[i] = arguments[i].toImmutable();
                } else {
                    System.arraycopy(arguments, j, arg, 0, arg.length);
                }
                passed[i] = arg;
                break;
            } else {
                env.error(trace, ErrorType.E_CORE_ERROR, name + "(): Cannot call this method dynamically");
                passed[i] = Memory.NULL;
            }
            i++;
        }
        try {
            return method.returnOperation.unconvertNoThow(env, trace, method.method.invoke(_this, passed));
        } finally {
            i = 0;
            if (this != this.getClazz().methodConstruct) {
                for (Object o : passed) {
                    MemoryOperation operation = method.argumentOperations[i];
                    if (operation != null) {
                        operation.releaseConverted(env, trace, o);
                    }
                    i++;
                }
            }
        }
    } catch (InvocationTargetException e) {
        return env.__throwException(e);
    } catch (Throwable e) {
        throw e;
    } finally {
        unsetArguments(arguments);
    }
}
Also used : Memory(php.runtime.Memory) MemoryOperation(php.runtime.memory.support.MemoryOperation) InvocationTargetException(java.lang.reflect.InvocationTargetException) IObject(php.runtime.lang.IObject)

Example 98 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class DumpInputStream method readMemory.

public Memory readMemory() throws IOException {
    int tmp = readInt();
    if (tmp == -1)
        return null;
    if (tmp == -2)
        return new ConstantMemory(readUTF());
    if (tmp == -3)
        return new ClassConstantMemory(readUTF(), readUTF());
    if (tmp >= 0 && tmp < Memory.Type.values().length) {
        Memory.Type type = Memory.Type.values()[tmp];
        switch(type) {
            case BOOL:
                return readBoolean() ? Memory.TRUE : Memory.FALSE;
            case INT:
                return LongMemory.valueOf(readLong());
            case DOUBLE:
                return DoubleMemory.valueOf(readDouble());
            case NULL:
                return Memory.NULL;
            case STRING:
                return StringMemory.valueOf(readUTF());
            case ARRAY:
                ArrayMemory array = new ArrayMemory();
                int size = readInt();
                if (size < 0 || size > Short.MAX_VALUE)
                    throw new DumpException("Invalid array memory size");
                for (int i = 0; i < size; i++) {
                    Memory key = readMemory();
                    Memory value = readMemory();
                    array.refOfIndex(key).assign(value);
                }
                return array;
            case OBJECT:
            default:
                throw new DumpException("Cannot read " + type.toString() + " memory");
        }
    } else
        throw new DumpException("Invalid ~Memory.Type value");
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) LongMemory(php.runtime.memory.LongMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) StringMemory(php.runtime.memory.StringMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory)

Example 99 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class DumpOutputStream method writeMemory.

public void writeMemory(Memory memory) throws IOException {
    if (memory == null) {
        writeInt(-1);
        return;
    }
    memory = memory.toValue();
    if (memory instanceof ConstantMemory) {
        writeInt(-2);
        writeUTF(((ConstantMemory) memory).getName());
        return;
    } else if (memory instanceof ClassConstantMemory) {
        writeInt(-3);
        writeUTF(((ClassConstantMemory) memory).getClassName());
        writeUTF(((ClassConstantMemory) memory).getName());
        return;
    }
    Memory.Type type = memory.getRealType();
    writeInt(type.ordinal());
    switch(type) {
        case NULL:
            break;
        case INT:
            writeLong(memory.toLong());
            break;
        case STRING:
            writeUTF(memory.toString());
            break;
        case DOUBLE:
            writeDouble(memory.toDouble());
            break;
        case BOOL:
            writeBoolean(memory.toBoolean());
            break;
        case ARRAY:
            ArrayMemory array = memory.toValue(ArrayMemory.class);
            if (array.size() > Short.MAX_VALUE)
                throw new DumpException("Array is too big");
            writeInt(array.size());
            ForeachIterator foreachIterator = array.foreachIterator(false, false);
            while (foreachIterator.next()) {
                Memory key = foreachIterator.getMemoryKey();
                Memory value = foreachIterator.getValue();
                if (value.isShortcut())
                    throw new DumpException("Cannot dump references");
                if (value.toValue() != Memory.UNDEFINED) {
                    writeMemory(key);
                    writeMemory(value.toValue());
                }
            }
            break;
        case OBJECT:
        default:
            throw new DumpException("Cannot dump " + type.toString() + " memory");
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory)

Example 100 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class ArrayMemory method toBoolArray.

public boolean[] toBoolArray() {
    boolean[] r = new boolean[size];
    int i = 0;
    for (Memory e : this) {
        r[i] = e.toBoolean();
        i++;
    }
    return r;
}
Also used : ShortcutMemory(php.runtime.memory.helper.ShortcutMemory) Memory(php.runtime.Memory) ArrayKeyMemory(php.runtime.memory.helper.ArrayKeyMemory) ArrayValueMemory(php.runtime.memory.helper.ArrayValueMemory)

Aggregations

Memory (php.runtime.Memory)346 Test (org.junit.Test)124 ArrayMemory (php.runtime.memory.ArrayMemory)122 ObjectMemory (php.runtime.memory.ObjectMemory)75 LongMemory (php.runtime.memory.LongMemory)58 StringMemory (php.runtime.memory.StringMemory)58 ForeachIterator (php.runtime.lang.ForeachIterator)47 ReferenceMemory (php.runtime.memory.ReferenceMemory)40 KeyValueMemory (php.runtime.memory.KeyValueMemory)25 Invoker (php.runtime.invoke.Invoker)21 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)21 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)21 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)21 Environment (php.runtime.env.Environment)20 IObject (php.runtime.lang.IObject)19 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)16 ClassEntity (php.runtime.reflection.ClassEntity)16 TraceInfo (php.runtime.env.TraceInfo)12 File (java.io.File)7 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)7