Search in sources :

Example 1 with RunnableInvoker

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

the class WrapPromise method resolve.

@Signature
public static WrapPromise resolve(Environment env, Memory result) {
    if (result.instanceOf(WrapPromise.class)) {
        WrapPromise promise = new WrapPromise(env);
        promise.__construct(env, new RunnableInvoker(env, new Callback<Memory, Memory[]>() {

            @Override
            public Memory call(Memory[] args) {
                try {
                    return ObjectMemory.valueOf(promise.then(env, Invoker.create(env, args[0]), Invoker.create(env, args[1])));
                } catch (Exception e) {
                    env.forwardThrow(e);
                    return Memory.NULL;
                }
            }
        }));
        return promise;
    } else {
        return new WrapPromise(env, new Callback<Memory, Memory[]>() {

            @Override
            public Memory call(Memory[] args) {
                // resolve.
                return Invoker.create(env, args[0]).callAny(result);
            }
        });
    }
}
Also used : Callback(php.runtime.common.Callback) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) RunnableInvoker(php.runtime.invoke.RunnableInvoker) BaseBaseException(php.runtime.lang.exception.BaseBaseException) JavaException(php.runtime.ext.java.JavaException)

Example 2 with RunnableInvoker

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

the class WrapPromise method all.

@Signature
public static WrapPromise all(Environment env, ForeachIterator promises) {
    Map<String, WrapPromise> list = new LinkedHashMap<>();
    while (promises.next()) {
        Memory value = promises.getValue();
        if (value.instanceOf(WrapPromise.class)) {
            list.put(promises.getStringKey(), value.toObject(WrapPromise.class));
        }
    }
    if (list.isEmpty()) {
        return resolve(env, Memory.NULL);
    }
    int max = list.size();
    ArrayMemory result = ArrayMemory.createListed(max);
    for (String key : list.keySet()) {
        result.put(key, Memory.NULL);
    }
    return new WrapPromise(env, new Callback<Memory, Memory[]>() {

        @Override
        public Memory call(Memory[] args) {
            final boolean[] done = { false };
            Invoker resolve = Invoker.create(env, args[0]);
            Invoker reject = Invoker.create(env, args[1]);
            final int[] count = { 0 };
            for (Map.Entry<String, WrapPromise> entry : list.entrySet()) {
                WrapPromise promise = entry.getValue();
                String key = entry.getKey();
                try {
                    promise.then(env, new RunnableInvoker(env, new Callback<Memory, Memory[]>() {

                        @Override
                        public Memory call(Memory[] args) {
                            if (!done[0]) {
                                result.put(key, args[0]);
                                count[0]++;
                                if (max == count[0]) {
                                    done[0] = true;
                                    resolve.callNoThrow(result);
                                }
                            }
                            return Memory.NULL;
                        }
                    }), new RunnableInvoker(env, new Callback<Memory, Memory[]>() {

                        @Override
                        public Memory call(Memory[] memories) {
                            if (!done[0]) {
                                done[0] = true;
                                reject.callNoThrow(args[0]);
                            }
                            return Memory.NULL;
                        }
                    }));
                } catch (Exception e) {
                    env.forwardThrow(e);
                }
            }
            return Memory.NULL;
        }
    });
}
Also used : Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) RunnableInvoker(php.runtime.invoke.RunnableInvoker) BaseBaseException(php.runtime.lang.exception.BaseBaseException) JavaException(php.runtime.ext.java.JavaException) LinkedHashMap(java.util.LinkedHashMap) ArrayMemory(php.runtime.memory.ArrayMemory) RunnableInvoker(php.runtime.invoke.RunnableInvoker) Invoker(php.runtime.invoke.Invoker)

Aggregations

Memory (php.runtime.Memory)2 JavaException (php.runtime.ext.java.JavaException)2 RunnableInvoker (php.runtime.invoke.RunnableInvoker)2 BaseBaseException (php.runtime.lang.exception.BaseBaseException)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 ObjectMemory (php.runtime.memory.ObjectMemory)2 StringMemory (php.runtime.memory.StringMemory)2 LinkedHashMap (java.util.LinkedHashMap)1 Callback (php.runtime.common.Callback)1 Invoker (php.runtime.invoke.Invoker)1