Search in sources :

Example 11 with BStructType

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

the class CPU method execStoreOpcodes.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void execStoreOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    int k;
    int fieldIndex;
    BIntArray bIntArray;
    BFloatArray bFloatArray;
    BStringArray bStringArray;
    BBooleanArray bBooleanArray;
    BBlobArray bBlobArray;
    BRefValueArray bArray;
    StructureType structureType;
    BMap<String, BRefType> bMap;
    BJSON jsonVal;
    switch(opcode) {
        case InstructionCodes.IASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bIntArray = (BIntArray) sf.refRegs[i];
            if (bIntArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bIntArray.add(sf.longRegs[j], sf.longRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.FASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bFloatArray = (BFloatArray) sf.refRegs[i];
            if (bFloatArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bFloatArray.add(sf.longRegs[j], sf.doubleRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.SASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bStringArray = (BStringArray) sf.refRegs[i];
            if (bStringArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bStringArray.add(sf.longRegs[j], sf.stringRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.BASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bBooleanArray = (BBooleanArray) sf.refRegs[i];
            if (bBooleanArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bBooleanArray.add(sf.longRegs[j], sf.intRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.LASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bBlobArray = (BBlobArray) sf.refRegs[i];
            if (bBlobArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bBlobArray.add(sf.longRegs[j], sf.byteRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.RASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bArray = (BRefValueArray) sf.refRegs[i];
            if (bArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                bArray.add(sf.longRegs[j], sf.refRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.JSONASTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            jsonVal = (BJSON) sf.refRegs[i];
            if (jsonVal == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                JSONUtils.setArrayElement(jsonVal, sf.longRegs[j], (BJSON) sf.refRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.IGSTORE:
            // Stack reg index
            i = operands[0];
            // Global var index
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setIntField(j, sf.longRegs[i]);
            break;
        case InstructionCodes.FGSTORE:
            i = operands[0];
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setFloatField(j, sf.doubleRegs[i]);
            break;
        case InstructionCodes.SGSTORE:
            i = operands[0];
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setStringField(j, sf.stringRegs[i]);
            break;
        case InstructionCodes.BGSTORE:
            i = operands[0];
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setBooleanField(j, sf.intRegs[i]);
            break;
        case InstructionCodes.LGSTORE:
            i = operands[0];
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setBlobField(j, sf.byteRegs[i]);
            break;
        case InstructionCodes.RGSTORE:
            i = operands[0];
            j = operands[1];
            ctx.programFile.getGlobalMemoryBlock().setRefField(j, sf.refRegs[i]);
            break;
        case InstructionCodes.IFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setIntField(fieldIndex, sf.longRegs[j]);
            break;
        case InstructionCodes.FFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setFloatField(fieldIndex, sf.doubleRegs[j]);
            break;
        case InstructionCodes.SFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setStringField(fieldIndex, sf.stringRegs[j]);
            break;
        case InstructionCodes.BFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setBooleanField(fieldIndex, sf.intRegs[j]);
            break;
        case InstructionCodes.LFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setBlobField(fieldIndex, sf.byteRegs[j]);
            break;
        case InstructionCodes.RFIELDSTORE:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            structureType.setRefField(fieldIndex, sf.refRegs[j]);
            break;
        case InstructionCodes.MAPSTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bMap = (BMap<String, BRefType>) sf.refRegs[i];
            if (bMap == null) {
                handleNullRefError(ctx);
                break;
            }
            BMapType mapType = (BMapType) bMap.getType();
            if (sf.refRegs[k] == null) {
                bMap.put(sf.stringRegs[j], sf.refRegs[k]);
            } else if (mapType.getConstrainedType() == BTypes.typeAny || mapType.getConstrainedType().equals(sf.refRegs[k].getType())) {
                bMap.put(sf.stringRegs[j], sf.refRegs[k]);
            } else if (sf.refRegs[k].getType().getTag() == TypeTags.STRUCT_TAG && mapType.getConstrainedType().getTag() == TypeTags.STRUCT_TAG && checkStructEquivalency((BStructType) sf.refRegs[k].getType(), (BStructType) mapType.getConstrainedType())) {
                bMap.put(sf.stringRegs[j], sf.refRegs[k]);
            } else {
                ctx.setError(BLangVMErrors.createError(ctx, BLangExceptionHelper.getErrorMessage(RuntimeErrors.INVALID_MAP_INSERTION, mapType.getConstrainedType(), sf.refRegs[k].getType())));
                handleError(ctx);
                break;
            }
            break;
        case InstructionCodes.JSONSTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            jsonVal = (BJSON) sf.refRegs[i];
            if (jsonVal == null) {
                handleNullRefError(ctx);
                break;
            }
            JSONUtils.setElement(jsonVal, sf.stringRegs[j], (BJSON) sf.refRegs[k]);
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BIntArray(org.ballerinalang.model.values.BIntArray) BBlobArray(org.ballerinalang.model.values.BBlobArray) BStructType(org.ballerinalang.model.types.BStructType) BMapType(org.ballerinalang.model.types.BMapType) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) StructureType(org.ballerinalang.model.values.StructureType) BFloatArray(org.ballerinalang.model.values.BFloatArray)

Example 12 with BStructType

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

the class BLangVMStructs method createBStruct.

/**
 * Create BStruct for given StructInfo and BValues.
 *
 * @param structInfo {@link StructInfo} of the BStruct
 * @param values     field values of the BStruct.
 * @return BStruct instance.
 */
public static BStruct createBStruct(StructInfo structInfo, Object... values) {
    BStructType structType = structInfo.getType();
    BStruct bStruct = new BStruct(structType);
    int[] indexes = new int[] { -1, -1, -1, -1, -1, -1 };
    BStructType.StructField[] structFields = structType.getStructFields();
    for (int i = 0; i < structFields.length; i++) {
        if (values.length < i + 1) {
            break;
        }
        BType paramType = structFields[i].getFieldType();
        setValue(bStruct, indexes, paramType.getTag(), values[i]);
    }
    return bStruct;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BType(org.ballerinalang.model.types.BType)

Example 13 with BStructType

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

the class TableOMDataSource method serialize.

@Override
public void serialize(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    xmlStreamWriter.writeStartElement("", this.rootWrapper, "");
    while (table.hasNext(this.isInTransaction)) {
        table.next();
        xmlStreamWriter.writeStartElement("", this.rowWrapper, "");
        BStructType structType = table.getStructType();
        BStructType.StructField[] structFields = null;
        if (structType != null) {
            structFields = structType.getStructFields();
        }
        int index = 1;
        for (ColumnDefinition col : table.getColumnDefs()) {
            String name;
            if (structFields != null) {
                name = structFields[index - 1].getFieldName();
            } else {
                name = col.getName();
            }
            writeElement(xmlStreamWriter, name, col.getType(), index, structFields);
            ++index;
        }
        xmlStreamWriter.writeEndElement();
    }
    xmlStreamWriter.writeEndElement();
    table.close(isInTransaction);
    xmlStreamWriter.flush();
}
Also used : BStructType(org.ballerinalang.model.types.BStructType)

Example 14 with BStructType

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

the class BCompileUtil method createAndGetStruct.

public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
    PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
    StructInfo structInfo = structPackageInfo.getStructInfo(structName);
    BStructType structType = structInfo.getType();
    return new BStruct(structType);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 15 with BStructType

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

the class JSONUtils method convertJSON.

private static Object convertJSON(JsonNode jsonValue, BType targetType) {
    switch(targetType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToInt(jsonValue);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToFloat(jsonValue);
        case TypeTags.STRING_TAG:
            if (jsonValue.isString()) {
                return jsonValue.stringValue();
            } else {
                return jsonValue.toString();
            }
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBool(jsonValue);
        case TypeTags.UNION_TAG:
            BUnionType type = (BUnionType) targetType;
            if (jsonValue.isNull() && type.isNullable()) {
                return null;
            }
            List<BType> matchingTypes = type.getMemberTypes().stream().filter(memberType -> memberType != BTypes.typeNull).collect(Collectors.toList());
            if (matchingTypes.size() == 1) {
                return convertJSON(jsonValue, matchingTypes.get(0));
            }
            break;
        case TypeTags.STRUCT_TAG:
            return convertJSONNodeToStruct(jsonValue, (BStructType) targetType);
        case TypeTags.ANY_TAG:
        case TypeTags.JSON_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            return new BJSON(jsonValue);
        case TypeTags.ARRAY_TAG:
            return jsonNodeToBArray(jsonValue, (BArrayType) targetType);
        case TypeTags.MAP_TAG:
            return jsonNodeToBMap(jsonValue, (BMapType) targetType);
        case TypeTags.NULL_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            break;
        default:
            break;
    }
    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, targetType, getTypeName(jsonValue));
}
Also used : BUnionType(org.ballerinalang.model.types.BUnionType) BUnionType(org.ballerinalang.model.types.BUnionType) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean) OMText(org.apache.axiom.om.OMText) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BMapType(org.ballerinalang.model.types.BMapType) RuntimeErrors(org.ballerinalang.util.exceptions.RuntimeErrors) ArrayList(java.util.ArrayList) BJSON(org.ballerinalang.model.values.BJSON) BNewArray(org.ballerinalang.model.values.BNewArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) OMNamespace(org.apache.axiom.om.OMNamespace) BString(org.ballerinalang.model.values.BString) TableJSONDataSource(org.ballerinalang.model.TableJSONDataSource) BInteger(org.ballerinalang.model.values.BInteger) BXML(org.ballerinalang.model.values.BXML) BTypes(org.ballerinalang.model.types.BTypes) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) BMap(org.ballerinalang.model.values.BMap) OMElement(org.apache.axiom.om.OMElement) BFloatArray(org.ballerinalang.model.values.BFloatArray) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) Iterator(java.util.Iterator) Type(org.ballerinalang.model.util.JsonNode.Type) Set(java.util.Set) BStructType(org.ballerinalang.model.types.BStructType) BXMLItem(org.ballerinalang.model.values.BXMLItem) BLangExceptionHelper(org.ballerinalang.util.exceptions.BLangExceptionHelper) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) OMAbstractFactory(org.apache.axiom.om.OMAbstractFactory) BRefType(org.ballerinalang.model.values.BRefType) Entry(java.util.Map.Entry) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) OMFactory(org.apache.axiom.om.OMFactory) BTable(org.ballerinalang.model.values.BTable) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) BJSON(org.ballerinalang.model.values.BJSON)

Aggregations

BStructType (org.ballerinalang.model.types.BStructType)40 BStruct (org.ballerinalang.model.values.BStruct)24 BType (org.ballerinalang.model.types.BType)13 BString (org.ballerinalang.model.values.BString)11 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)11 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)7 BValue (org.ballerinalang.model.values.BValue)7 StructInfo (org.ballerinalang.util.codegen.StructInfo)7 BRefType (org.ballerinalang.model.values.BRefType)6 BBoolean (org.ballerinalang.model.values.BBoolean)5 BJSON (org.ballerinalang.model.values.BJSON)5 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)5 Struct (java.sql.Struct)4 BFloat (org.ballerinalang.model.values.BFloat)4 BInteger (org.ballerinalang.model.values.BInteger)4 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)4 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 BMapType (org.ballerinalang.model.types.BMapType)3 BMap (org.ballerinalang.model.values.BMap)3