Search in sources :

Example 91 with ForeachIterator

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

Example 92 with ForeachIterator

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

the class WrapSourceMap method insertLines.

@Signature({ @Arg(value = "inserts", type = HintType.ARRAY), @Arg("allCountLines") })
public Memory insertLines(Environment env, Memory... args) {
    ArrayMemory _inserts = args[0].toValue(ArrayMemory.class);
    int[][] inserts = new int[_inserts.size()][];
    ForeachIterator iterator = _inserts.getNewIterator(env);
    int i = 0;
    while (iterator.next()) {
        Memory value = iterator.getValue();
        inserts[i++] = new int[] { value.valueOfIndex(0).toInteger(), value.valueOfIndex(1).toInteger() };
    }
    getWrappedObject().insertLines(inserts, args[1].toInteger());
    return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) StringMemory(php.runtime.memory.StringMemory) Signature(php.runtime.annotation.Reflection.Signature)

Example 93 with ForeachIterator

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

the class LangFunctions method extract.

public static int extract(Environment env, TraceInfo trace, @Runtime.GetLocals ArrayMemory locals, @Runtime.Reference Memory array, int extractType, Memory _prefix) {
    if (expecting(env, trace, 1, array, Memory.Type.ARRAY)) {
        boolean isRefs = (extractType & LangConstants.EXTR_REFS) == LangConstants.EXTR_REFS;
        ForeachIterator iterator = array.getNewIterator(env, isRefs, false);
        int count = 0;
        if (extractType == LangConstants.EXTR_PREFIX_ALL || extractType == LangConstants.EXTR_PREFIX_IF_EXISTS || extractType == LangConstants.EXTR_PREFIX_INVALID || extractType == LangConstants.EXTR_PREFIX_SAME)
            if (_prefix.isNull()) {
                env.warning(trace, "extract(): specified extract type requires the prefix parameter");
                return 0;
            }
        String prefix = _prefix.isNull() ? "" : _prefix.concat("_");
        if (!prefix.isEmpty())
            if (!GrammarUtils.isValidName(prefix)) {
                env.warning(trace, "extract(): prefix is not a valid identifier");
                return 0;
            }
        while (iterator.next()) {
            Object key = iterator.getKey();
            String keyS = key.toString();
            String var;
            Memory value = iterator.getValue();
            if (!isRefs)
                value = value.toImmutable();
            switch(extractType) {
                case LangConstants.EXTR_OVERWRITE:
                    if (GrammarUtils.isValidName(keyS)) {
                        locals.refOfIndex(keyS).assign(value);
                        count++;
                    }
                    break;
                case LangConstants.EXTR_SKIP:
                    if (GrammarUtils.isValidName(keyS) && locals.valueOfIndex(keyS).isUndefined()) {
                        locals.refOfIndex(keyS).assign(value);
                        count++;
                    }
                    break;
                case LangConstants.EXTR_PREFIX_SAME:
                    if (!locals.valueOfIndex(keyS).isUndefined()) {
                        var = prefix.concat(keyS);
                        if (GrammarUtils.isValidName(var)) {
                            locals.refOfIndex(var).assign(value);
                            count++;
                        }
                    } else if (GrammarUtils.isValidName(keyS)) {
                        locals.refOfIndex(keyS).assign(value);
                        count++;
                    }
                    break;
                case LangConstants.EXTR_PREFIX_ALL:
                    var = prefix.concat(keyS);
                    if (GrammarUtils.isValidName(var)) {
                        locals.refOfIndex(prefix.concat(keyS)).assign(value);
                        count++;
                    }
                    break;
                case LangConstants.EXTR_PREFIX_INVALID:
                    if (GrammarUtils.isValidName(keyS)) {
                        locals.refOfIndex(keyS).assign(value);
                        count++;
                    } else {
                        var = prefix.concat(keyS);
                        if (GrammarUtils.isValidName(var)) {
                            locals.refOfIndex(var).assign(value);
                            count++;
                        }
                    }
                    break;
                case LangConstants.EXTR_IF_EXISTS:
                    if (GrammarUtils.isValidName(keyS))
                        if (!locals.valueOfIndex(keyS).isUndefined()) {
                            locals.refOfIndex(keyS).assign(value);
                            count++;
                        }
                    break;
                case LangConstants.EXTR_PREFIX_IF_EXISTS:
                    if (!locals.valueOfIndex(keyS).isUndefined()) {
                        var = prefix.concat(keyS);
                        if (GrammarUtils.isValidName(var)) {
                            locals.refOfIndex(var).assign(value);
                            count++;
                        }
                    }
                    break;
            }
        }
        return count;
    } else
        return 0;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) IObject(php.runtime.lang.IObject)

Example 94 with ForeachIterator

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

the class LangFunctions method get_object_vars.

public static Memory get_object_vars(Environment env, TraceInfo trace, Memory object) {
    if (expecting(env, trace, 1, object, Memory.Type.OBJECT)) {
        ObjectMemory o = object.toValue(ObjectMemory.class);
        ArrayMemory props = o.value.getProperties();
        ClassEntity entity = o.getReflection();
        ClassEntity context = env.getLastClassOnStack();
        ForeachIterator iterator = props.foreachIterator(false, false);
        ArrayMemory result = new ArrayMemory();
        while (iterator.next()) {
            PropertyEntity prop = entity.findProperty(iterator.getKey().toString());
            if (prop == null || prop.canAccess(env, context) == 0)
                result.refOfIndex(prop == null ? iterator.getKey().toString() : prop.getName()).assign(iterator.getValue());
        }
        return result.toConstant();
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 95 with ForeachIterator

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

the class ArrayMemory method toIntMap.

public Map<String, Integer> toIntMap() {
    Map<String, Integer> r = new LinkedHashMap<String, Integer>();
    ForeachIterator iterator = foreachIterator(false, false);
    while (iterator.next()) {
        r.put(iterator.getKey().toString(), iterator.getValue().toInteger());
    }
    return r;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3