Search in sources :

Example 6 with Invoker

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

the class SPLFunctions method iterator_apply.

public static long iterator_apply(Environment env, TraceInfo trace, Memory object, Memory callback, Memory args) throws Throwable {
    if (expectingImplement(env, trace, 1, object, Traversable.class)) {
        Invoker invoker = expectingCallback(env, trace, 2, callback);
        if (invoker == null)
            return 0;
        if (args != null && !expecting(env, trace, 3, args, Memory.Type.ARRAY)) {
            return 0;
        }
        ForeachIterator iterator = object.getNewIterator(env, false, false);
        Memory[] values = args == null ? null : args.toValue(ArrayMemory.class).values(true);
        int i = 0;
        while (iterator.next()) {
            if (!invoker.call(values).toBoolean())
                break;
            i++;
        }
        return i;
    } else
        return 0;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 7 with Invoker

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

the class ReflectionParameter method __construct.

@Signature({ @Arg("function"), @Arg("parameter") })
public Memory __construct(Environment env, Memory... args) {
    ParameterEntity[] parameters = null;
    if (args[0].isClosure()) {
        ClosureEntity tmp = (ClosureEntity) args[0].toValue(ObjectMemory.class).getReflection();
        parameters = tmp.parameters;
    } else if (args[0].isArray()) {
        Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "%s does not exists", args[0].toString());
            return Memory.NULL;
        }
        parameters = invoker.getParameters();
    } else {
        String name = args[0].toString();
        if (name.contains("::")) {
            Invoker invoker = Invoker.valueOf(env, null, args[0]);
            if (invoker == null) {
                exception(env, "%s does not exists", args[0].toString());
                return Memory.NULL;
            }
            parameters = invoker.getParameters();
        } else {
            FunctionEntity tmp = env.fetchFunction(name);
            functionEntity = tmp;
            if (tmp == null) {
                exception(env, "Function %s does not exist", args[0].toString());
                return Memory.NULL;
            }
            if (tmp.isInternal()) {
                exception(env, "%s(): ReflectionParameter does not support internal functions", tmp.getName());
                return Memory.NULL;
            }
            parameters = tmp.getParameters();
        }
    }
    entity = null;
    String name = args[1].toString();
    if (parameters != null) {
        if (args[1].isNumber()) {
            int index = args[1].toInteger();
            if (index >= 0 && index < parameters.length) {
                entity = parameters[index];
                position = index;
            }
        } else {
            int i = 0;
            for (ParameterEntity e : parameters) {
                if (e.getName().equals(name)) {
                    entity = e;
                    position = i;
                    break;
                }
                i++;
            }
        }
    }
    if (entity == null)
        exception(env, "Parameter %s does not exist", name);
    setEntity(entity);
    return Memory.NULL;
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) AbstractFunctionEntity(php.runtime.reflection.support.AbstractFunctionEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) Invoker(php.runtime.invoke.Invoker) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 8 with Invoker

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

the class LangFunctions method set_exception_handler.

public static Memory set_exception_handler(Environment env, TraceInfo trace, @Runtime.Reference Memory handler) {
    Invoker invoker = expectingCallback(env, trace, 1, handler);
    if (invoker != null) {
        ExceptionHandler eh = env.getExceptionHandler();
        env.setExceptionHandler(new ExceptionHandler(invoker, handler.toImmutable()));
        return eh == null || eh.invoker == null ? Memory.NULL : eh.invokerMemory;
    } else
        return Memory.FALSE;
}
Also used : ExceptionHandler(php.runtime.env.handler.ExceptionHandler) Invoker(php.runtime.invoke.Invoker)

Example 9 with Invoker

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

the class LangFunctions method _call_user_func.

private static Memory _call_user_func(Environment env, TraceInfo trace, Memory function, Memory... args) throws Throwable {
    Invoker invoker = expectingCallback(env, trace, 1, function);
    if (invoker == null) {
        return Memory.FALSE;
    }
    invoker.setTrace(trace);
    return invoker.call(args);
}
Also used : Invoker(php.runtime.invoke.Invoker)

Example 10 with Invoker

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

the class FileObject method find.

@Signature(@Arg(value = "filter", optional = @Optional("NULL")))
public Memory find(final Environment env, Memory... args) {
    if (args[0].isNull()) {
        return ArrayMemory.ofStrings(file.list()).toConstant();
    } else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "Invalid filter value, must be callable");
            return Memory.NULL;
        }
        final TraceInfo trace = env.trace();
        invoker.setTrace(trace);
        String[] result = file.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                FileObject o = new FileObject(env, __class__, dir);
                Memory[] args = new Memory[] { new ObjectMemory(o), new StringMemory(name) };
                return invoker.callNoThrow(args).toBoolean();
            }
        });
        return ArrayMemory.ofStrings(result);
    }
}
Also used : Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo)

Aggregations

Invoker (php.runtime.invoke.Invoker)50 Memory (php.runtime.Memory)21 ArrayMemory (php.runtime.memory.ArrayMemory)13 ForeachIterator (php.runtime.lang.ForeachIterator)12 LongMemory (php.runtime.memory.LongMemory)9 ObjectMemory (php.runtime.memory.ObjectMemory)9 KeyValueMemory (php.runtime.memory.KeyValueMemory)7 StringMemory (php.runtime.memory.StringMemory)7 Environment (php.runtime.env.Environment)6 ReferenceMemory (php.runtime.memory.ReferenceMemory)5 SplClassLoader (php.runtime.env.SplClassLoader)4 File (java.io.File)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 TraceInfo (php.runtime.env.TraceInfo)2 FileObject (php.runtime.ext.core.classes.stream.FileObject)2 ActionEvent (java.awt.event.ActionEvent)1 MessageDigest (java.security.MessageDigest)1 Scanner (java.util.Scanner)1 FileFilter (javax.swing.filechooser.FileFilter)1