Search in sources :

Example 1 with ObjectMemory

use of php.runtime.memory.ObjectMemory in project jphp by jphp-compiler.

the class ClassesTest method testProperties.

@Test
public void testProperties() throws Throwable {
    Memory memory;
    memory = runDynamic("class A { var $x = 11, $y = 30; } return new A();", false);
    Assert.assertTrue(memory.isObject());
    IObject object = ((ObjectMemory) memory).value;
    Assert.assertEquals(11, object.getReflection().getProperty(environment, null, object, "x", null, 0).toLong());
    Assert.assertEquals(30, object.getReflection().getProperty(environment, null, object, "y", null, 0).toLong());
    memory = runDynamic("class A { public $arr = array(1, 2, 3); } return new A()->arr;", false);
    Assert.assertTrue(memory.isArray());
}
Also used : IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Test(org.junit.Test)

Example 2 with ObjectMemory

use of php.runtime.memory.ObjectMemory in project jphp by jphp-compiler.

the class ReflectionMethod method getDeclaringClass.

@Signature
public Memory getDeclaringClass(Environment env, Memory... args) {
    ClassEntity entity = env.fetchClass("ReflectionClass");
    ReflectionClass r = new ReflectionClass(env, entity);
    r.setEntity(methodEntity.getClazz());
    return new ObjectMemory(r);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 3 with ObjectMemory

use of php.runtime.memory.ObjectMemory in project jphp by jphp-compiler.

the class ObjectInvokeHelper method invokeParentMethod.

public static Memory invokeParentMethod(Memory object, String methodName, String methodLowerName, Environment env, TraceInfo trace, Memory[] args) throws Throwable {
    Memory[] passed = null;
    boolean doublePop = false;
    if (object.isNull()) {
        ClassEntity parent = env.__getParentClass(trace);
        return InvokeHelper.callStatic(env, trace, parent.getLowerName(), methodLowerName, parent.getName(), methodName, args, null, 0);
    }
    IObject iObject = ((ObjectMemory) object).value;
    ClassEntity childClazz = iObject.getReflection();
    ClassEntity clazz = env.getLastClassOnStack().getParent();
    MethodEntity method;
    if (clazz == null) {
        env.error(trace, "Cannot access parent:: when current class scope has no parent");
        return Memory.NULL;
    }
    if (methodName == null) {
        method = childClazz.methodMagicInvoke != null ? childClazz.methodMagicInvoke : clazz.methodMagicInvoke;
    } else {
        method = clazz.findMethod(methodLowerName);
        if (method == null && ((method = childClazz.methodMagicCall != null ? childClazz.methodMagicCall : clazz.methodMagicCall) != null)) {
            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(), className, methodName, trace);
    }
    Memory result = method.getImmutableResult();
    if (result != null)
        return result;
    try {
        if (trace != null) {
            env.pushCall(trace, iObject, args, methodName, method.getClazz().getName(), className);
            if (doublePop)
                env.pushCall(trace, iObject, passed, method.getName(), method.getClazz().getName(), className);
        }
        result = method.invokeDynamic(iObject, env, passed);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new CriticalException("Unable to call parent:: method " + className + "::" + methodName + "(), error = " + e.getMessage());
    } finally {
        if (trace != null) {
            env.popCall();
            if (doublePop)
                env.popCall();
        }
    }
    return result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) IObject(php.runtime.lang.IObject) 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 4 with ObjectMemory

use of php.runtime.memory.ObjectMemory in project jphp by jphp-compiler.

the class ObjectInvokeHelper method getProperty.

public static Memory getProperty(Memory object, String property, Environment env, TraceInfo trace, PropertyCallCache callCache, int cacheIndex) throws Throwable {
    object = object.toValue();
    if (!object.isObject()) {
        env.error(trace, Messages.ERR_CANNOT_GET_PROPERTY_OF_NON_OBJECT.fetch(property));
        return Memory.NULL;
    }
    IObject iObject = ((ObjectMemory) object).value;
    return iObject.getReflection().getProperty(env, trace, iObject, property, callCache, cacheIndex);
}
Also used : IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 5 with ObjectMemory

use of php.runtime.memory.ObjectMemory in project jphp by jphp-compiler.

the class JavaMethod method invoke.

@Signature({ @Arg(value = "object", typeClass = "php\\lang\\JavaObject", optional = @Optional("NULL")) })
public Memory invoke(Environment env, Memory... args) {
    int len = args.length - 1;
    if (len < paramTypes.length || len > paramTypes.length)
        exception(env, new IllegalArgumentException("Invalid argument count"));
    Object[] passed = new Object[len];
    int i = 0;
    for (MemoryUtils.Converter converter : converters) {
        Memory arg = args[i + 1];
        if (arg.instanceOf("php\\lang\\JavaObject")) {
            passed[i] = ((JavaObject) arg.toValue(ObjectMemory.class).value).getObject();
        } else {
            if (converter != null) {
                passed[i] = converter.run(args[i + 1]);
            } else {
                passed[i] = null;
            }
        }
        i++;
    }
    Object obj = args[0].isNull() ? null : ((JavaObject) args[0].toValue(ObjectMemory.class).value).getObject();
    try {
        Object result = method.invoke(obj, passed);
        if (result == null)
            return Memory.NULL;
        if (resultConverter != null)
            return MemoryUtils.valueOf(result);
        else {
            if (method.getReturnType() == void.class)
                return Memory.NULL;
            return new ObjectMemory(JavaObject.of(env, result));
        }
    } catch (IllegalAccessException e) {
        exception(env, e);
    } catch (InvocationTargetException e) {
        exception(env, e.getTargetException());
    }
    return Memory.NULL;
}
Also used : ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) MemoryUtils(php.runtime.memory.support.MemoryUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ObjectMemory (php.runtime.memory.ObjectMemory)55 ArrayMemory (php.runtime.memory.ArrayMemory)24 Memory (php.runtime.Memory)19 ClassEntity (php.runtime.reflection.ClassEntity)17 StringMemory (php.runtime.memory.StringMemory)14 IObject (php.runtime.lang.IObject)11 Invoker (php.runtime.invoke.Invoker)6 LongMemory (php.runtime.memory.LongMemory)6 Environment (php.runtime.env.Environment)5 MethodEntity (php.runtime.reflection.MethodEntity)5 Calendar (java.util.Calendar)4 Closure (php.runtime.lang.Closure)4 Locale (java.util.Locale)3 Test (org.junit.Test)3 WrapLocale (php.runtime.ext.core.classes.util.WrapLocale)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2