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();
}
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();
}
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());
}
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();
}
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;
}
}
Aggregations