Search in sources :

Example 31 with Invoker

use of php.runtime.invoke.Invoker 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 32 with Invoker

use of php.runtime.invoke.Invoker 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 33 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class WrapRegex method replaceWithCallback.

@Signature(@Arg(value = "callback", type = HintType.CALLABLE))
public Memory replaceWithCallback(Environment env, Memory... args) {
    Invoker invoker = Invoker.valueOf(env, null, args[0]);
    StringBuffer sb = new StringBuffer();
    ObjectMemory self = new ObjectMemory(this);
    while (matcher.find()) {
        Memory r = invoker.callNoThrow(self);
        if (r.toValue() == Memory.FALSE)
            break;
        matcher.appendReplacement(sb, r.toString());
    }
    matcher.appendTail(sb);
    return StringMemory.valueOf(sb.toString());
}
Also used : Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory)

Example 34 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class FileObject method findFiles.

@Signature(@Arg(value = "filter", optional = @Optional("NULL")))
public Memory findFiles(final Environment env, Memory... args) {
    File[] result;
    if (args[0].isNull()) {
        result = file.listFiles();
    } else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "Invalid filter value, must be callable");
            return Memory.NULL;
        }
        final TraceInfo trace = env.trace();
        invoker.setTrace(trace);
        result = file.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                FileObject o = new FileObject(env, __class__, dir);
                Memory[] args = new Memory[] { new ObjectMemory(o), new StringMemory(name) };
                return invoker.callNoThrow(args).toBoolean();
            }
        });
    }
    ArrayMemory arr = new ArrayMemory();
    if (result != null) {
        for (File e : result) {
            arr.add(new ObjectMemory(new FileObject(env, __class__, e)));
        }
    }
    return arr.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) Invoker(php.runtime.invoke.Invoker) 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) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo)

Example 35 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class FileObject method hash.

@Signature({ @Arg(value = "algorithm", optional = @Optional("MD5")), @Arg(value = "progress", type = HintType.CALLABLE, optional = @Optional("null")) })
public Memory hash(Environment env, Memory... args) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance(args[0].toString());
    Invoker invoker = Invoker.valueOf(env, null, args[1]);
    byte[] buffer = new byte[FsUtils.BUFFER_SIZE];
    int len;
    FileInputStream is;
    try {
        is = new FileInputStream(file);
        int sum = 0;
        try {
            while ((len = is.read(buffer)) > 0) {
                messageDigest.update(buffer, 0, len);
                sum += len;
                if (invoker != null) {
                    if (invoker.callAny(sum, len).toValue() == Memory.FALSE) {
                        break;
                    }
                }
            }
        } finally {
            is.close();
        }
        return StringMemory.valueOf(DigestUtils.bytesToHex(messageDigest.digest()));
    } catch (FileNotFoundException e) {
        return Memory.NULL;
    } catch (IOException e) {
        return Memory.NULL;
    }
}
Also used : Invoker(php.runtime.invoke.Invoker) MessageDigest(java.security.MessageDigest)

Aggregations

Invoker (php.runtime.invoke.Invoker)50 Memory (php.runtime.Memory)21 ArrayMemory (php.runtime.memory.ArrayMemory)13 ForeachIterator (php.runtime.lang.ForeachIterator)12 LongMemory (php.runtime.memory.LongMemory)9 ObjectMemory (php.runtime.memory.ObjectMemory)9 KeyValueMemory (php.runtime.memory.KeyValueMemory)7 StringMemory (php.runtime.memory.StringMemory)7 Environment (php.runtime.env.Environment)6 ReferenceMemory (php.runtime.memory.ReferenceMemory)5 SplClassLoader (php.runtime.env.SplClassLoader)4 File (java.io.File)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 TraceInfo (php.runtime.env.TraceInfo)2 FileObject (php.runtime.ext.core.classes.stream.FileObject)2 ActionEvent (java.awt.event.ActionEvent)1 MessageDigest (java.security.MessageDigest)1 Scanner (java.util.Scanner)1 FileFilter (javax.swing.filechooser.FileFilter)1