Search in sources :

Example 6 with ArrayMemory

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

the class SPLFunctions method class_implements.

public static Memory class_implements(Environment env, TraceInfo trace, Memory object, boolean autoLoad) {
    ClassEntity entity;
    if (object.isObject()) {
        entity = object.toValue(ObjectMemory.class).getReflection();
    } else {
        entity = env.fetchClass(object.toString(), autoLoad);
    }
    if (entity == null) {
        env.warning(trace, "class_implements(): Class %s does not exist and could not be loaded", object.toString());
        return Memory.FALSE;
    }
    ArrayMemory result = new ArrayMemory();
    do {
        for (ClassEntity el : entity.getInterfaces().values()) {
            result.refOfIndex(el.getName()).assign(el.getName());
        }
        entity = entity.getParent();
        if (entity == null)
            break;
    } while (true);
    return result.toConstant();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory)

Example 7 with ArrayMemory

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

the class ReflectionFunction method invokeArgs.

@Signature(@Arg(value = "args", type = HintType.ARRAY))
public Memory invokeArgs(Environment env, Memory... args) throws Throwable {
    ArrayMemory value = args[0].toValue(ArrayMemory.class);
    Memory[] passed = value.values();
    return invoke(env, passed);
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 8 with ArrayMemory

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

the class InvokeArgumentHelper method makeArguments.

public static Memory[] makeArguments(Environment env, Memory[] args, ParameterEntity[] parameters, String originClassName, String originMethodName, TraceInfo trace) {
    if (parameters == null)
        return args;
    args = unpackArgs(env, trace, args, parameters);
    Memory[] passed = args;
    if ((args == null && parameters.length > 0) || (args != null && args.length < parameters.length)) {
        passed = new Memory[parameters.length];
        if (args != null && args.length > 0) {
            System.arraycopy(args, 0, passed, 0, args.length);
        }
    }
    int i = 0;
    if (passed != null) {
        boolean variadicMemoryExists = false;
        for (ParameterEntity param : parameters) {
            Memory arg = passed[i];
            if (param.isVariadic()) {
                ArrayMemory variadicArgs = new ArrayMemory();
                int _i = i;
                while (arg != null) {
                    if (arg instanceof VariadicMemory) {
                        variadicMemoryExists = true;
                        ForeachIterator iterator = arg.getNewIterator(env, param.isReference(), false);
                        if (iterator == null) {
                            env.warning(trace, INVALID_TYPE_MESSAGE);
                        } else {
                            makeVariadic(iterator, variadicArgs, param, env, trace, _i, originClassName, originMethodName);
                        }
                    } else {
                        if (variadicMemoryExists) {
                            env.error(trace, "Cannot use positional argument after argument unpacking");
                        }
                        if (!param.checkTypeHinting(env, arg)) {
                            invalidType(env, trace, param, _i + 1, arg, originClassName, originMethodName);
                        }
                        variadicArgs.add(makeValue(param, arg, env, trace));
                    }
                    i++;
                    if (i < passed.length) {
                        arg = passed[i];
                    } else {
                        break;
                    }
                }
                passed[_i] = variadicArgs;
                break;
            }
            if (arg == null) {
                Memory def = param.getDefaultValue();
                if (def != null) {
                    if (!param.isReference()) {
                        passed[i] = param.isMutable() ? def.toImmutable(env, trace) : def;
                    } else {
                        passed[i] = new ReferenceMemory(param.isMutable() ? def.toImmutable(env, trace) : def);
                    }
                } else {
                    if (param.getTypeClass() != null) {
                        invalidType(env, trace, param, i + 1, null, originClassName, originMethodName);
                    }
                    env.error(trace, ErrorType.E_ERROR, Messages.ERR_MISSING_ARGUMENT, (i + 1) + " ($" + param.getName() + ")", originMethodName == null ? originClassName : originClassName + "::" + originMethodName);
                    passed[i] = param.isReference() ? new ReferenceMemory() : Memory.NULL;
                }
            } else {
                if (param.isReference()) {
                    if (!arg.isReference() && !arg.isObject()) {
                        env.error(trace, ErrorType.E_ERROR, "Only variables can be passed by reference");
                        passed[i] = new ReferenceMemory(arg);
                    }
                } else {
                    passed[i] = param.isMutable() ? arg.toImmutable() : arg.toValue();
                }
            }
            if (!param.checkTypeHinting(env, passed[i])) {
                invalidType(env, trace, param, i + 1, passed[i], originClassName, originMethodName);
            }
            i++;
        }
        if (!variadicMemoryExists) {
            for (int j = parameters.length; j < passed.length; j++) {
                passed[j] = passed[j].toImmutable();
            }
        }
    }
    return passed;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) 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)

Example 9 with ArrayMemory

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

the class InvokeArgumentHelper method makeVariadic.

public static void makeVariadic(ForeachIterator iterator, ArrayMemory variadicArray, ParameterEntity param, Environment env, TraceInfo trace, int index, String originClassName, String originMethodName) {
    while (iterator.next()) {
        Memory arg = iterator.getValue();
        if (!param.checkTypeHinting(env, arg)) {
            invalidType(env, trace, param, index + 1, arg, originClassName, originMethodName);
        }
        variadicArray.add(makeValue(param, iterator.getValue(), env, trace));
    }
}
Also used : 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 10 with ArrayMemory

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

the class WrapLocale method __debugInfo.

@Signature
public Memory __debugInfo(Environment env, Memory... args) {
    ArrayMemory r = new ArrayMemory();
    r.refOfIndex("*language").assign(locale.getLanguage());
    r.refOfIndex("*country").assign(locale.getCountry());
    r.refOfIndex("*variant").assign(locale.getVariant());
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory)

Aggregations

ArrayMemory (php.runtime.memory.ArrayMemory)148 Memory (php.runtime.Memory)60 ForeachIterator (php.runtime.lang.ForeachIterator)41 ObjectMemory (php.runtime.memory.ObjectMemory)35 LongMemory (php.runtime.memory.LongMemory)31 StringMemory (php.runtime.memory.StringMemory)29 ReferenceMemory (php.runtime.memory.ReferenceMemory)25 KeyValueMemory (php.runtime.memory.KeyValueMemory)17 Invoker (php.runtime.invoke.Invoker)9 ClassEntity (php.runtime.reflection.ClassEntity)9 Signature (php.runtime.annotation.Reflection.Signature)6 IObject (php.runtime.lang.IObject)6 Map (java.util.Map)4 Test (org.junit.Test)4 DoubleMemory (php.runtime.memory.DoubleMemory)4 ConcurrentEnvironment (php.runtime.env.ConcurrentEnvironment)3 TraceInfo (php.runtime.env.TraceInfo)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ParameterEntity (php.runtime.reflection.ParameterEntity)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2