Search in sources :

Example 1 with BBlob

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

the class CPU method extractValues.

@SuppressWarnings("rawtypes")
private static BRefType[] extractValues(WorkerData data, BType[] types, int[] regs) {
    BRefType[] result = new BRefType[types.length];
    for (int i = 0; i < regs.length; i++) {
        BType paramType = types[i];
        int argReg = regs[i];
        switch(paramType.getTag()) {
            case TypeTags.INT_TAG:
                result[i] = new BInteger(data.longRegs[argReg]);
                break;
            case TypeTags.FLOAT_TAG:
                result[i] = new BFloat(data.doubleRegs[argReg]);
                break;
            case TypeTags.STRING_TAG:
                result[i] = new BString(data.stringRegs[argReg]);
                break;
            case TypeTags.BOOLEAN_TAG:
                result[i] = new BBoolean(data.intRegs[argReg] > 0);
                break;
            case TypeTags.BLOB_TAG:
                result[i] = new BBlob(data.byteRegs[argReg]);
                break;
            default:
                result[i] = data.refRegs[argReg];
        }
    }
    return result;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BBoolean(org.ballerinalang.model.values.BBoolean) BBlob(org.ballerinalang.model.values.BBlob)

Example 2 with BBlob

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

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

the class BLangVMUtils method populateReturnData.

public static BValue[] populateReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, int[] retRegs) {
    WorkerData data = ctx.workerLocal;
    BType[] retTypes = callableUnitInfo.getRetParamTypes();
    BValue[] returnValues = new BValue[retTypes.length];
    for (int i = 0; i < returnValues.length; i++) {
        BType retType = retTypes[i];
        switch(retType.getTag()) {
            case TypeTags.INT_TAG:
                returnValues[i] = new BInteger(data.longRegs[retRegs[i]]);
                break;
            case TypeTags.FLOAT_TAG:
                returnValues[i] = new BFloat(data.doubleRegs[retRegs[i]]);
                break;
            case TypeTags.STRING_TAG:
                returnValues[i] = new BString(data.stringRegs[retRegs[i]]);
                break;
            case TypeTags.BOOLEAN_TAG:
                boolean boolValue = data.intRegs[retRegs[i]] == 1;
                returnValues[i] = new BBoolean(boolValue);
                break;
            case TypeTags.BLOB_TAG:
                returnValues[i] = new BBlob(data.byteRegs[retRegs[i]]);
                break;
            default:
                returnValues[i] = data.refRegs[retRegs[i]];
                break;
        }
    }
    return returnValues;
}
Also used : BType(org.ballerinalang.model.types.BType) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BBoolean(org.ballerinalang.model.values.BBoolean) WorkerData(org.ballerinalang.bre.bvm.WorkerData) BBlob(org.ballerinalang.model.values.BBlob)

Example 4 with BBlob

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

the class BBlobValueTest method testBlobGlobalVariable.

@Test(description = "Test blob global variable")
public void testBlobGlobalVariable() {
    byte[] bytes = "string".getBytes();
    BValue[] args = { new BBlob(bytes) };
    BValue[] returns = BRunUtil.invoke(result, "testGlobalVariable", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BBlob.class);
    BBlob blob = (BBlob) returns[0];
    Assert.assertEquals(blob.blobValue(), bytes, "Invalid byte value returned.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 5 with BBlob

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

the class BBlobValueTest method testBlobArray.

@Test(description = "Test blob array")
public void testBlobArray() {
    byte[] bytes1 = "string1".getBytes();
    byte[] bytes2 = "string2".getBytes();
    BValue[] args = { new BBlob(bytes1), new BBlob(bytes2) };
    BValue[] returns = BRunUtil.invoke(result, "testBlobParameterArray", args);
    Assert.assertEquals(returns.length, 2);
    Assert.assertSame(returns[0].getClass(), BBlob.class);
    Assert.assertSame(returns[1].getClass(), BBlob.class);
    BBlob blob1 = (BBlob) returns[0];
    BBlob blob2 = (BBlob) returns[1];
    Assert.assertEquals(blob1.blobValue(), bytes1, "Invalid byte value returned.");
    Assert.assertEquals(blob2.blobValue(), bytes2, "Invalid byte value returned.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Aggregations

BBlob (org.ballerinalang.model.values.BBlob)32 BValue (org.ballerinalang.model.values.BValue)23 Test (org.testng.annotations.Test)19 BString (org.ballerinalang.model.values.BString)16 BInteger (org.ballerinalang.model.values.BInteger)11 BBoolean (org.ballerinalang.model.values.BBoolean)6 BFloat (org.ballerinalang.model.values.BFloat)6 BStruct (org.ballerinalang.model.values.BStruct)5 BType (org.ballerinalang.model.types.BType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 ByteBuffer (java.nio.ByteBuffer)2 Base64 (java.util.Base64)2 BJSON (org.ballerinalang.model.values.BJSON)2 BRefType (org.ballerinalang.model.values.BRefType)2 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)2 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CRC32 (java.util.zip.CRC32)1 Checksum (java.util.zip.Checksum)1 Context (org.ballerinalang.bre.Context)1