Search in sources :

Example 86 with ArrayMemory

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

the class ItemsUtils method sortByKeys.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "comparator", type = HintType.CALLABLE, optional = @Optional("null")), @Arg(value = "saveKeys", optional = @Optional("false")) })
public static Memory sortByKeys(Environment env, Memory... args) {
    boolean saveKeys = args[2].toBoolean();
    List<KeyValueMemory> tmp = new ArrayList<KeyValueMemory>();
    ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
    while (iterator.next()) {
        tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
    }
    final Invoker invoker = args[0].isNull() ? null : Invoker.valueOf(env, null, args[1]);
    Collections.sort(tmp, new Comparator<KeyValueMemory>() {

        @Override
        public int compare(KeyValueMemory o1, KeyValueMemory o2) {
            if (invoker == null)
                return o1.key.compareTo(o2.key);
            else
                return invoker.callNoThrow(o1.key, o2.key).toInteger();
        }
    });
    ArrayMemory r = new ArrayMemory();
    Iterator<KeyValueMemory> iterator1 = tmp.iterator();
    while (iterator1.hasNext()) {
        if (saveKeys)
            r.add(iterator1.next());
        else
            r.add(iterator1.next().value);
        iterator1.remove();
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Invoker(php.runtime.invoke.Invoker)

Example 87 with ArrayMemory

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

the class ItemsUtils method sort.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "comparator", type = HintType.CALLABLE, optional = @Optional("null")), @Arg(value = "saveKeys", optional = @Optional("false")) })
public static Memory sort(Environment env, Memory... args) {
    boolean saveKeys = args[2].toBoolean();
    Memory[] sortTmp;
    if (!saveKeys && args[0].isArray()) {
        Memory[] original = args[0].toValue(ArrayMemory.class).values(true);
        sortTmp = Arrays.copyOf(original, original.length);
    } else {
        ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
        List<Memory> tmp = new ArrayList<Memory>();
        while (iterator.next()) {
            if (saveKeys)
                tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
            else
                tmp.add(iterator.getValue().toImmutable());
        }
        sortTmp = tmp.toArray(new Memory[tmp.size()]);
        tmp.clear();
    }
    if (args[1].isNull()) {
        Arrays.sort(sortTmp, new Comparator<Memory>() {

            @Override
            public int compare(Memory o1, Memory o2) {
                return o1.compareTo(o2);
            }
        });
    } else {
        final Invoker invoker = Invoker.valueOf(env, null, args[1]);
        Arrays.sort(sortTmp, new Comparator<Memory>() {

            @Override
            public int compare(Memory o1, Memory o2) {
                return invoker.callNoThrow(o1, o2).toInteger();
            }
        });
    }
    ArrayMemory r = new ArrayMemory();
    for (Memory el : sortTmp) {
        r.add(el);
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 88 with ArrayMemory

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

the class WrapTime method __debugInfo.

@Signature
public Memory __debugInfo(Environment env, Memory... args) {
    ArrayMemory r = new ArrayMemory();
    r.refOfIndex("*time").assign(date.getTime());
    r.refOfIndex("*timeZone").assign(timeZone.getID());
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory)

Example 89 with ArrayMemory

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

the class WrapTime method add.

@Signature(@Arg(value = "args", type = HintType.ARRAY))
public Memory add(Environment env, Memory... args) {
    ArrayMemory arg = args[0].toValue(ArrayMemory.class);
    Memory year = arg.getByScalar("year");
    Memory month = arg.getByScalar("month");
    Memory day = arg.getByScalar("day");
    Memory hour = arg.getByScalar("hour");
    Memory min = arg.getByScalar("min");
    Memory sec = arg.getByScalar("sec");
    Memory millis = arg.getByScalar("millis");
    Calendar calendar1 = (Calendar) calendar.clone();
    if (year != null)
        calendar1.add(Calendar.YEAR, year.toInteger());
    if (month != null)
        calendar1.add(Calendar.MONTH, month.toInteger());
    if (day != null)
        calendar1.add(Calendar.DAY_OF_MONTH, day.toInteger());
    if (hour != null)
        calendar1.add(Calendar.HOUR_OF_DAY, hour.toInteger());
    if (min != null)
        calendar1.add(Calendar.MINUTE, min.toInteger());
    if (sec != null)
        calendar1.add(Calendar.SECOND, sec.toInteger());
    if (millis != null)
        calendar1.add(Calendar.MILLISECOND, millis.toInteger());
    return new ObjectMemory(new WrapTime(env, calendar1.getTime(), timeZone));
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) Calendar(java.util.Calendar)

Example 90 with ArrayMemory

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

the class WrapTimeFormat method __debugInfo.

@Signature
public Memory __debugInfo(Environment env, Memory... args) {
    ArrayMemory r = new ArrayMemory();
    r.refOfIndex("*format").assign(format);
    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