Search in sources :

Example 11 with VariableType

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;
}
Also used : VariableType(io.nuls.contract.vm.code.VariableType) ObjectRef(io.nuls.contract.vm.ObjectRef) Result(io.nuls.contract.vm.Result)

Example 12 with VariableType

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

Example 13 with VariableType

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;
}
Also used : ClassCode(io.nuls.contract.vm.code.ClassCode) VariableType(io.nuls.contract.vm.code.VariableType) ObjectRef(io.nuls.contract.vm.ObjectRef) Result(io.nuls.contract.vm.Result)

Example 14 with VariableType

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;
}
Also used : VariableType(io.nuls.contract.vm.code.VariableType) ObjectRef(io.nuls.contract.vm.ObjectRef) Result(io.nuls.contract.vm.Result)

Example 15 with VariableType

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;
}
Also used : VariableType(io.nuls.contract.vm.code.VariableType) ObjectRef(io.nuls.contract.vm.ObjectRef) Result(io.nuls.contract.vm.Result)

Aggregations

VariableType (io.nuls.contract.vm.code.VariableType)21 ObjectRef (io.nuls.contract.vm.ObjectRef)18 Result (io.nuls.contract.vm.Result)12 ClassCode (io.nuls.contract.vm.code.ClassCode)4 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)3 MethodArgs (io.nuls.contract.vm.MethodArgs)2 MethodCode (io.nuls.contract.vm.code.MethodCode)2 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)2 ProgramMethodArg (io.nuls.contract.vm.program.ProgramMethodArg)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MultiANewArrayInsnNode (org.objectweb.asm.tree.MultiANewArrayInsnNode)1