Search in sources :

Example 6 with BType

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

the class CPU method copyArgValues.

public static void copyArgValues(WorkerData callerSF, WorkerData calleeSF, 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:
                calleeSF.longRegs[++longRegIndex] = callerSF.longRegs[argReg];
                break;
            case TypeTags.FLOAT_TAG:
                calleeSF.doubleRegs[++doubleRegIndex] = callerSF.doubleRegs[argReg];
                break;
            case TypeTags.STRING_TAG:
                calleeSF.stringRegs[++stringRegIndex] = callerSF.stringRegs[argReg];
                break;
            case TypeTags.BOOLEAN_TAG:
                calleeSF.intRegs[++booleanRegIndex] = callerSF.intRegs[argReg];
                break;
            case TypeTags.BLOB_TAG:
                calleeSF.byteRegs[++blobRegIndex] = callerSF.byteRegs[argReg];
                break;
            default:
                calleeSF.refRegs[++refRegIndex] = callerSF.refRegs[argReg];
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType)

Example 7 with BType

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

the class CPU method convertStructToMap.

private static void convertStructToMap(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int j = operands[1];
    BStruct bStruct = (BStruct) sf.refRegs[i];
    if (bStruct == null) {
        handleNullRefError(ctx);
        return;
    }
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int blobRegIndex = -1;
    int refRegIndex = -1;
    BStructType.StructField[] structFields = (bStruct.getType()).getStructFields();
    BMap<String, BValue> map = BTypes.typeMap.getEmptyValue();
    for (BStructType.StructField structField : structFields) {
        String key = structField.getFieldName();
        BType fieldType = structField.getFieldType();
        switch(fieldType.getTag()) {
            case TypeTags.INT_TAG:
                map.put(key, new BInteger(bStruct.getIntField(++longRegIndex)));
                break;
            case TypeTags.FLOAT_TAG:
                map.put(key, new BFloat(bStruct.getFloatField(++doubleRegIndex)));
                break;
            case TypeTags.STRING_TAG:
                map.put(key, new BString(bStruct.getStringField(++stringRegIndex)));
                break;
            case TypeTags.BOOLEAN_TAG:
                map.put(key, new BBoolean(bStruct.getBooleanField(++booleanRegIndex) == 1));
                break;
            case TypeTags.BLOB_TAG:
                map.put(key, new BBlob(bStruct.getBlobField(++blobRegIndex)));
                break;
            default:
                BValue value = bStruct.getRefField(++refRegIndex);
                map.put(key, value == null ? null : value.copy());
        }
    }
    sf.refRegs[j] = map;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BFloat(org.ballerinalang.model.values.BFloat) BBlob(org.ballerinalang.model.values.BBlob)

Example 8 with BType

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

the class CPU method copyArgValuesForWorkerReceive.

@SuppressWarnings("rawtypes")
public static void copyArgValuesForWorkerReceive(WorkerData currentSF, int[] argRegs, BType[] paramTypes, BRefType[] passedInValues) {
    for (int i = 0; i < argRegs.length; i++) {
        int regIndex = argRegs[i];
        BType paramType = paramTypes[i];
        switch(paramType.getTag()) {
            case TypeTags.INT_TAG:
                currentSF.longRegs[regIndex] = ((BInteger) passedInValues[i]).intValue();
                break;
            case TypeTags.FLOAT_TAG:
                currentSF.doubleRegs[regIndex] = ((BFloat) passedInValues[i]).floatValue();
                break;
            case TypeTags.STRING_TAG:
                currentSF.stringRegs[regIndex] = (passedInValues[i]).stringValue();
                break;
            case TypeTags.BOOLEAN_TAG:
                currentSF.intRegs[regIndex] = (((BBoolean) passedInValues[i]).booleanValue()) ? 1 : 0;
                break;
            case TypeTags.BLOB_TAG:
                currentSF.byteRegs[regIndex] = ((BBlob) passedInValues[i]).blobValue();
                break;
            default:
                currentSF.refRegs[regIndex] = (BRefType) passedInValues[i];
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType)

Example 9 with BType

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

the class CPU method handleVariableLock.

private static boolean handleVariableLock(WorkerExecutionContext ctx, BType[] types, int[] varRegs) {
    boolean lockAcquired = true;
    for (int i = 0; i < varRegs.length && lockAcquired; i++) {
        BType paramType = types[i];
        int regIndex = varRegs[i];
        switch(paramType.getTag()) {
            case TypeTags.INT_TAG:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockIntField(ctx, regIndex);
                break;
            case TypeTags.FLOAT_TAG:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockFloatField(ctx, regIndex);
                break;
            case TypeTags.STRING_TAG:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockStringField(ctx, regIndex);
                break;
            case TypeTags.BOOLEAN_TAG:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockBooleanField(ctx, regIndex);
                break;
            case TypeTags.BLOB_TAG:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockBlobField(ctx, regIndex);
                break;
            default:
                lockAcquired = ctx.programFile.getGlobalMemoryBlock().lockRefField(ctx, regIndex);
        }
    }
    return lockAcquired;
}
Also used : BType(org.ballerinalang.model.types.BType)

Example 10 with BType

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

the class CPU method convertMapToStruct.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void convertMapToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i];
    if (bMap == null) {
        handleNullRefError(ctx);
        return;
    }
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int blobRegIndex = -1;
    int refRegIndex = -1;
    BStructType structType = (BStructType) typeRefCPEntry.getType();
    BStruct bStruct = new BStruct(structType);
    StructInfo structInfo = ctx.callableUnitInfo.getPackageInfo().getStructInfo(structType.getName());
    Set<String> keys = bMap.keySet();
    for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
        String key = fieldInfo.getName();
        BType fieldType = fieldInfo.getFieldType();
        BValue mapVal = null;
        try {
            boolean containsField = keys.contains(key);
            DefaultValueAttributeInfo defaultValAttrInfo = null;
            if (containsField) {
                mapVal = bMap.get(key);
                if (mapVal == null && BTypes.isValueType(fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, null);
                }
                if (mapVal != null && !checkCast(mapVal, fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, mapVal.getType());
                }
            } else {
                defaultValAttrInfo = (DefaultValueAttributeInfo) getAttributeInfo(fieldInfo, AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE);
            }
            switch(fieldType.getTag()) {
                case TypeTags.INT_TAG:
                    longRegIndex++;
                    if (containsField) {
                        bStruct.setIntField(longRegIndex, ((BInteger) mapVal).intValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setIntField(longRegIndex, defaultValAttrInfo.getDefaultValue().getIntValue());
                    }
                    break;
                case TypeTags.FLOAT_TAG:
                    doubleRegIndex++;
                    if (containsField) {
                        bStruct.setFloatField(doubleRegIndex, ((BFloat) mapVal).floatValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setFloatField(doubleRegIndex, defaultValAttrInfo.getDefaultValue().getFloatValue());
                    }
                    break;
                case TypeTags.STRING_TAG:
                    stringRegIndex++;
                    if (containsField) {
                        bStruct.setStringField(stringRegIndex, ((BString) mapVal).stringValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setStringField(stringRegIndex, defaultValAttrInfo.getDefaultValue().getStringValue());
                    }
                    break;
                case TypeTags.BOOLEAN_TAG:
                    booleanRegIndex++;
                    if (containsField) {
                        bStruct.setBooleanField(booleanRegIndex, ((BBoolean) mapVal).booleanValue() ? 1 : 0);
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setBooleanField(booleanRegIndex, defaultValAttrInfo.getDefaultValue().getBooleanValue() ? 1 : 0);
                    }
                    break;
                case TypeTags.BLOB_TAG:
                    blobRegIndex++;
                    if (containsField && mapVal != null) {
                        bStruct.setBlobField(blobRegIndex, ((BBlob) mapVal).blobValue());
                    }
                    break;
                default:
                    bStruct.setRefField(++refRegIndex, (BRefType) mapVal);
            }
        } catch (BallerinaException e) {
            sf.refRegs[j] = null;
            String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage();
            handleTypeConversionError(ctx, sf, j, errorMsg);
            return;
        }
    }
    sf.refRegs[j] = bStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) StructInfo(org.ballerinalang.util.codegen.StructInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

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