Search in sources :

Example 16 with BStruct

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

the class VectorTest method testAdd.

@Test(description = "Test case for testing addition of elements to a vector.")
public void testAdd() {
    String[] args = new String[] { "Foo", "Bar", "Ballerina" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testAdd", new BValue[] { buildStringArray(args) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long vectorSize = vector.getIntField(0);
    Assert.assertEquals(vectorSize, args.length);
    for (int i = 0; i < args.length; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), args[i]);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with BStruct

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

the class StartSpanWithParentSpan method execute.

@Override
public void execute(Context context) {
    String serviceName = context.getStringArgument(0);
    String spanName = context.getStringArgument(1);
    BMap tags = (BMap) context.getRefArgument(0);
    String reference = context.getRefArgument(1).stringValue();
    BStruct parentSpan = (BStruct) context.getRefArgument(2);
    PrintStream err = System.err;
    String spanId;
    if (ReferenceType.valueOf(reference) != ReferenceType.ROOT && parentSpan != null) {
        String parentSpanId = parentSpan.getStringField(0);
        spanId = OpenTracerBallerinaWrapper.getInstance().startSpan(serviceName, spanName, Utils.toStringMap(tags), ReferenceType.valueOf(reference), parentSpanId);
    } else {
        spanId = OpenTracerBallerinaWrapper.getInstance().startSpan(serviceName, spanName, Utils.toStringMap(tags), ReferenceType.valueOf(reference), Collections.emptyMap());
    }
    if (spanId != null) {
        context.setReturnValues(Utils.createSpanStruct(context, spanId, serviceName, spanName));
    } else {
        context.setReturnValues(Utils.createSpanStruct(context, null, null, null));
        err.println("ballerina: Can not use tracing API when tracing is disabled");
    }
}
Also used : PrintStream(java.io.PrintStream) BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap)

Example 18 with BStruct

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

the class CPU method invokeVirtualFunction.

private static WorkerExecutionContext invokeVirtualFunction(WorkerExecutionContext ctx, int receiver, FunctionInfo virtualFuncInfo, int[] argRegs, int[] retRegs, int flags) {
    BStruct structVal = (BStruct) ctx.workerLocal.refRegs[receiver];
    if (structVal == null) {
        ctx.setError(BLangVMErrors.createNullRefException(ctx));
        handleError(ctx);
        return null;
    }
    StructInfo structInfo = structVal.getType().structInfo;
    AttachedFunctionInfo attachedFuncInfo = structInfo.funcInfoEntries.get(virtualFuncInfo.getName());
    FunctionInfo concreteFuncInfo = attachedFuncInfo.functionInfo;
    return BLangFunctions.invokeCallable(concreteFuncInfo, ctx, argRegs, retRegs, false, flags);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) AttachedFunctionInfo(org.ballerinalang.util.codegen.AttachedFunctionInfo) StructInfo(org.ballerinalang.util.codegen.StructInfo) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) AttachedFunctionInfo(org.ballerinalang.util.codegen.AttachedFunctionInfo)

Example 19 with BStruct

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

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

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16