use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.
the class WrapFlow method group.
@Signature({ @Arg(value = "callback", type = HintType.CALLABLE) })
public Memory group(Environment env, Memory... args) {
final Invoker invoker = Invoker.valueOf(env, null, args[0]);
final ForeachIterator iterator = getSelfIterator(env);
return new ObjectMemory(new WrapFlow(env, new ForeachIterator(false, false, false) {
protected Memory key;
@Override
protected boolean init() {
key = Memory.CONST_INT_M1;
return true;
}
@Override
protected boolean nextValue() {
Memory r = new ArrayMemory();
boolean done = false;
while (iterator.next()) {
done = true;
if (withKeys)
r.refOfIndex(iterator.getMemoryKey()).assign(iterator.getValue());
else
r.refOfPush().assign(iterator.getValue());
if (call(iterator, invoker).toBoolean())
break;
}
if (done) {
currentKeyMemory = key.inc();
currentKey = currentKeyMemory;
currentValue = r;
}
return done;
}
@Override
protected boolean prevValue() {
return false;
}
@Override
public void reset() {
iterator.reset();
key = Memory.CONST_INT_M1;
}
}));
}
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);
int i = 0;
while (matcher.find()) {
Memory r = invoker.callNoThrow(self, LongMemory.valueOf(i));
if (r.toValue() == Memory.FALSE)
break;
matcher.appendReplacement(sb, r.toString());
i++;
}
matcher.appendTail(sb);
return StringMemory.valueOf(sb.toString());
}
use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.
the class Stream method eachLine.
@Signature({ @Arg(value = "callback", type = HintType.CALLABLE), @Arg(value = "encoding", optional = @Optional("null")) })
public Memory eachLine(Environment env, Memory... args) throws Throwable {
Invoker invoker = Invoker.create(env, args[0]);
InputStream is = getInputStream(env, this);
Scanner scanner = new Scanner(is, args[1].isNull() ? env.getDefaultCharset().name() : args[1].toString());
int count = 0;
while (scanner.hasNextLine()) {
Memory call = invoker.call(StringMemory.valueOf(scanner.nextLine()));
if (call.toBoolean()) {
break;
}
count++;
}
return LongMemory.valueOf(count);
}
use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.
the class SPLFunctions method spl_autoload_register.
public static boolean spl_autoload_register(Environment env, TraceInfo trace, Memory callback, boolean _throw, boolean prepend) {
Invoker invoker = expectingCallback(env, trace, 1, callback);
if (invoker == null)
return false;
env.registerAutoloader(new SplClassLoader(invoker, callback), prepend);
return true;
}
use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.
the class SPLFunctions method spl_autoload_functions.
public static Memory spl_autoload_functions(Environment env) {
ArrayMemory result = new ArrayMemory();
for (SplClassLoader loader : env.getClassLoaders()) {
result.add(loader.getCallback().toImmutable());
}
if (result.size() == 0) {
if (env.__autoload == null) {
Invoker invoker = Invoker.valueOf(env, null, __autoloadMethod);
if (invoker != null) {
env.__autoload = new SplClassLoader(invoker, __autoloadMethod);
result.add(env.__autoload.getCallback().toImmutable());
}
}
}
return result.toConstant();
}
Aggregations