Search in sources :

Example 1 with ClassConstantMemory

use of php.runtime.memory.helper.ClassConstantMemory 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 == -4)
        return UninitializedMemory.valueOf(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 : Memory(php.runtime.Memory) 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 2 with ClassConstantMemory

use of php.runtime.memory.helper.ClassConstantMemory 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;
    } else if (memory instanceof UninitializedMemory) {
        writeInt(-4);
        writeUTF(((UninitializedMemory) memory).getArg());
        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) UninitializedMemory(php.runtime.memory.UninitializedMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) UninitializedMemory(php.runtime.memory.UninitializedMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory)

Example 3 with ClassConstantMemory

use of php.runtime.memory.helper.ClassConstantMemory in project jphp by jphp-compiler.

the class MethodStmtCompiler method compile.

@Override
public MethodEntity compile() {
    if (statement != null) {
        if (external)
            statement.setDynamicLocal(true);
        if (statement.getDocComment() != null)
            entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
        entity.setAbstract(statement.isAbstract());
        entity.setAbstractable(statement.getBody() == null);
        entity.setFinal(statement.isFinal());
        entity.setStatic(statement.isStatic());
        entity.setModifier(statement.getModifier());
        entity.setReturnReference(statement.isReturnReference());
        entity.setTrace(statement.toTraceInfo(compiler.getContext()));
        entity.setImmutable(statement.getArguments().isEmpty());
        entity.setGeneratorEntity(generatorEntity);
        if (statement.getReturnHintTypeClass() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintTypeClass().getName()));
        } else if (statement.getReturnHintType() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintType()));
            if (statement.getReturnHintType() == HintType.SELF) {
                entity.setUsesStackTrace(true);
            }
        }
        entity.setReturnTypeNullable(statement.isReturnOptional());
        if (clazz.isSystem())
            entity.setInternalName(entity.getName());
        else
            entity.setInternalName(entity.getName() + "$" + clazz.entity.nextMethodIndex());
        ParameterEntity[] parameters = new ParameterEntity[statement.getArguments().size()];
        int i = 0;
        for (ArgumentStmtToken argument : statement.getArguments()) {
            parameters[i] = new ParameterEntity(compiler.getContext());
            ParameterEntity parameter = parameters[i];
            parameter.setReference(argument.isReference());
            parameter.setName(argument.getName().getName());
            parameter.setTrace(argument.toTraceInfo(compiler.getContext()));
            parameter.setNullable(argument.isOptional());
            parameter.setMutable(statement.isDynamicLocal() || statement.variable(argument.getName()).isMutable());
            parameter.setUsed(!statement.isUnusedVariable(argument.getName()));
            parameter.setVariadic(argument.isVariadic());
            parameter.setType(argument.getHintType());
            if (argument.getHintTypeClass() != null) {
                parameter.setTypeClass(argument.getHintTypeClass().getName());
            }
            ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(compiler);
            ExprStmtToken value = argument.getValue();
            if (value != null) {
                Memory defaultValue = expressionStmtCompiler.writeExpression(value, true, true, false);
                // try detect constant
                if (value.isSingle()) {
                    if (value.getSingle() instanceof NameToken) {
                        parameter.setDefaultValueConstName(((NameToken) value.getSingle()).getName());
                        if (defaultValue == null) {
                            defaultValue = (new ConstantMemory(((NameToken) value.getSingle()).getName()));
                            parameter.setMutable(true);
                        }
                    } else if (value.getSingle() instanceof StaticAccessExprToken) {
                        StaticAccessExprToken access = (StaticAccessExprToken) value.getSingle();
                        if (access.getClazz() instanceof NameToken && access.getField() instanceof NameToken) {
                            if (defaultValue == null)
                                defaultValue = (new ClassConstantMemory(((NameToken) access.getClazz()).getName(), ((NameToken) access.getField()).getName()));
                            parameter.setDefaultValueConstName(((NameToken) access.getClazz()).getName() + "::" + ((NameToken) access.getField()).getName());
                            parameter.setMutable(true);
                        }
                    }
                }
                if (defaultValue == null)
                    compiler.getEnvironment().error(argument.toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE, "$" + argument.getName().getName());
                parameter.setDefaultValue(defaultValue);
            }
            i++;
        }
        entity.setParameters(parameters);
    }
    if (statement != null && clazz.statement.isInterface()) {
        if (!statement.isInterfacable()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_INTERFACE_FUNCTION_CANNOT_CONTAIN_BODY.fetch(entity.getSignatureString(false)));
        }
        if (statement.isAbstract() || statement.isFinal()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_ACCESS_TYPE_FOR_INTERFACE_METHOD.fetch(entity.getSignatureString(false)));
        }
    } else {
        writeHeader();
        if (statement.isGenerator()) {
            entity.setEmpty(false);
            entity.setImmutable(false);
            entity.setResult(null);
            GeneratorStmtCompiler generatorStmtCompiler = new GeneratorStmtCompiler(compiler, statement);
            entity.setGeneratorEntity(generatorStmtCompiler.compile());
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            expr.makeUnknown(new TypeInsnNode(NEW, entity.getGeneratorEntity().getInternalName()));
            expr.stackPush(Memory.Type.REFERENCE);
            expr.writePushDup();
            // env
            expr.writePushEnv();
            // classEntity
            expr.writePushDup();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getGeneratorEntity().getId());
            expr.writeSysDynamicCall(Environment.class, "__getGenerator", ClassEntity.class, String.class, Integer.TYPE);
            // self
            expr.writePushThis();
            // uses
            expr.writeVarLoad("~args");
            expr.writeSysCall(entity.getGeneratorEntity().getInternalName(), INVOKESPECIAL, Constants.INIT_METHOD, void.class, Environment.class, ClassEntity.class, Memory.class, Memory[].class);
            expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
            expr.makeUnknown(new InsnNode(Opcodes.ARETURN));
            expr.stackPop();
        } else {
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            entity.setEmpty(true);
            if (entity.getResult() == null) {
                entity.setResult(Memory.UNDEFINED);
            }
            if (statement != null && statement.getBody() != null) {
                expr.writeDefineVariables(statement.getLocal());
                expr.write(statement.getBody());
                if (!statement.getBody().getInstructions().isEmpty()) {
                    entity.setEmpty(false);
                    if (entity.getResult() != null && entity.getResult().isUndefined()) {
                        entity.setResult(null);
                    }
                }
            }
            if (generatorEntity != null) {
                expr.writeVarLoad(LocalVariable.THIS);
                expr.writePushConstBoolean(false);
                expr.writeSysDynamicCall(null, "_setValid", void.class, Boolean.TYPE);
            }
            ReturnStmtToken token = new ReturnStmtToken(new TokenMeta("", 0, 0, 0, 0));
            token.setValue(null);
            token.setEmpty(true);
            expr.getCompiler(ReturnStmtToken.class).write(token);
        }
        writeFooter();
    }
    return entity;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ArrayMemory(php.runtime.memory.ArrayMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) Memory(php.runtime.Memory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ObjectMemory(php.runtime.memory.ObjectMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory)

Aggregations

Memory (php.runtime.Memory)3 ClassConstantMemory (php.runtime.memory.helper.ClassConstantMemory)3 ConstantMemory (php.runtime.memory.helper.ConstantMemory)3 ArrayMemory (php.runtime.memory.ArrayMemory)2 TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 UninitializedMemory (php.runtime.memory.UninitializedMemory)1 DocumentComment (php.runtime.reflection.DocumentComment)1 ParameterEntity (php.runtime.reflection.ParameterEntity)1