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());
}
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);
}
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;
}
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);
}
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;
}
Aggregations