Search in sources :

Example 11 with BRefType

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

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

the class MessageUtils method generateRequestStruct.

public static BValue generateRequestStruct(Message request, String fieldName, BType structType, Context context) {
    BValue bValue = null;
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    if (structType instanceof BStructType) {
        BStruct requestStruct = createStruct(context, fieldName);
        for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
            String structFieldName = structField.getFieldName();
            if (structField.getFieldType() instanceof BRefType) {
                BType bType = structField.getFieldType();
                if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
                    Message message = (Message) request.getFields().get(structFieldName);
                    requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, structFieldName, structField.getFieldType(), context));
                }
            } else {
                if (request.getFields().containsKey(structFieldName)) {
                    String fieldType = structField.getFieldType().getName();
                    switch(fieldType) {
                        case STRING:
                            {
                                requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
                                break;
                            }
                        case INT:
                            {
                                requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
                                break;
                            }
                        case FLOAT:
                            {
                                Float value = (Float) request.getFields().get(structFieldName);
                                if (value != null) {
                                    requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
                                }
                                break;
                            }
                        case DOUBLE:
                            {
                                Double value = (Double) request.getFields().get(structFieldName);
                                if (value != null) {
                                    requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
                                }
                                break;
                            }
                        case BOOLEAN:
                            {
                                requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
                                break;
                            }
                        default:
                            {
                                throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
                            }
                    }
                }
            }
        }
        bValue = requestStruct;
    } else {
        Map<String, Object> fields = request.getFields();
        if (fields.size() == 1 && fields.containsKey("value")) {
            fieldName = "value";
        }
        if (request.getFields().containsKey(fieldName)) {
            String fieldType = structType.getName();
            switch(fieldType) {
                case STRING:
                    {
                        bValue = new BString((String) request.getFields().get(fieldName));
                        break;
                    }
                case INT:
                    {
                        bValue = new BInteger((Long) request.getFields().get(fieldName));
                        break;
                    }
                case FLOAT:
                    {
                        Float value = (Float) request.getFields().get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        Double value = (Double) request.getFields().get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case BOOLEAN:
                    {
                        bValue = new BBoolean((Boolean) request.getFields().get(fieldName));
                        break;
                    }
                default:
                    {
                        throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
                    }
            }
        }
    }
    return bValue;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) 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) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BFloat(org.ballerinalang.model.values.BFloat)

Example 13 with BRefType

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

the class JSONUtils method convertArrayToJSON.

/**
 * Convert {@link BRefValueArray} to {@link BJSON}.
 *
 * @param refValueArray {@link BRefValueArray} to be converted to {@link BJSON}
 * @return JSON representation of the provided refValueArray
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static BJSON convertArrayToJSON(BRefValueArray refValueArray) {
    BJSON bjson = new BJSON(new JsonNode(Type.ARRAY));
    JsonNode arrayNode = bjson.value();
    for (int i = 0; i < refValueArray.size(); i++) {
        BRefType value = refValueArray.get(i);
        if (value == null) {
            arrayNode.add(new BJSON(NULL).value());
        } else if (value.getType().getTag() == TypeTags.MAP_TAG) {
            arrayNode.add(convertMapToJSON((BMap<String, BValue>) value).value());
        } else if (value instanceof BJSON) {
            arrayNode.add(((BJSON) value).value());
        } else if (value instanceof BStruct) {
            arrayNode.add(convertStructToJSON((BStruct) value).value());
        } else if (value instanceof BNewArray) {
            arrayNode.add(convertArrayToJSON((BNewArray) value).value());
        } else {
            throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, value.getType());
        }
    }
    return bjson;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BJSON(org.ballerinalang.model.values.BJSON)

Example 14 with BRefType

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

the class TableProvider method prepareAndExecuteStatement.

private void prepareAndExecuteStatement(String queryStatement, BRefValueArray params) {
    PreparedStatement stmt = null;
    Connection conn = this.getConnection();
    try {
        stmt = conn.prepareStatement(queryStatement);
        for (int index = 1; index <= params.size(); index++) {
            BRefType param = params.get(index - 1);
            switch(param.getType().getTag()) {
                case TypeTags.INT_TAG:
                    stmt.setLong(index, (Long) param.value());
                    break;
                case TypeTags.STRING_TAG:
                    stmt.setString(index, (String) param.value());
                    break;
                case TypeTags.FLOAT_TAG:
                    stmt.setDouble(index, (Double) param.value());
                    break;
                case TypeTags.BOOLEAN_TAG:
                    stmt.setBoolean(index, (Boolean) param.value());
                    break;
                case TypeTags.XML_TAG:
                case TypeTags.JSON_TAG:
                    stmt.setString(index, (String) param.value());
                    break;
                case TypeTags.BLOB_TAG:
                    byte[] blobData = (byte[]) param.value();
                    stmt.setBlob(index, new ByteArrayInputStream(blobData), blobData.length);
                    break;
                case TypeTags.ARRAY_TAG:
                    Object[] arrayData = getArrayData(param);
                    stmt.setObject(index, arrayData);
                    break;
            }
        }
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw new BallerinaException("error in executing statement : " + queryStatement + " error:" + e.getMessage());
    } finally {
        releaseResources(conn, stmt);
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 15 with BRefType

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

the class GetValues method execute.

public void execute(Context ctx) {
    BMap<String, BValue> map = (BMap<String, BValue>) ctx.getRefArgument(0);
    Set<String> keySet = map.keySet();
    BRefValueArray bRefValueArray = new BRefValueArray(BTypes.typeAny);
    int i = 0;
    for (String key : keySet) {
        BValue value = map.get(key);
        bRefValueArray.add(i, ((BRefType) value));
        i++;
    }
    ctx.setReturnValues(bRefValueArray);
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Aggregations

BRefType (org.ballerinalang.model.values.BRefType)20 BString (org.ballerinalang.model.values.BString)13 BInteger (org.ballerinalang.model.values.BInteger)8 BFloat (org.ballerinalang.model.values.BFloat)7 BJSON (org.ballerinalang.model.values.BJSON)7 BBoolean (org.ballerinalang.model.values.BBoolean)6 BStruct (org.ballerinalang.model.values.BStruct)6 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 BMap (org.ballerinalang.model.values.BMap)5 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)5 BType (org.ballerinalang.model.types.BType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 BBlobArray (org.ballerinalang.model.values.BBlobArray)3 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)3 BFloatArray (org.ballerinalang.model.values.BFloatArray)3 BIntArray (org.ballerinalang.model.values.BIntArray)3 BStringArray (org.ballerinalang.model.values.BStringArray)3 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)3 TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)3