Search in sources :

Example 21 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class ProgramFileReader method getLocalVariableInfo.

private LocalVariableInfo getLocalVariableInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
    int varNameCPIndex = dataInStream.readInt();
    UTF8CPEntry varNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(varNameCPIndex);
    int variableIndex = dataInStream.readInt();
    int typeSigCPIndex = dataInStream.readInt();
    UTF8CPEntry typeSigCPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeSigCPIndex);
    BType type = getBTypeFromDescriptor(typeSigCPEntry.getValue());
    LocalVariableInfo localVariableInfo = new LocalVariableInfo(varNameCPEntry.getValue(), varNameCPIndex, variableIndex, typeSigCPIndex, type);
    int attchmntIndexesLength = dataInStream.readShort();
    int[] attachmentIndexes = new int[attchmntIndexesLength];
    for (int i = 0; i < attchmntIndexesLength; i++) {
        attachmentIndexes[i] = dataInStream.readInt();
    }
    localVariableInfo.setAttachmentIndexes(attachmentIndexes);
    return localVariableInfo;
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BType(org.ballerinalang.model.types.BType)

Example 22 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class ProgramFileReader method createBTypeFromSig.

private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
    int nameIndex;
    char ch = chars[index];
    switch(ch) {
        case 'I':
            typeStack.push(BTypes.typeInt);
            return index + 1;
        case 'F':
            typeStack.push(BTypes.typeFloat);
            return index + 1;
        case 'S':
            typeStack.push(BTypes.typeString);
            return index + 1;
        case 'B':
            typeStack.push(BTypes.typeBoolean);
            return index + 1;
        case 'L':
            typeStack.push(BTypes.typeBlob);
            return index + 1;
        case 'Y':
            typeStack.push(BTypes.typeDesc);
            return index + 1;
        case 'A':
            typeStack.push(BTypes.typeAny);
            return index + 1;
        case 'R':
            // TODO Improve this logic
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
            typeStack.push(BTypes.getTypeFromName(typeName));
            return nameIndex + 1;
        case 'C':
        case 'J':
        case 'T':
        case 'E':
        case 'D':
        case 'H':
        case 'Z':
            char typeChar = chars[index];
            // TODO Improve this logic
            index++;
            nameIndex = index;
            int colonIndex = -1;
            while (chars[nameIndex] != ';') {
                if (chars[nameIndex] == ':') {
                    colonIndex = nameIndex;
                }
                nameIndex++;
            }
            String pkgPath;
            String name;
            PackageInfo packageInfoOfType;
            if (colonIndex != -1) {
                pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
                name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
                packageInfoOfType = programFile.getPackageInfo(pkgPath);
            } else {
                name = new String(Arrays.copyOfRange(chars, index, nameIndex));
                // Setting the current package;
                packageInfoOfType = packageInfo;
            }
            if (typeChar == 'C') {
                typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
            } else if (typeChar == 'J') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeJSON);
                } else {
                    typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'D') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeTable);
                } else {
                    typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'H') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeStream);
                } else {
                    typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'E') {
                typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
            } else {
                // This is a struct type
                typeStack.push(packageInfoOfType.getStructInfo(name).getType());
            }
            return nameIndex + 1;
        case '[':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType elemType = typeStack.pop();
            BArrayType arrayType = new BArrayType(elemType);
            typeStack.push(arrayType);
            return index;
        case 'M':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType constrainedType = typeStack.pop();
            BType mapType;
            if (constrainedType == BTypes.typeAny) {
                mapType = BTypes.typeMap;
            } else {
                mapType = new BMapType(constrainedType);
            }
            typeStack.push(mapType);
            return index;
        case 'U':
            // TODO : Fix this for type casting.
            typeStack.push(new BFunctionType());
            return index + 1;
        case 'O':
        case 'P':
            typeChar = chars[index];
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            List<BType> memberTypes = new ArrayList<>();
            int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
            index = nameIndex;
            for (int i = 0; i < memberCount; i++) {
                index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
                memberTypes.add(typeStack.pop());
            }
            if (typeChar == 'O') {
                typeStack.push(new BUnionType(memberTypes));
            } else if (typeChar == 'P') {
                typeStack.push(new BTupleType(memberTypes));
            }
            return index + 1;
        case 'N':
            typeStack.push(BTypes.typeNull);
            return index + 1;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ArrayList(java.util.ArrayList) BTupleType(org.ballerinalang.model.types.BTupleType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) BUnionType(org.ballerinalang.model.types.BUnionType) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Example 23 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class BLangVMUtils method populateWorkerDataWithValues.

@SuppressWarnings("rawtypes")
public static void populateWorkerDataWithValues(WorkerData data, int[] regIndexes, BValue[] vals, BType[] types) {
    if (vals == null) {
        return;
    }
    for (int i = 0; i < vals.length; i++) {
        int callersRetRegIndex = regIndexes[i];
        BType retType = types[i];
        switch(retType.getTag()) {
            case TypeTags.INT_TAG:
                if (vals[i] == null) {
                    data.longRegs[callersRetRegIndex] = 0;
                    break;
                }
                data.longRegs[callersRetRegIndex] = ((BInteger) vals[i]).intValue();
                break;
            case TypeTags.FLOAT_TAG:
                if (vals[i] == null) {
                    data.doubleRegs[callersRetRegIndex] = 0;
                    break;
                }
                data.doubleRegs[callersRetRegIndex] = ((BFloat) vals[i]).floatValue();
                break;
            case TypeTags.STRING_TAG:
                if (vals[i] == null) {
                    data.stringRegs[callersRetRegIndex] = BLangConstants.STRING_EMPTY_VALUE;
                    break;
                }
                data.stringRegs[callersRetRegIndex] = vals[i].stringValue();
                break;
            case TypeTags.BOOLEAN_TAG:
                if (vals[i] == null) {
                    data.intRegs[callersRetRegIndex] = 0;
                    break;
                }
                data.intRegs[callersRetRegIndex] = ((BBoolean) vals[i]).booleanValue() ? 1 : 0;
                break;
            case TypeTags.BLOB_TAG:
                if (vals[i] == null) {
                    data.byteRegs[callersRetRegIndex] = new byte[0];
                    break;
                }
                data.byteRegs[callersRetRegIndex] = ((BBlob) vals[i]).blobValue();
                break;
            default:
                data.refRegs[callersRetRegIndex] = (BRefType) vals[i];
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean)

Example 24 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class BLangVMUtils method populateArgAndReturnData.

@SuppressWarnings("rawtypes")
public static int[][] populateArgAndReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, BValue[] args) {
    WorkerDataIndex wdi1 = callableUnitInfo.paramWorkerIndex;
    WorkerDataIndex wdi2 = callableUnitInfo.retWorkerIndex;
    WorkerData local = createWorkerData(wdi1, wdi2);
    BType[] types = callableUnitInfo.getParamTypes();
    int longParamCount = 0, doubleParamCount = 0, stringParamCount = 0, intParamCount = 0, byteParamCount = 0, refParamCount = 0;
    for (int i = 0; i < types.length; i++) {
        switch(types[i].getTag()) {
            case TypeTags.INT_TAG:
                if (args[i] instanceof BString) {
                    local.longRegs[longParamCount] = ((BString) args[i]).intValue();
                } else {
                    local.longRegs[longParamCount] = ((BInteger) args[i]).intValue();
                }
                longParamCount++;
                break;
            case TypeTags.FLOAT_TAG:
                if (args[i] instanceof BString) {
                    local.doubleRegs[doubleParamCount] = ((BString) args[i]).floatValue();
                } else {
                    local.doubleRegs[doubleParamCount] = ((BFloat) args[i]).floatValue();
                }
                doubleParamCount++;
                break;
            case TypeTags.STRING_TAG:
                local.stringRegs[stringParamCount] = args[i].stringValue();
                stringParamCount++;
                break;
            case TypeTags.BOOLEAN_TAG:
                if (args[i] instanceof BString) {
                    local.intRegs[intParamCount] = ((BString) args[i]).value().toLowerCase().equals("true") ? 1 : 0;
                } else {
                    local.intRegs[intParamCount] = ((BBoolean) args[i]).booleanValue() ? 1 : 0;
                }
                intParamCount++;
                break;
            case TypeTags.BLOB_TAG:
                local.byteRegs[byteParamCount] = ((BBlob) args[i]).blobValue();
                byteParamCount++;
                break;
            default:
                local.refRegs[refParamCount] = (BRefType) args[i];
                refParamCount++;
                break;
        }
    }
    ctx.workerLocal = local;
    return new int[][] { wdi1.retRegs, BLangVMUtils.createReturnRegValues(wdi1, wdi2, callableUnitInfo.getRetParamTypes()) };
}
Also used : BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 25 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class BLangVMUtils method copyArgValues.

public static void copyArgValues(WorkerData caller, WorkerData callee, int[] argRegs, BType[] paramTypes) {
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int refRegIndex = -1;
    int blobRegIndex = -1;
    for (int i = 0; i < argRegs.length; i++) {
        BType paramType = paramTypes[i];
        int argReg = argRegs[i];
        switch(paramType.getTag()) {
            case TypeTags.INT_TAG:
                callee.longRegs[++longRegIndex] = caller.longRegs[argReg];
                break;
            case TypeTags.FLOAT_TAG:
                callee.doubleRegs[++doubleRegIndex] = caller.doubleRegs[argReg];
                break;
            case TypeTags.STRING_TAG:
                callee.stringRegs[++stringRegIndex] = caller.stringRegs[argReg];
                break;
            case TypeTags.BOOLEAN_TAG:
                callee.intRegs[++booleanRegIndex] = caller.intRegs[argReg];
                break;
            case TypeTags.BLOB_TAG:
                callee.byteRegs[++blobRegIndex] = caller.byteRegs[argReg];
                break;
            default:
                callee.refRegs[++refRegIndex] = caller.refRegs[argReg];
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType)

Aggregations

BType (org.ballerinalang.model.types.BType)48 BStructType (org.ballerinalang.model.types.BStructType)16 BString (org.ballerinalang.model.values.BString)16 BStruct (org.ballerinalang.model.values.BStruct)15 BValue (org.ballerinalang.model.values.BValue)13 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)12 BBoolean (org.ballerinalang.model.values.BBoolean)11 BArrayType (org.ballerinalang.model.types.BArrayType)7 BFloat (org.ballerinalang.model.values.BFloat)6 BInteger (org.ballerinalang.model.values.BInteger)6 BMapType (org.ballerinalang.model.types.BMapType)4 BRefType (org.ballerinalang.model.values.BRefType)4 SQLException (java.sql.SQLException)3 Struct (java.sql.Struct)3 ArrayList (java.util.ArrayList)3 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)3 BBlob (org.ballerinalang.model.values.BBlob)3 Message (org.ballerinalang.net.grpc.Message)3 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)3 StructInfo (org.ballerinalang.util.codegen.StructInfo)3