Search in sources :

Example 41 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class ReflectionClass method getInterfaces.

@Signature
public Memory getInterfaces(Environment env, Memory... args) {
    ArrayMemory result = new ArrayMemory();
    ClassEntity classEntity = env.fetchClass("ReflectionClass");
    for (ClassEntity e : entity.getInterfaces().values()) {
        ReflectionClass cls = new ReflectionClass(env, classEntity);
        cls.setEntity(e);
        result.add(new ObjectMemory(cls));
    }
    return result.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ClassEntity(php.runtime.reflection.ClassEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 42 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class ObjectInvokeHelper method invokeMethod.

public static Memory invokeMethod(Memory object, String methodName, String methodLowerName, Environment env, TraceInfo trace, Memory[] args) throws Throwable {
    object = object.toValue();
    Memory[] passed = null;
    boolean doublePop = false;
    if (object.type != Memory.Type.OBJECT) {
        env.error(trace, ErrorType.E_RECOVERABLE_ERROR, Messages.ERR_CANNOT_CALL_OF_NON_OBJECT.fetch(methodName));
        return Memory.NULL;
    }
    IObject iObject = ((ObjectMemory) object).value;
    ClassEntity clazz = iObject.getReflection();
    MethodEntity method;
    if (methodName == null) {
        method = clazz.methodMagicInvoke;
    } else {
        method = clazz.findMethod(methodLowerName);
        if (method != null && method.isContextDepends()) {
            ClassEntity context = env.getLastClassOnStack();
            if (context != null) {
                MethodEntity contextMethod = context.findMethod(methodLowerName);
                if (contextMethod != null) {
                    method = contextMethod;
                }
            }
        }
        if (method == null && ((method = clazz.methodMagicCall) != null)) {
            clazz.methodMagicCall.setModifier(Modifier.PUBLIC);
            passed = new Memory[] { new StringMemory(methodName), ArrayMemory.of(args) };
            doublePop = true;
        }
    }
    String className = clazz.getName();
    if (method == null) {
        if (methodName == null)
            methodName = "__invoke";
        env.error(trace, ErrorType.E_ERROR, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(className + "::" + methodName));
        return Memory.NULL;
    }
    InvokeHelper.checkAccess(env, trace, method);
    if (passed == null) {
        passed = InvokeHelper.makeArguments(env, args, method.getParameters(args == null ? 0 : args.length), className, methodName, trace);
    }
    Memory result = method.getImmutableResult();
    if (result != null) {
        return result;
    }
    try {
        if (trace != null) {
            String staticClass = className;
            if (iObject instanceof Closure) {
                staticClass = ((Closure) iObject).getScope();
            }
            String stackClass = clazz.isHiddenInCallStack() ? staticClass : method.getClazz().getName();
            env.pushCall(trace, iObject, args, methodName, stackClass, staticClass);
            if (doublePop) {
                env.pushCall(trace, iObject, passed, method.getName(), stackClass, staticClass);
            }
        }
        return method.invokeDynamic(iObject, env, passed);
    } catch (NoClassDefFoundError e) {
        throw new CriticalException("Unable to call method " + className + "::" + methodName + "(), " + e.getMessage());
    } finally {
        if (trace != null) {
            env.popCall();
            if (doublePop)
                env.popCall();
        }
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) IObject(php.runtime.lang.IObject) Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) MethodEntity(php.runtime.reflection.MethodEntity) StringMemory(php.runtime.memory.StringMemory) CriticalException(php.runtime.exceptions.CriticalException)

Example 43 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class ObjectInvokeHelper method getConstant.

public static Memory getConstant(String className, String lowerClassName, String constant, Environment env, TraceInfo trace, ConstantCallCache callCache, int cacheIndex) {
    ConstantEntity constantEntity = null;
    if (callCache != null) {
        constantEntity = callCache.get(env, cacheIndex);
    }
    if (constantEntity == null) {
        ClassEntity entity = env.fetchClass(className, lowerClassName, true);
        if (entity == null) {
            env.error(trace, Messages.ERR_CLASS_NOT_FOUND.fetch(className));
            return Memory.NULL;
        }
        constantEntity = entity.findConstant(constant);
        if (constantEntity == null) {
            env.error(trace, Messages.ERR_UNDEFINED_CLASS_CONSTANT.fetch(constant));
            return Memory.NULL;
        }
        if (callCache != null) {
            callCache.put(env, cacheIndex, constantEntity);
        }
    }
    Memory value = constantEntity.getValue(env);
    if (value == null) {
        return Memory.NULL;
    }
    return value;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) ConstantEntity(php.runtime.reflection.ConstantEntity)

Example 44 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class SPLFunctions method class_parents.

public static Memory class_parents(Environment env, TraceInfo trace, Memory object, boolean autoLoad) {
    ClassEntity entity;
    if (object.isObject()) {
        entity = object.toValue(ObjectMemory.class).getReflection();
    } else {
        entity = env.fetchClass(object.toString(), autoLoad);
    }
    if (entity == null) {
        env.warning(trace, "class_parents(): Class %s does not exist and could not be loaded", object.toString());
        return Memory.FALSE;
    }
    ArrayMemory result = new ArrayMemory();
    do {
        entity = entity.getParent();
        if (entity == null)
            break;
        result.refOfIndex(entity.getName()).assign(entity.getName());
    } while (true);
    return result.toConstant();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory)

Example 45 with ClassEntity

use of php.runtime.reflection.ClassEntity in project jphp by jphp-compiler.

the class ModuleDumper method load.

@Override
public ModuleEntity load(InputStream input) throws IOException {
    DumpInputStream data = new DumpInputStream(input);
    int STAMP = data.readInt();
    if (STAMP != DUMP_STAMP)
        throw new DumpException("Invalid file format");
    int VERSION = data.readInt();
    if (VERSION != DUMP_VERSION)
        throw new DumpException("Invalid dump version - " + VERSION + ", only " + DUMP_VERSION);
    // legacy
    data.readLangMode();
    ModuleEntity entity = new ModuleEntity(context);
    entity.setName(data.readName());
    entity.setInternalName(data.readName());
    entity.setTrace(data.readTrace(context));
    // constants
    int count = data.readInt();
    for (int i = 0; i < count; i++) {
        ConstantEntity el = constantDumper.load(input);
        el.setModule(entity);
        entity.addConstant(el);
    }
    // closures
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        ClosureEntity el = closureDumper.load(input);
        el.setModule(entity);
        entity.addClosure(el);
    }
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        GeneratorEntity el = generatorDumper.load(input);
        el.setModule(entity);
        entity.addGenerator(el);
    }
    // functions
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        FunctionEntity el = functionDumper.load(input);
        el.setModule(entity);
        entity.addFunction(el);
    }
    // classes
    count = data.readInt();
    ClassDumper classDumper = new ClassDumper(context, entity, env, debugInformation);
    for (int i = 0; i < count; i++) {
        ClassEntity el = classDumper.load(input);
        el.setModule(entity);
        entity.addClass(el);
    }
    // byte code
    entity.setData(data.readRawData(Integer.MAX_VALUE));
    data.readRawData(1024 * 1024 * 5);
    return entity;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) ModuleEntity(php.runtime.reflection.ModuleEntity) DumpException(php.runtime.loader.dump.io.DumpException) ConstantEntity(php.runtime.reflection.ConstantEntity)

Aggregations

ClassEntity (php.runtime.reflection.ClassEntity)54 ObjectMemory (php.runtime.memory.ObjectMemory)21 Memory (php.runtime.Memory)14 ArrayMemory (php.runtime.memory.ArrayMemory)14 PropertyEntity (php.runtime.reflection.PropertyEntity)10 MethodEntity (php.runtime.reflection.MethodEntity)9 StringMemory (php.runtime.memory.StringMemory)8 ConstantEntity (php.runtime.reflection.ConstantEntity)5 FunctionEntity (php.runtime.reflection.FunctionEntity)5 CriticalException (php.runtime.exceptions.CriticalException)4 Closure (php.runtime.lang.Closure)4 IObject (php.runtime.lang.IObject)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2