Search in sources :

Example 21 with Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class WrapThreadPool method schedule.

@Signature({ @Arg(value = "runnable", type = HintType.CALLABLE), @Arg("delay"), @Arg(value = "env", typeClass = "php\\lang\\Environment", optional = @Optional("NULL")) })
public Memory schedule(final Environment env, Memory... args) {
    final Environment _env = args[2].isNull() ? env : args[2].toObject(WrapEnvironment.class).getWrapEnvironment();
    final Invoker invoker = Invoker.valueOf(_env, null, args[0]);
    ScheduledFuture<Memory> future = getScheduledExecutorService(env).schedule(new Callable<Memory>() {

        @Override
        public Memory call() throws Exception {
            Environment.addThreadSupport(_env);
            return invoker.callNoThrow();
        }
    }, args[1].toLong(), TimeUnit.MILLISECONDS);
    return new ObjectMemory(new WrapFuture(env, future));
}
Also used : Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Environment(php.runtime.env.Environment)

Example 22 with Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class FunctionEntity method getClosure.

@Override
public Closure getClosure(Environment env) {
    if (cachedClosure != null)
        return cachedClosure;
    final FunctionEntity bind = this;
    final ClosureEntity closureEntity1 = new ClosureEntity(this.getContext());
    closureEntity1.setParent(env.scope.fetchUserClass(Closure.class));
    closureEntity1.parameters = this.parameters;
    closureEntity1.setReturnReference(this.isReturnReference());
    MethodEntity m = new MethodEntity(this);
    m.setClazz(closureEntity1);
    m.setName("__invoke");
    closureEntity1.addMethod(m, null);
    closureEntity1.doneDeclare();
    Closure tmp = new Closure(env, closureEntity1, new ObjectMemory(env.getLateObject()), null, new Memory[0]) {

        @Override
        public Memory __invoke(Environment e, Memory... args) {
            try {
                return bind.invoke(e, e.peekCall(0).trace, args);
            } catch (RuntimeException e1) {
                throw e1;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }

        @Override
        public Memory getOrCreateStatic(String name) {
            return Memory.NULL;
        }

        @Override
        public ClassEntity getReflection() {
            return closureEntity1;
        }
    };
    try {
        m.setNativeMethod(tmp.getClass().getDeclaredMethod("__invoke", Environment.class, Memory[].class));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return cachedClosure = tmp;
}
Also used : AbstractFunctionEntity(php.runtime.reflection.support.AbstractFunctionEntity) Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Environment(php.runtime.env.Environment)

Example 23 with Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class MethodEntity method getClosure.

/*
            ClassNode classNode = clazz.getClassNode();
            for(Object m : classNode.methods) {
                MethodNode method = (MethodNode) m;
                if (method.name.equals(getInternalName()) ){
                    return cachedMethodNode = method;
                }
            }

        throw new CriticalException("Cannot find MethodNode for method - " + name + "(" + getSignatureString(true) + ")");
    */
public Closure getClosure(Environment env, final IObject object) {
    if (cachedClosure != null)
        return cachedClosure;
    final MethodEntity bind = this;
    final ClosureEntity closureEntity1 = new ClosureEntity(this.getContext());
    closureEntity1.setParent(env.scope.fetchUserClass(Closure.class));
    closureEntity1.parameters = this.parameters;
    closureEntity1.setReturnReference(this.isReturnReference());
    MethodEntity m = new MethodEntity(this);
    m.setClazz(closureEntity1);
    m.setName("__invoke");
    closureEntity1.addMethod(m, null);
    closureEntity1.doneDeclare();
    Closure tmp = new Closure(env, closureEntity1, new ObjectMemory(env.getLateObject()), clazz.getName(), new Memory[0]) {

        @Override
        public Memory __invoke(Environment e, Memory... args) {
            try {
                if (object == null)
                    return bind.invokeStatic(e, args);
                else
                    return bind.invokeDynamic(object, e, args);
            } catch (RuntimeException e1) {
                throw e1;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }

        @Override
        public Memory getOrCreateStatic(String name) {
            return Memory.NULL;
        }

        @Override
        public ClassEntity getReflection() {
            return closureEntity1;
        }
    };
    try {
        m.setNativeMethod(tmp.getClass().getDeclaredMethod("__invoke", Environment.class, Memory[].class));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return cachedClosure = tmp;
}
Also used : Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Environment(php.runtime.env.Environment)

Aggregations

Environment (php.runtime.env.Environment)23 Memory (php.runtime.Memory)13 Invoker (php.runtime.invoke.Invoker)6 ObjectMemory (php.runtime.memory.ObjectMemory)5 ArrayAccess (php.runtime.lang.spl.ArrayAccess)4 CompileScope (php.runtime.env.CompileScope)3 TraceInfo (php.runtime.env.TraceInfo)3 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 Context (php.runtime.env.Context)2 Closure (php.runtime.lang.Closure)2 IObject (php.runtime.lang.IObject)2 Launcher (php.runtime.launcher.Launcher)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1