Search in sources :

Example 56 with Invoker

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;
        }
    }));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory)

Example 57 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);
    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());
}
Also used : Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory)

Example 58 with Invoker

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);
}
Also used : Scanner(java.util.Scanner) Invoker(php.runtime.invoke.Invoker) LongMemory(php.runtime.memory.LongMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) BinaryMemory(php.runtime.memory.BinaryMemory) StringMemory(php.runtime.memory.StringMemory)

Example 59 with Invoker

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;
}
Also used : SplClassLoader(php.runtime.env.SplClassLoader) Invoker(php.runtime.invoke.Invoker)

Example 60 with Invoker

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();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) SplClassLoader(php.runtime.env.SplClassLoader) Invoker(php.runtime.invoke.Invoker)

Aggregations

Invoker (php.runtime.invoke.Invoker)61 Memory (php.runtime.Memory)29 ArrayMemory (php.runtime.memory.ArrayMemory)13 ForeachIterator (php.runtime.lang.ForeachIterator)12 LongMemory (php.runtime.memory.LongMemory)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 StringMemory (php.runtime.memory.StringMemory)10 Environment (php.runtime.env.Environment)7 Signature (php.runtime.annotation.Reflection.Signature)5 KeyValueMemory (php.runtime.memory.KeyValueMemory)5 ReferenceMemory (php.runtime.memory.ReferenceMemory)5 SplClassLoader (php.runtime.env.SplClassLoader)4 FileObject (php.runtime.ext.core.classes.stream.FileObject)4 DynamicMethodInvoker (php.runtime.invoke.DynamicMethodInvoker)4 RunnableInvoker (php.runtime.invoke.RunnableInvoker)3 File (java.io.File)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)2 WebSocketHandler (org.eclipse.jetty.websocket.server.WebSocketHandler)2