use of php.runtime.memory.support.MemoryOperation in project jphp by jphp-compiler.
the class CompileMethodEntity method invokeDynamic.
public Memory invokeDynamic(Object _this, Environment env, Memory... arguments) throws Throwable {
try {
if (isAbstract) {
env.error(ErrorType.E_ERROR, "Cannot call abstract method %s", getSignatureString(false));
return Memory.NULL;
}
if (_this == null && !isStatic) {
_this = clazz.newMock(env);
if (_this == null)
env.error(ErrorType.E_ERROR, Messages.ERR_STATIC_METHOD_CALLED_DYNAMICALLY.fetch(getClazz().getName() + "::" + getName()));
}
CompileMethod.Method method = function.find(arguments == null ? 0 : arguments.length);
if (method == null) {
env.warning(env.trace(), Messages.ERR_EXPECT_LEAST_PARAMS.fetch(name, function.getMinArgs(), arguments == null ? 0 : arguments.length));
return Memory.NULL;
} else {
if (arguments != null && arguments.length > method.argsCount && !method.isVarArg()) {
env.error(env.trace(), ErrorType.E_ERROR, Messages.ERR_EXPECT_EXACTLY_PARAMS, name, method.argsCount, arguments.length);
return Memory.NULL;
}
}
Class<?>[] types = method.parameterTypes;
Object[] passed = new Object[types.length];
int i = 0;
int j = 0;
for (Class<?> clazz : types) {
boolean isRef = method.references[i];
boolean mutableValue = method.mutableValues[i];
MemoryOperation<?> operation = method.argumentOperations[i];
if (clazz == Memory.class) {
passed[i] = isRef ? arguments[j] : (mutableValue ? arguments[j].toValue() : arguments[j].toImmutable());
j++;
} else if (operation != null) {
if (operation instanceof InjectMemoryOperation) {
passed[i] = operation.convert(env, trace, null);
} else {
passed[i] = operation.convert(env, trace, arguments[j]);
j++;
}
} else if (i == types.length - 1 && types[i] == Memory[].class) {
Memory[] arg = new Memory[arguments.length - i + 1];
if (!isRef) {
for (int k = 0; k < arg.length; k++) arg[i] = arguments[i].toImmutable();
} else {
System.arraycopy(arguments, j, arg, 0, arg.length);
}
passed[i] = arg;
break;
} else {
env.error(trace, ErrorType.E_CORE_ERROR, name + "(): Cannot call this method dynamically");
passed[i] = Memory.NULL;
}
i++;
}
try {
return method.returnOperation.unconvertNoThow(env, trace, method.method.invoke(_this, passed));
} finally {
i = 0;
if (this != this.getClazz().methodConstruct) {
for (Object o : passed) {
MemoryOperation operation = method.argumentOperations[i];
if (operation != null) {
operation.releaseConverted(env, trace, o);
}
i++;
}
}
}
} catch (InvocationTargetException e) {
return env.__throwException(e);
} catch (Throwable e) {
throw e;
} finally {
unsetArguments(arguments);
}
}
use of php.runtime.memory.support.MemoryOperation in project jphp by jphp-compiler.
the class UXNode method lookup.
@Signature
@SuppressWarnings("unchecked")
public Memory lookup(Environment env, TraceInfo trace, String selector) throws Throwable {
Node result = __globalLookup(getWrappedObject(), selector);
if (result == null) {
return null;
}
MemoryOperation operation = MemoryOperation.get(result.getClass(), null);
if (operation == null) {
return null;
}
return operation.unconvert(env, trace, result);
}
use of php.runtime.memory.support.MemoryOperation in project jphp by jphp-compiler.
the class UXImage method ofNative.
@Signature
public static UXImage ofNative(Environment env, Memory memory) throws Throwable {
MemoryOperation operation = MemoryOperation.get(BufferedImage.class, null);
if (operation != null) {
BufferedImage convert = (BufferedImage) operation.convert(env, env.trace(), memory);
WritableImage image = SwingFXUtils.toFXImage(convert, null);
return new UXImage(env, image);
}
return null;
}
use of php.runtime.memory.support.MemoryOperation in project jphp by jphp-compiler.
the class NativeClassEntity method newObject.
@Override
public <T extends IObject> T newObject(Environment env, TraceInfo trace, boolean doConstruct, Memory... args) throws Throwable {
Object instance = null;
for (Constructor<?> construct : rawClass.getConstructors()) {
Class<?>[] parameterTypes = construct.getParameterTypes();
if ((args == null && parameterTypes.length == 0) || (parameterTypes.length == args.length)) {
Object[] passed = new Object[args == null ? 0 : args.length];
boolean next = false;
for (int i = 0; i < passed.length; i++) {
MemoryOperation op = MemoryOperation.get(parameterTypes[i], construct.getGenericParameterTypes()[i]);
if (op == null) {
next = true;
break;
}
passed[i] = op.convert(env, trace, args[i]);
}
if (next)
continue;
try {
instance = construct.newInstance(passed);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
NativeObject object = (NativeObject) super.newObject(env, trace, doConstruct, args);
object.__setNativeObject(instance);
return (T) object;
}
use of php.runtime.memory.support.MemoryOperation in project jphp by jphp-compiler.
the class Invoker method callAny.
public final Memory callAny(Object... args) {
if (args != null && args.length > 0) {
Memory[] passed = new Memory[args.length];
for (int i = 0; i < passed.length; i++) {
if (args[i] == null) {
passed[i] = Memory.NULL;
continue;
}
MemoryOperation operation = MemoryOperation.get(args[i].getClass(), args[i].getClass().getGenericSuperclass());
if (operation == null) {
throw new CriticalException("Unsupported bind type - " + args[i].getClass().toString());
}
passed[i] = operation.unconvertNoThow(env, trace, args[i]);
}
return callNoThrow(passed);
} else {
return callNoThrow();
}
}
Aggregations