Search in sources :

Example 6 with Environment

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

the class MemoryOperation method get.

@SuppressWarnings("unchecked")
public static MemoryOperation get(final Class<?> type, Type genericTypes, boolean includeParents) {
    MemoryOperation operation = null;
    if (genericTypes instanceof ParameterizedType) {
        operation = genericOperations.get(new ParametrizedClass(type, ((ParameterizedType) genericTypes).getActualTypeArguments()));
    }
    if (operation == null) {
        operation = operations.get(type);
        if (operation == null) {
            if (type.isArray()) {
                MemoryOperation arrayMemoryOperation = new ArrayMemoryOperation(type);
                register(arrayMemoryOperation);
                return arrayMemoryOperation;
            }
            if (Enum.class.isAssignableFrom(type)) {
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[] { Enum.class };
                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        return arg.isNull() ? null : Enum.valueOf((Class<? extends Enum>) type, arg.toString());
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        return arg == null ? Memory.NULL : StringMemory.valueOf(((Enum) arg).name());
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setTypeEnum((Class<? extends Enum>) type);
                    }
                };
            }
            final Class<? extends BaseWrapper> wrapperClass = wrappers.get(type);
            if (wrapperClass != null) {
                Constructor<BaseWrapper> constructor;
                try {
                    constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, type);
                } catch (NoSuchMethodException e) {
                    try {
                        constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, Object.class);
                    } catch (NoSuchMethodException e1) {
                        throw new CriticalException(e);
                    }
                }
                final Constructor<BaseWrapper> finalConstructor = constructor;
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[0];
                    }

                    @Override
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        if (arg.isNull()) {
                            return null;
                        }
                        return arg.toObject(BaseWrapper.class).getWrappedObject();
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        if (arg == null) {
                            return Memory.NULL;
                        }
                        Constructor<BaseWrapper> constructorContext = finalConstructor;
                        Class<? extends BaseWrapper> wrapperClassContext = wrapperClass;
                        if (arg.getClass() != type) {
                            wrapperClassContext = wrappers.get(arg.getClass());
                        }
                        if (wrapperClassContext != null && wrapperClassContext != wrapperClass) {
                            constructorContext = (Constructor<BaseWrapper>) wrapperClassContext.getConstructor(Environment.class, arg.getClass());
                        }
                        try {
                            BaseWrapper instance = constructorContext.newInstance(env, arg);
                            return ObjectMemory.valueOf(instance.__getOriginInstance());
                        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                            throw new CriticalException(e);
                        }
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setTypeNativeClass(type);
                    }
                };
            } else if (IObject.class.isAssignableFrom(type)) {
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[] { IObject.class };
                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        if (arg.isNull()) {
                            return null;
                        }
                        return arg.toObject((Class<? extends IObject>) type);
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        if (arg == null) {
                            return Memory.NULL;
                        }
                        return ObjectMemory.valueOf((IObject) arg);
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setType(ReflectionUtils.getClassName(type));
                    }
                };
            } else {
                Class<?> superType = type.getSuperclass();
                if (Object.class != superType && (includeParents || type.isAnonymousClass())) {
                    return get(superType, type.getGenericSuperclass(), includeParents);
                }
            }
        }
    }
    if (operation == null) {
        return null;
    }
    if (genericTypes instanceof ParameterizedType) {
        return operation.instance(((ParameterizedType) genericTypes).getActualTypeArguments());
    }
    return operation;
}
Also used : Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) ParameterizedType(java.lang.reflect.ParameterizedType) BaseWrapper(php.runtime.lang.BaseWrapper) IObject(php.runtime.lang.IObject) Constructor(java.lang.reflect.Constructor) TraceInfo(php.runtime.env.TraceInfo) IterableMemoryOperation(php.runtime.memory.support.operation.iterator.IterableMemoryOperation) PropertiesMemoryOperation(php.runtime.memory.support.operation.map.PropertiesMemoryOperation) HashMapMemoryOperation(php.runtime.memory.support.operation.map.HashMapMemoryOperation) SetMemoryOperation(php.runtime.memory.support.operation.collection.SetMemoryOperation) MapMemoryOperation(php.runtime.memory.support.operation.map.MapMemoryOperation) HashSetMemoryOperation(php.runtime.memory.support.operation.collection.HashSetMemoryOperation) ListMemoryOperation(php.runtime.memory.support.operation.collection.ListMemoryOperation) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterEntity(php.runtime.reflection.ParameterEntity) Environment(php.runtime.env.Environment) IObject(php.runtime.lang.IObject)

Example 7 with Environment

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

the class ObjectMemory method toString.

@Override
public String toString() {
    ClassEntity entity = value.getReflection();
    if (entity.methodMagicToString != null) {
        Environment env = value.getEnvironment();
        if (env == null)
            return "Object";
        // We can't get real trace info from toString method :(
        env.pushCall(entity.methodMagicToString.getTrace(), value, null, entity.methodMagicToString.getName(), entity.getName(), null);
        try {
            Memory result = entity.methodMagicToString.invokeDynamic(value, env, (Memory[]) null);
            if (!result.isString()) {
                env.error(ErrorType.E_RECOVERABLE_ERROR, "Method %s must return a string value", entity.methodMagicToString.getSignatureString(false));
                return "";
            }
            return result.toString();
        } catch (RuntimeException e) {
            throw e;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            env.popCall();
        }
    } else
        return "Object";
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Memory(php.runtime.Memory) Environment(php.runtime.env.Environment)

Example 8 with Environment

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

the class ObjectMemory method unsetOfIndex.

@Override
public void unsetOfIndex(TraceInfo trace, Memory index) {
    if (value instanceof ArrayAccess) {
        Environment env = value.getEnvironment();
        if (env != null && trace != null) {
            Memory[] args = new Memory[] { index };
            env.pushCall(value, "offsetUnset", args);
            try {
                ((ArrayAccess) value).offsetUnset(env, args);
            } finally {
                env.popCall();
            }
        } else
            invalidUseAsArray(trace);
    } else
        invalidUseAsArray(trace);
}
Also used : ArrayAccess(php.runtime.lang.spl.ArrayAccess) Memory(php.runtime.Memory) Environment(php.runtime.env.Environment)

Example 9 with Environment

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

the class DebugExtension method onRegister.

@Override
public void onRegister(final CompileScope scope) {
    if (scope.isDebugMode()) {
        String debugIdeKey = "JPHP_DEBUGGER";
        if (scope.configuration.containsKey("debug.ideKey")) {
            debugIdeKey = scope.configuration.get("debug.ideKey").toString();
        }
        String debugRootPath = "./src/";
        if (scope.configuration.containsKey("debug.rootPath")) {
            debugRootPath = scope.configuration.get("debug.rootPath").toString();
        }
        int debugPort = 9000;
        if (scope.configuration.containsKey("debug.port")) {
            debugPort = scope.configuration.get("debug.port").toInteger();
        }
        String debugHost = "127.0.0.1";
        if (scope.configuration.containsKey("debug.host")) {
            debugHost = scope.configuration.get("debug.host").toString();
        }
        final String finalDebugIdeKey = debugIdeKey;
        final String finalDebugRootPath = debugRootPath;
        final int finalDebugPort = debugPort;
        final DebugTickHandler tickHandler = new DebugTickHandler();
        scope.setTickHandler(tickHandler);
        final String finalDebugHost = debugHost;
        Thread debuggerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    final Debugger debugger = new Debugger(finalDebugPort, finalDebugHost);
                    scope.registerProgramShutdownHandler(new ProgramShutdownHandler() {

                        @Override
                        public void onShutdown(CompileScope scope, Environment env) {
                            debugger.shutdown();
                        }
                    });
                    if (debugger.isWorking()) {
                        debugger.setIdeKey(finalDebugIdeKey);
                        debugger.setRootPath(finalDebugRootPath);
                        debugger.run();
                    }
                    tickHandler.setDebugger(debugger);
                } catch (IOException e) {
                    throw new DebuggerException(e);
                }
            }
        });
        debuggerThread.setName("jphpDebuggerThread");
        debuggerThread.start();
    }
}
Also used : Debugger(org.develnext.jphp.debug.impl.Debugger) ProgramShutdownHandler(php.runtime.env.handler.ProgramShutdownHandler) CompileScope(php.runtime.env.CompileScope) DebuggerException(org.develnext.jphp.debug.impl.DebuggerException) Environment(php.runtime.env.Environment) IOException(java.io.IOException)

Example 10 with Environment

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

the class WrapThreadPool method submit.

@Signature({ @Arg(value = "runnable", type = HintType.CALLABLE), @Arg(value = "env", typeClass = "php\\lang\\Environment", optional = @Optional("NULL")) })
public Memory submit(Environment env, Memory... args) {
    final Environment _env = args[1].isNull() ? env : args[1].toObject(WrapEnvironment.class).getWrapEnvironment();
    final Invoker invoker = Invoker.valueOf(_env, null, args[0]);
    Future<Memory> future = service.submit(new Callable<Memory>() {

        @Override
        public Memory call() throws Exception {
            return invoker.callNoThrow();
        }
    });
    return new ObjectMemory(new WrapFuture(env, future));
}
Also used : Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) 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