use of io.nuls.contract.vm.code.VariableType in project nuls by nuls-io.
the class NativeMethod method result.
public static Result result(MethodCode methodCode, Object resultValue, Frame frame) {
VariableType variableType = methodCode.returnVariableType;
Result result = new Result(variableType);
if (variableType.isNotVoid()) {
result.value(resultValue);
if (resultValue == null) {
frame.operandStack.pushRef(null);
} else if (variableType.isPrimitive()) {
frame.operandStack.push(resultValue, variableType);
} else if (resultValue instanceof ObjectRef) {
frame.operandStack.pushRef((ObjectRef) resultValue);
} else {
throw new IllegalArgumentException("unknown result value");
}
}
return result;
}
use of io.nuls.contract.vm.code.VariableType in project nuls by nuls-io.
the class VM method runArgs.
private Object[] runArgs(ObjectRef objectRef, MethodCode methodCode, String[][] args) {
final List runArgs = new ArrayList();
runArgs.add(objectRef);
final List<VariableType> argsVariableType = methodCode.argsVariableType;
for (int i = 0; i < argsVariableType.size(); i++) {
final VariableType variableType = argsVariableType.get(i);
final ProgramMethodArg programMethodArg = methodCode.args.get(i);
final String[] arg = args[i];
String realArg = null;
if (arg != null && arg.length > 0) {
realArg = arg[0];
}
if (programMethodArg.isRequired()) {
if (arg == null || arg.length < 1 || (!variableType.isArray() && StringUtils.isEmpty(realArg))) {
throw new RuntimeException(String.format("parameter %s required", programMethodArg.getName()));
}
}
if (arg == null || arg.length == 0) {
runArgs.add(null);
} else if (variableType.isArray()) {
if (arg.length < 1) {
runArgs.add(null);
} else if (variableType.isPrimitiveType()) {
Object array = Array.newInstance(variableType.getComponentType().getPrimitiveTypeClass(), arg.length);
for (int j = 0; j < arg.length; j++) {
String item = arg[j];
Object value = variableType.getComponentType().getPrimitiveValue(item);
Array.set(array, j, value);
}
final ObjectRef ref = this.heap.newArray(array, variableType, arg.length);
runArgs.add(ref);
} else if (variableType.getComponentType().isWrapperType()) {
ObjectRef arrayRef = this.heap.newArray(variableType, arg.length);
for (int j = 0; j < arg.length; j++) {
String item = arg[j];
if (item == null) {
continue;
}
ObjectRef ref;
if (VariableType.CHAR_WRAPPER_TYPE.equals(variableType.getComponentType())) {
ref = this.heap.newCharacter(item.charAt(0));
} else {
ref = this.heap.runNewObject(variableType.getComponentType(), item);
}
if (isEnd()) {
return null;
}
this.heap.putArray(arrayRef, j, ref);
}
runArgs.add(arrayRef);
} else {
ObjectRef arrayRef = this.heap.newArray(VariableType.STRING_ARRAY_TYPE, arg.length);
for (int j = 0; j < arg.length; j++) {
String item = arg[j];
ObjectRef ref = this.heap.newString(item);
this.heap.putArray(arrayRef, j, ref);
}
runArgs.add(arrayRef);
}
} else if (variableType.isPrimitive()) {
final Object primitiveValue = variableType.getPrimitiveValue(realArg);
runArgs.add(primitiveValue);
if (variableType.isLong() || variableType.isDouble()) {
runArgs.add(null);
}
} else if (VariableType.STRING_TYPE.equals(variableType)) {
final ObjectRef ref = this.heap.newString(realArg);
runArgs.add(ref);
} else {
final ObjectRef ref = this.heap.runNewObject(variableType, realArg);
if (isEnd()) {
return null;
}
runArgs.add(ref);
}
}
return runArgs.toArray();
}
use of io.nuls.contract.vm.code.VariableType in project nuls by nuls-io.
the class NativeClass method getSuperclass.
/**
* native
*
* @see Class#getSuperclass()
*/
private static Result getSuperclass(MethodCode methodCode, MethodArgs methodArgs, Frame frame) {
ObjectRef objectRef = methodArgs.objectRef;
VariableType variableType;
if (objectRef.getVariableType().isArray()) {
variableType = objectRef.getVariableType();
} else {
variableType = VariableType.valueOf(objectRef.getRef());
}
ClassCode classCode = frame.methodArea.loadClass(variableType.getType());
VariableType superVariableType = VariableType.valueOf(classCode.superName);
ObjectRef classRef = frame.heap.getClassRef(superVariableType.getDesc());
Result result = NativeMethod.result(methodCode, classRef, frame);
return result;
}
use of io.nuls.contract.vm.code.VariableType in project nuls by nuls-io.
the class NativeClass method isArray.
/**
* native
*
* @see Class#isArray()
*/
private static Result isArray(MethodCode methodCode, MethodArgs methodArgs, Frame frame) {
ObjectRef objectRef = methodArgs.objectRef;
VariableType variableType;
if (objectRef.getVariableType().isArray()) {
variableType = objectRef.getVariableType();
} else {
variableType = VariableType.valueOf(objectRef.getRef());
}
boolean b = variableType.isArray();
Result result = NativeMethod.result(methodCode, b, frame);
return result;
}
use of io.nuls.contract.vm.code.VariableType in project nuls by nuls-io.
the class NativeClass method getPrimitiveClass.
/**
* native
*
* @see Class#getPrimitiveClass(String)
*/
private static Result getPrimitiveClass(MethodCode methodCode, MethodArgs methodArgs, Frame frame) {
ObjectRef objectRef = (ObjectRef) methodArgs.invokeArgs[0];
String name = frame.heap.runToString(objectRef);
VariableType variableType = VariableType.valueOf(name);
ObjectRef classRef = frame.heap.getClassRef(variableType.getDesc());
Result result = NativeMethod.result(methodCode, classRef, frame);
return result;
}
Aggregations