Search in sources :

Example 6 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method writeSysCall.

@SuppressWarnings("unchecked")
void writeSysCall(Class clazz, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) {
    if (INVOKE_TYPE != INVOKESPECIAL && clazz != null) {
        if (compiler.getScope().isDebugMode()) {
            if (!methodExists(clazz, method, paramClasses)) {
                throw new NoSuchMethodException(clazz, method, paramClasses);
            }
        }
    }
    Type[] args = new Type[paramClasses.length];
    if (INVOKE_TYPE == INVOKEVIRTUAL || INVOKE_TYPE == INVOKEINTERFACE)
        // this
        stackPop();
    for (int i = 0; i < args.length; i++) {
        args[i] = Type.getType(paramClasses[i]);
        stackPop();
    }
    String owner = clazz == null ? this.method.clazz.node.name : Type.getInternalName(clazz);
    if (clazz == null && this.method.clazz.entity.isTrait())
        throw new CriticalException("[Compiler Error] Cannot use current classname in Trait");
    code.add(new MethodInsnNode(INVOKE_TYPE, owner, method, Type.getMethodDescriptor(Type.getType(returnClazz), args), clazz != null && clazz.isInterface()));
    if (returnClazz != void.class) {
        stackPush(null, StackItem.Type.valueOf(returnClazz));
    }
}
Also used : Type(org.objectweb.asm.Type) CriticalException(php.runtime.exceptions.CriticalException)

Example 7 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method getCompiler.

@SuppressWarnings("unchecked")
public <T extends Token> BaseStatementCompiler<T> getCompiler(Class<T> clazz) {
    if (compilers == null) {
        compilers = new HashMap<Class<? extends Token>, BaseStatementCompiler>();
    }
    BaseStatementCompiler<T> r = compilers.get(clazz);
    if (r != null)
        return r;
    Class<? extends BaseStatementCompiler<T>> rule = (Class<? extends BaseStatementCompiler<T>>) compilerRules.get(clazz);
    if (rule == null)
        return null;
    try {
        r = rule.getConstructor(ExpressionStmtCompiler.class).newInstance(this);
        compilers.put(clazz, r);
        return r;
    } catch (Exception e) {
        throw new CriticalException(e);
    }
}
Also used : CompileClass(php.runtime.ext.support.compile.CompileClass) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ClassExprToken(org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken) OpenEchoTagToken(org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CriticalException(php.runtime.exceptions.CriticalException)

Example 8 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class MethodNodeImpl method duplicate.

public static MethodNodeImpl duplicate(MethodNode node) {
    MethodNodeImpl self = new MethodNodeImpl();
    self.name = node.name;
    self.desc = node.desc;
    self.access = node.access;
    self.annotationDefault = node.annotationDefault;
    self.attrs = node.attrs;
    self.exceptions = node.exceptions;
    self.tryCatchBlocks = node.tryCatchBlocks;
    self.localVariables = node.localVariables;
    self.instructions = node.instructions;
    self.invisibleAnnotations = node.invisibleAnnotations;
    self.invisibleParameterAnnotations = node.invisibleParameterAnnotations;
    self.visibleAnnotations = node.visibleAnnotations;
    self.visibleParameterAnnotations = node.visibleParameterAnnotations;
    self.maxLocals = node.maxLocals;
    self.maxStack = node.maxStack;
    self.localVariables = node.localVariables;
    // it's needed for copied trait method, else it will generate an error in duplicated usage
    try {
        Field field = node.getClass().getDeclaredField("visited");
        field.setAccessible(true);
        field.setBoolean(self, true);
    } catch (NoSuchFieldException e) {
        throw new CriticalException(e);
    } catch (IllegalAccessException e) {
        throw new CriticalException(e);
    }
    return self;
}
Also used : Field(java.lang.reflect.Field) CriticalException(php.runtime.exceptions.CriticalException)

Example 9 with CriticalException

use of php.runtime.exceptions.CriticalException 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 10 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ObjectMemory method manualUnset.

@Override
public void manualUnset(Environment env) {
    ClassEntity entity = value.getReflection();
    if (entity.methodDestruct != null) {
        if (!value.isFinalized()) {
            value.doFinalize();
            env.pushCall(value, entity.methodDestruct.getName());
            try {
                if (value instanceof IManualDestructable) {
                    ((IManualDestructable) value).onManualDestruct(env);
                }
                entity.methodDestruct.invokeDynamic(value, env);
            } catch (InvocationTargetException e) {
                env.__throwException(e);
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new CriticalException(throwable);
            } finally {
                env.popCall();
            }
        }
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) IManualDestructable(php.runtime.lang.support.IManualDestructable) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CriticalException (php.runtime.exceptions.CriticalException)19 Memory (php.runtime.Memory)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IObject (php.runtime.lang.IObject)5 ObjectMemory (php.runtime.memory.ObjectMemory)4 ClassEntity (php.runtime.reflection.ClassEntity)4 IOException (java.io.IOException)3 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)3 BaseWrapper (php.runtime.lang.BaseWrapper)3 StringMemory (php.runtime.memory.StringMemory)3 Field (java.lang.reflect.Field)2 OpenEchoTagToken (org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken)2 Token (org.develnext.jphp.core.tokenizer.token.Token)2 ClassExprToken (org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken)2 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)2 Reflection (php.runtime.annotation.Reflection)2 Environment (php.runtime.env.Environment)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 ReferenceMemory (php.runtime.memory.ReferenceMemory)2 Constructor (java.lang.reflect.Constructor)1