Search in sources :

Example 16 with ParameterEntity

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

the class ReflectionFunction method getParameters.

@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
    if (cachedParameters != null)
        return cachedParameters;
    if (functionEntity instanceof CompileFunctionEntity)
        exception(env, "Cannot get parameters for internal function %s()", functionEntity.getName());
    ParameterEntity[] parameters = closureEntity == null ? functionEntity.getParameters() : closureEntity.parameters;
    ClassEntity entity = env.fetchClass("ReflectionParameter");
    ArrayMemory result = new ArrayMemory();
    int i = 0;
    for (ParameterEntity param : parameters) {
        ReflectionParameter e = new ReflectionParameter(env, entity);
        e.setEntity(param);
        e.setFunctionEntity(functionEntity);
        e.setPosition(i);
        i++;
        result.add(new ObjectMemory(e));
    }
    return cachedParameters = result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory) CompileFunctionEntity(php.runtime.reflection.CompileFunctionEntity)

Example 17 with ParameterEntity

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

the class ReflectionMethod method getParameters.

@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
    if (cachedParameters != null)
        return cachedParameters;
    ParameterEntity[] parameters = methodEntity.getParameters(Integer.MAX_VALUE);
    ClassEntity entity = env.fetchClass("ReflectionParameter");
    ArrayMemory result = new ArrayMemory();
    int i = 0;
    for (ParameterEntity param : parameters) {
        ReflectionParameter e = new ReflectionParameter(env, entity);
        e.setEntity(param);
        e.setFunctionEntity(methodEntity);
        e.setPosition(i);
        i++;
        result.add(new ObjectMemory(e));
    }
    return cachedParameters = result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 18 with ParameterEntity

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

the class WrapInvoker method getDescription.

@Signature
public Memory getDescription(Environment env, Memory... args) {
    StringBuilderMemory sb = new StringBuilderMemory();
    sb.append(invoker.getName());
    sb.append("(");
    int i = 0;
    if (invoker.getParameters() == null) {
        sb.append("<internal>");
    } else
        for (ParameterEntity param : invoker.getParameters()) {
            if (i != 0)
                sb.append(", ");
            sb.append(param.getSignatureString());
            i++;
        }
    sb.append(")");
    return sb;
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) StringBuilderMemory(php.runtime.memory.StringBuilderMemory)

Example 19 with ParameterEntity

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

the class InvokeArgumentHelper method checkType.

public static void checkType(Environment env, TraceInfo trace, MethodEntity methodEntity, Memory... args) {
    if (args == null) {
        return;
    }
    ParameterEntity[] parameters = methodEntity.getParameters(args.length);
    int i = 0;
    for (Memory arg : args) {
        if (i > parameters.length - 1) {
            break;
        }
        if (!parameters[i].checkTypeHinting(env, arg)) {
            invalidType(env, trace, parameters[i], i + 1, arg, methodEntity.getClazzName(), methodEntity.getName());
        }
        i++;
    }
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory)

Example 20 with ParameterEntity

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

the class InvokeArgumentHelper method unpackArgs.

public static Memory[] unpackArgs(Environment env, TraceInfo trace, Memory[] passed, ParameterEntity[] parameters) {
    List<Memory> varPassed = null;
    if (passed == null) {
        return null;
    }
    int cnt = 0, paramCnt = 0;
    ParameterEntity parameterEntity = null;
    boolean variadicMemoryExists = false;
    for (Memory arg : passed) {
        if (arg instanceof VariadicMemory) {
            variadicMemoryExists = true;
            if (varPassed == null) {
                varPassed = new ArrayList<Memory>();
                for (int i = 0; i < cnt; i++) {
                    varPassed.add(passed[i]);
                }
            }
            boolean isGenerator = arg.instanceOf(Generator.class);
            ForeachIterator foreachIterator = arg.getNewIterator(env, !isGenerator, false);
            if (foreachIterator == null || (!isGenerator && !arg.isTraversable())) {
                env.warning(trace, INVALID_TYPE_MESSAGE);
            } else {
                boolean isRef;
                while (foreachIterator.next()) {
                    if (parameters != null) {
                        if (parameterEntity == null || !parameterEntity.isVariadic()) {
                            parameterEntity = paramCnt < parameters.length ? parameters[paramCnt] : null;
                        }
                    }
                    isRef = parameterEntity != null && parameterEntity.isReference();
                    Memory value = foreachIterator.getValue();
                    varPassed.add(isRef ? value : value.toImmutable());
                    paramCnt++;
                    if (parameterEntity != null && !parameterEntity.isVariadic()) {
                        parameterEntity = null;
                    }
                }
            }
        } else {
            if (variadicMemoryExists) {
                env.error(trace, "Cannot use positional argument after argument unpacking");
            }
            if (varPassed != null) {
                varPassed.add(arg);
            }
            paramCnt++;
        }
        cnt++;
    }
    if (varPassed != null) {
        passed = varPassed.toArray(new Memory[varPassed.size()]);
    }
    return passed;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ParameterEntity(php.runtime.reflection.ParameterEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory)

Aggregations

ParameterEntity (php.runtime.reflection.ParameterEntity)27 Test (org.junit.Test)8 ObjectMemory (php.runtime.memory.ObjectMemory)7 ArrayMemory (php.runtime.memory.ArrayMemory)6 Memory (php.runtime.Memory)5 DumpException (php.runtime.loader.dump.io.DumpException)5 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)5 DumpOutputStream (php.runtime.loader.dump.io.DumpOutputStream)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)3 VariadicMemory (php.runtime.memory.helper.VariadicMemory)3 DocumentComment (php.runtime.reflection.DocumentComment)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ForeachIterator (php.runtime.lang.ForeachIterator)2 ClassEntity (php.runtime.reflection.ClassEntity)2 FunctionEntity (php.runtime.reflection.FunctionEntity)2 MethodEntity (php.runtime.reflection.MethodEntity)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1