Search in sources :

Example 1 with Closure

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

the class PrintR method printClosure.

@Override
protected void printClosure(Closure closure, int level, Set<Integer> used) {
    ClassEntity classEntity = closure.getReflection();
    writeObjectHeader(Closure.class.getSimpleName());
    if (used.contains(closure.getPointer())) {
        printer.write(" *RECURSION*");
    } else {
        printer.write(StringUtils.repeat(' ', level));
        writeOpen();
        level += PRINT_INDENT;
        used.add(closure.getPointer());
        level -= PRINT_INDENT;
        printer.write(StringUtils.repeat(' ', level));
        writeClose();
        used.remove(closure.getPointer());
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Closure(php.runtime.lang.Closure)

Example 2 with Closure

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

the class VarDump method printClosure.

@Override
protected void printClosure(Closure closure, int level, Set<Integer> used) {
    ClassEntity classEntity = closure.getReflection();
    if (used.contains(closure.getPointer())) {
        printer.write("*RECURSION*\n");
    } else {
        printer.write("object(");
        printer.write(Closure.class.getSimpleName());
        printer.write(")#" + closure.getPointer());
        printer.write(" (" + closure.getUses().length + ") {\n");
        level += PRINT_INDENT;
        used.add(closure.getPointer());
        level -= PRINT_INDENT;
        printer.write(StringUtils.repeat(' ', level));
        printer.write("}\n");
        used.remove(closure.getPointer());
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Closure(php.runtime.lang.Closure)

Example 3 with Closure

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

the class ObjectInvokeHelper method invokeMethod.

public static Memory invokeMethod(IObject iObject, MethodEntity method, Environment env, TraceInfo trace, Memory[] args, boolean checkAccess) throws Throwable {
    ClassEntity clazz = iObject.getReflection();
    if (method == null)
        method = clazz.methodMagicInvoke;
    String className = clazz.getName();
    if (method == null) {
        env.error(trace, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(className + "::__invoke"));
        return Memory.NULL;
    }
    if (checkAccess)
        InvokeHelper.checkAccess(env, trace, method);
    Memory[] passed = InvokeArgumentHelper.makeArguments(env, args, method.getParameters(args == null ? 0 : args.length), className, method.getName(), className, trace);
    Memory result = method.getImmutableResultTyped(env, trace);
    if (result != null) {
        return result;
    }
    if (trace != null) {
        String staticClass = className;
        if (iObject instanceof Closure) {
            staticClass = ((Closure) iObject).getScope();
        }
        if (clazz.isHiddenInCallStack()) {
            env.pushCall(trace, iObject, args, method.getName(), staticClass, staticClass);
        } else {
            env.pushCallEx(trace, iObject, args, method.getName(), method.getClazz(), staticClass);
        }
    }
    try {
        result = method.invokeDynamic(iObject, env, trace, passed);
    } finally {
        if (trace != null)
            env.popCall();
    }
    return result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Closure(php.runtime.lang.Closure) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 4 with Closure

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

the class Environment method __tick.

/**
 *** UTILS ****
 */
public void __tick(TraceInfo trace, ArrayMemory locals) {
    TickHandler tickHandler = scope.getTickHandler();
    if (tickHandler != null) {
        IObject $this = this.getLateObject();
        if ($this != null) {
            Memory value = ObjectMemory.valueOf($this);
            if ($this instanceof Closure) {
                value = ((Closure) $this).getSelf();
            }
            if (value.isObject()) {
                locals.putAsKeyString("this", value);
            }
        }
        tickHandler.onTick(this, trace, locals);
    }
}
Also used : IObject(php.runtime.lang.IObject) Closure(php.runtime.lang.Closure) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) ArrayMemory(php.runtime.memory.ArrayMemory) Memory(php.runtime.Memory) TickHandler(php.runtime.env.handler.TickHandler)

Example 5 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.setUsesStackTrace(true);
    m.setClazz(closureEntity1);
    m.setName("__invoke");
    closureEntity1.addMethod(m, null);
    closureEntity1.doneDeclare();
    Closure tmp = new Closure(env, closureEntity1, new ObjectMemory(env.getLateObject()), null, Memory.CONST_EMPTY_ARRAY) {

        @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)

Aggregations

Closure (php.runtime.lang.Closure)9 Memory (php.runtime.Memory)7 ObjectMemory (php.runtime.memory.ObjectMemory)7 ArrayMemory (php.runtime.memory.ArrayMemory)5 ClassEntity (php.runtime.reflection.ClassEntity)5 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 StringMemory (php.runtime.memory.StringMemory)4 Environment (php.runtime.env.Environment)2 IObject (php.runtime.lang.IObject)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 StringWriter (java.io.StringWriter)1 CallStack (php.runtime.env.CallStack)1 TickHandler (php.runtime.env.handler.TickHandler)1 CriticalException (php.runtime.exceptions.CriticalException)1 PlainPrinter (php.runtime.memory.output.PlainPrinter)1 MethodEntity (php.runtime.reflection.MethodEntity)1 ParameterEntity (php.runtime.reflection.ParameterEntity)1 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)1