Search in sources :

Example 6 with Closure

use of php.runtime.lang.Closure in project jphp by jphp-compiler.

the class CallStackItem method toString.

public String toString(boolean withArgs) {
    StringBuilder sb = new StringBuilder();
    if (object instanceof Closure)
        sb.append("{closure}");
    else if (clazz != null) {
        sb.append(clazz);
        if (object == null)
            sb.append("::");
        else
            sb.append("->");
        sb.append(function);
    } else if (function != null) {
        sb.append(function);
    } else
        sb.append("<internal>");
    sb.append("(");
    if (withArgs) {
        StringWriter writer = new StringWriter();
        PlainPrinter printer = new PlainPrinter(null, writer);
        int i = 0;
        if (args != null)
            for (Memory arg : args) {
                printer.print(arg);
                if (i != args.length - 1)
                    writer.append(", ");
                i++;
            }
        sb.append(writer.toString());
    }
    sb.append(")");
    if (trace != null && trace != TraceInfo.UNKNOWN) {
        sb.append(" called at [");
        sb.append(trace.getFileName());
        sb.append(":");
        sb.append(trace.getStartLine() + 1);
        sb.append("]");
    }
    return sb.toString();
}
Also used : Closure(php.runtime.lang.Closure) StringWriter(java.io.StringWriter) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) PlainPrinter(php.runtime.memory.output.PlainPrinter)

Example 7 with Closure

use of php.runtime.lang.Closure 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 8 with Closure

use of php.runtime.lang.Closure 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

Closure (php.runtime.lang.Closure)8 Memory (php.runtime.Memory)6 ObjectMemory (php.runtime.memory.ObjectMemory)6 ClassEntity (php.runtime.reflection.ClassEntity)5 Environment (php.runtime.env.Environment)3 ArrayMemory (php.runtime.memory.ArrayMemory)3 ReferenceMemory (php.runtime.memory.ReferenceMemory)2 StringMemory (php.runtime.memory.StringMemory)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CriticalException (php.runtime.exceptions.CriticalException)1 IObject (php.runtime.lang.IObject)1 PlainPrinter (php.runtime.memory.output.PlainPrinter)1 MethodEntity (php.runtime.reflection.MethodEntity)1 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)1