Search in sources :

Example 56 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorTest method testIsEmpty.

@Test(description = "Test case for testing isEmpty() function")
public void testIsEmpty() {
    final int booleanTrue = 1;
    int vectorSize = 10;
    boolean[] expectedVals = new boolean[] { false, true, true };
    BValue[] returns = BRunUtil.invoke(compileResult, "testIsEmpty", new BValue[] { new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BBooleanArray isEmptyVals = (BBooleanArray) returns[0];
    for (int i = 0; i < expectedVals.length; i++) {
        Assert.assertEquals(isEmptyVals.get(i) == booleanTrue, expectedVals[i]);
    }
}
Also used : BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 57 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class CPU method calculateLength.

@SuppressWarnings("rawtypes")
private static void calculateLength(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    int typeTag = typeRefCPEntry.getType().getTag();
    if (typeTag == TypeTags.STRING_TAG) {
        String value = sf.stringRegs[i];
        if (value == null) {
            handleNullRefError(ctx);
        } else {
            sf.longRegs[j] = value.length();
        }
        return;
    } else if (typeTag == TypeTags.BLOB_TAG) {
        // Here it is assumed null is not supported for blob type
        sf.longRegs[j] = sf.byteRegs[i].length;
        return;
    }
    BValue entity = sf.refRegs[i];
    if (entity == null) {
        handleNullRefError(ctx);
        return;
    }
    if (typeTag == TypeTags.XML_TAG) {
        sf.longRegs[j] = ((BXML) entity).length();
        return;
    } else if (entity instanceof BJSON) {
        if (JSONUtils.isJSONArray((BJSON) entity)) {
            sf.longRegs[j] = JSONUtils.getJSONArrayLength((BJSON) sf.refRegs[i]);
        } else {
            sf.longRegs[j] = -1;
        }
        return;
    } else if (typeTag == TypeTags.MAP_TAG) {
        sf.longRegs[j] = ((BMap) entity).size();
        return;
    }
    BNewArray newArray = (BNewArray) entity;
    sf.longRegs[j] = newArray.size();
    return;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON)

Example 58 with BValue

use of org.ballerinalang.model.values.BValue 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 59 with BValue

use of org.ballerinalang.model.values.BValue 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)

Example 60 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class CPU method execIteratorOperation.

private static void execIteratorOperation(WorkerExecutionContext ctx, WorkerData sf, Instruction instruction) {
    int i, j;
    BCollection collection;
    BIterator iterator;
    InstructionIteratorNext nextInstruction;
    switch(instruction.getOpcode()) {
        case InstructionCodes.ITR_NEW:
            // collection
            i = instruction.getOperands()[0];
            // iterator variable (ref) index.
            j = instruction.getOperands()[1];
            collection = (BCollection) sf.refRegs[i];
            if (collection == null) {
                handleNullRefError(ctx);
                return;
            }
            sf.refRegs[j] = collection.newIterator();
            break;
        case InstructionCodes.ITR_HAS_NEXT:
            // iterator
            i = instruction.getOperands()[0];
            // boolean variable index to store has next result
            j = instruction.getOperands()[1];
            iterator = (BIterator) sf.refRegs[i];
            if (iterator == null) {
                sf.intRegs[j] = 0;
                return;
            }
            sf.intRegs[j] = iterator.hasNext() ? 1 : 0;
            break;
        case InstructionCodes.ITR_NEXT:
            nextInstruction = (InstructionIteratorNext) instruction;
            iterator = (BIterator) sf.refRegs[nextInstruction.iteratorIndex];
            if (iterator == null) {
                return;
            }
            BValue[] values = iterator.getNext(nextInstruction.arity);
            copyValuesToRegistries(nextInstruction.typeTags, nextInstruction.retRegs, values, sf);
            break;
    }
}
Also used : BValue(org.ballerinalang.model.values.BValue) InstructionIteratorNext(org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext) BIterator(org.ballerinalang.model.values.BIterator) BCollection(org.ballerinalang.model.values.BCollection)

Aggregations

BValue (org.ballerinalang.model.values.BValue)1043 Test (org.testng.annotations.Test)923 BString (org.ballerinalang.model.values.BString)437 BInteger (org.ballerinalang.model.values.BInteger)323 BStruct (org.ballerinalang.model.values.BStruct)188 BFloat (org.ballerinalang.model.values.BFloat)118 BJSON (org.ballerinalang.model.values.BJSON)112 BBoolean (org.ballerinalang.model.values.BBoolean)79 CompileResult (org.ballerinalang.launcher.util.CompileResult)60 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)45 BMap (org.ballerinalang.model.values.BMap)43 BXMLItem (org.ballerinalang.model.values.BXMLItem)42 BXML (org.ballerinalang.model.values.BXML)40 BStringArray (org.ballerinalang.model.values.BStringArray)30 BIntArray (org.ballerinalang.model.values.BIntArray)25 BBlob (org.ballerinalang.model.values.BBlob)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)19 BeforeTest (org.testng.annotations.BeforeTest)19 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)19 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)16