Search in sources :

Example 11 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity 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

ClosureEntity (php.runtime.reflection.helper.ClosureEntity)11 ClassEntity (php.runtime.reflection.ClassEntity)5 Memory (php.runtime.Memory)4 FunctionEntity (php.runtime.reflection.FunctionEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 Environment (php.runtime.env.Environment)3 Closure (php.runtime.lang.Closure)3 DumpException (php.runtime.loader.dump.io.DumpException)3 ObjectMemory (php.runtime.memory.ObjectMemory)3 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)2 ConstantEntity (php.runtime.reflection.ConstantEntity)2 ParameterEntity (php.runtime.reflection.ParameterEntity)2 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)2 Method (java.lang.reflect.Method)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 ClosureStmtToken (org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 YieldExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.YieldExprToken)1 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)1 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)1