Search in sources :

Example 11 with Environment

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

the class WrapThreadPool method execute.

@Signature({ @Arg(value = "runnable", type = HintType.CALLABLE), @Arg(value = "env", nativeType = WrapEnvironment.class, optional = @Optional("NULL")) })
public Memory execute(final Environment env, Memory... args) {
    Environment _env = env;
    if (!args[1].isNull()) {
        _env = args[1].toObject(WrapEnvironment.class).getWrapEnvironment();
    }
    final Invoker invoker = Invoker.valueOf(_env, null, args[0]);
    invoker.setTrace(env.trace());
    final Environment final_env = _env;
    service.execute(new Runnable() {

        @Override
        public void run() {
            Environment.addThreadSupport(final_env);
            invoker.callNoThrow();
        }
    });
    return Memory.NULL;
}
Also used : Invoker(php.runtime.invoke.Invoker) Environment(php.runtime.env.Environment)

Example 12 with Environment

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

the class ExpressionStmtCompiler method writePushCompileFunction.

Memory writePushCompileFunction(CallExprToken function, CompileFunction compileFunction, boolean returnValue, boolean writeOpcode, PushCallStatistic statistic) {
    CompileFunction.Method method = compileFunction.find(function.getParameters().size());
    if (method == null) {
        if (!writeOpcode)
            return Memory.NULL;
        writeWarning(function, Messages.ERR_EXPECT_LEAST_PARAMS.fetch(compileFunction.name, compileFunction.getMinArgs(), function.getParameters().size()));
        if (returnValue)
            writePushNull();
        return null;
    } else if (!method.isVarArg() && method.argsCount < function.getParameters().size()) {
        if (!writeOpcode)
            return Memory.NULL;
        writeWarning(function, Messages.ERR_EXPECT_EXACTLY_PARAMS.fetch(compileFunction.name, method.argsCount, function.getParameters().size()));
        if (returnValue)
            writePushNull();
        return null;
    }
    if (statistic != null)
        statistic.returnType = StackItem.Type.valueOf(method.resultType);
    Class[] types = method.parameterTypes;
    ListIterator<ExprStmtToken> iterator = function.getParameters().listIterator();
    Object[] arguments = new Object[types.length];
    int j = 0;
    boolean immutable = method.isImmutable;
    boolean init = false;
    for (int i = 0; i < method.parameterTypes.length; i++) {
        Class<?> argType = method.parameterTypes[i];
        boolean isRef = method.references[i];
        boolean isMutable = method.isPresentAnnotationOfParam(i, Runtime.MutableValue.class);
        if (method.isPresentAnnotationOfParam(i, Runtime.GetLocals.class)) {
            if (!writeOpcode)
                return null;
            immutable = false;
            writePushLocal();
        } else if (argType == Environment.class) {
            if (immutable) {
                arguments[i] = compiler.getEnvironment();
            } else {
                if (!writeOpcode)
                    return null;
                writePushEnv();
            }
        } else if (argType == TraceInfo.class) {
            if (immutable) {
                arguments[i] = function.toTraceInfo(compiler.getContext());
            } else {
                if (!writeOpcode)
                    return null;
                writePushTraceInfo(function);
            }
        } else {
            if (argType == Memory[].class) {
                Memory[] args = new Memory[function.getParameters().size() - j];
                boolean arrCreated = false;
                for (int t = 0; t < function.getParameters().size() - j; t++) {
                    ExprStmtToken next = iterator.next();
                    if (immutable)
                        args[t] = writeExpression(next, true, true, false);
                    if (args[t] == null) {
                        if (!writeOpcode)
                            return null;
                        if (!arrCreated) {
                            if (immutable) {
                                for (int n = 0; n < i; /*j*/
                                n++) {
                                    if (arguments[n] instanceof TraceInfo) {
                                        writePushTraceInfo(function);
                                    } else if (arguments[n] instanceof Environment) {
                                        writePushEnv();
                                    } else {
                                        writePushMemory((Memory) arguments[n]);
                                        writePop(types[n], true, true);
                                        arguments[t] = null;
                                    }
                                }
                            }
                            // create new array
                            writePushSmallInt(args.length);
                            code.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
                            stackPop();
                            stackPush(Memory.Type.REFERENCE);
                            arrCreated = true;
                        }
                        writePushDup();
                        writePushSmallInt(t);
                        writeExpression(next, true, false, writeOpcode);
                        //if (!isRef)  BUGFIX - array is memory[] and so we need to convert value to memory
                        writePopBoxing(!isRef);
                        code.add(new InsnNode(AASTORE));
                        stackPop();
                        stackPop();
                        stackPop();
                        immutable = false;
                    }
                }
                if (!immutable && !arrCreated) {
                    code.add(new InsnNode(ACONST_NULL));
                    stackPush(Memory.Type.REFERENCE);
                }
                arguments[i] = MemoryUtils.valueOf(args);
            } else {
                ExprStmtToken next = iterator.next();
                if (!immutable && !init) {
                    init = true;
                    for (int k = 0; k < i; /*j*/
                    k++) {
                        if (arguments[k] != null) {
                            if (arguments[k] instanceof TraceInfo) {
                                writePushTraceInfo(function);
                            } else if (arguments[k] instanceof Environment) {
                                writePushEnv();
                            } else {
                                writePushMemory((Memory) arguments[k]);
                                writePop(types[k], true, isRef);
                                arguments[k] = null;
                            }
                        }
                    }
                }
                if (immutable)
                    arguments[i] = writeExpression(next, true, true, false);
                if (arguments[i] == null) {
                    if (!writeOpcode)
                        return null;
                    if (!init) {
                        for (int n = 0; n < i; /*j*/
                        n++) {
                            if (arguments[n] instanceof TraceInfo) {
                                writePushTraceInfo(function);
                            } else if (arguments[n] instanceof Environment) {
                                writePushEnv();
                            } else {
                                writePushMemory((Memory) arguments[n]);
                                writePop(types[n], true, false);
                            }
                            arguments[n] = null;
                        }
                        init = true;
                    }
                    if (isRef)
                        writeExpression(next, true, false, writeOpcode);
                    else {
                        Memory tmp = writeExpression(next, true, true, true);
                        if (tmp != null)
                            writePushMemory(tmp);
                    }
                    writePop(argType, true, isMutable && !isRef);
                    if (!isMutable && !isRef && stackPeek().type.isReference()) {
                        writeSysDynamicCall(Memory.class, "toValue", Memory.class);
                    }
                    immutable = false;
                }
                j++;
            }
        }
    }
    if (immutable) {
        if (!returnValue)
            return null;
        Object[] typedArguments = new Object[arguments.length];
        for (int i = 0; i < arguments.length; i++) {
            if (method.parameterTypes[i] != Memory.class && arguments[i] instanceof Memory)
                typedArguments[i] = method.converters[i].run((Memory) arguments[i]);
            else
                //MemoryUtils.toValue((Memory)arguments[i], types[i]);
                typedArguments[i] = arguments[i];
        }
        try {
            Object value = method.method.invoke(null, typedArguments);
            return MemoryUtils.valueOf(value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        }
    }
    if (!writeOpcode)
        return null;
    this.method.entity.setImmutable(false);
    writeLineNumber(function);
    writePushStaticCall(method.method);
    if (returnValue) {
        if (method.resultType == void.class)
            writePushNull();
        setStackPeekAsImmutable();
    }
    return null;
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) CompileFunction(php.runtime.ext.support.compile.CompileFunction) TraceInfo(php.runtime.env.TraceInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) Runtime(php.runtime.annotation.Runtime) Environment(php.runtime.env.Environment) CompileClass(php.runtime.ext.support.compile.CompileClass) IObject(php.runtime.lang.IObject)

Example 13 with Environment

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

the class EvalCommand method getValue.

public static Memory getValue(Debugger context, String value) {
    DebugTick tick = context.getRegisteredTick();
    Environment environment = new Environment(new CompileScope(tick.getEnvironment().getScope()));
    try {
        return EvalFunctions.eval(environment, tick.getTrace(), tick.getLocals(), "return " + value + ";");
    } catch (Throwable throwable) {
        return null;
    }
}
Also used : DebugTick(org.develnext.jphp.debug.impl.DebugTick) CompileScope(php.runtime.env.CompileScope) Environment(php.runtime.env.Environment)

Example 14 with Environment

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

the class JsonProcessor method onSerialize.

@Signature({ @Arg("type"), @Arg(value = "callback", type = HintType.CALLABLE, optional = @Optional("null")) })
public Memory onSerialize(Environment env, Memory... args) {
    Memory.Type type = Memory.Type.of(args[0].toString());
    if (type == null)
        throw new IllegalArgumentException("Invalid type - " + args[0]);
    MemorySerializer.Handler handler = null;
    if (!args[1].isNull()) {
        final Invoker invoker = Invoker.valueOf(env, env.trace(), args[1]);
        handler = new MemorySerializer.Handler() {

            @Override
            public Memory call(Environment env, Memory value) {
                return invoker.callNoThrow(value);
            }
        };
    }
    memorySerializer.setTypeHandler(type, handler);
    return null;
}
Also used : MemorySerializer(org.develnext.jphp.json.gson.MemorySerializer) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) Environment(php.runtime.env.Environment)

Example 15 with Environment

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

the class JsonProcessor method onClassSerialize.

@Signature({ @Arg("className"), @Arg(value = "callback", type = HintType.CALLABLE, optional = @Optional("null")) })
public Memory onClassSerialize(Environment env, Memory... args) {
    ClassEntity entity = env.fetchClass(args[0].toString(), true);
    if (entity == null)
        throw new IllegalArgumentException("Class not found - " + args[0]);
    MemorySerializer.Handler handler = null;
    if (!args[1].isNull()) {
        final Invoker invoker = Invoker.valueOf(env, env.trace(), args[1]);
        handler = new MemorySerializer.Handler() {

            @Override
            public Memory call(Environment env, Memory value) {
                return invoker.callNoThrow(value);
            }
        };
    }
    memorySerializer.setClassHandler(entity.getName(), handler);
    return Memory.NULL;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) MemorySerializer(org.develnext.jphp.json.gson.MemorySerializer) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) 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