Search in sources :

Example 1 with ProgramMethodArg

use of io.nuls.contract.vm.program.ProgramMethodArg 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();
}
Also used : VariableType(io.nuls.contract.vm.code.VariableType) ProgramMethodArg(io.nuls.contract.vm.program.ProgramMethodArg) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

VariableType (io.nuls.contract.vm.code.VariableType)1 ProgramMethodArg (io.nuls.contract.vm.program.ProgramMethodArg)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1