use of org.ballerinalang.model.types.BMapType in project ballerina by ballerina-lang.
the class CPU method checkMapCast.
private static boolean checkMapCast(BType sourceType, BType targetType) {
BMapType sourceMapType = (BMapType) sourceType;
BMapType targetMapType = (BMapType) targetType;
if (sourceMapType.equals(targetMapType)) {
return true;
}
if (sourceMapType.getConstrainedType().getTag() == TypeTags.STRUCT_TAG && targetMapType.getConstrainedType().getTag() == TypeTags.STRUCT_TAG) {
return checkStructEquivalency((BStructType) sourceMapType.getConstrainedType(), (BStructType) targetMapType.getConstrainedType());
}
return false;
}
use of org.ballerinalang.model.types.BMapType 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();
}
}
use of org.ballerinalang.model.types.BMapType 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));
}
use of org.ballerinalang.model.types.BMapType in project ballerina by ballerina-lang.
the class ProgramFileReader method createBTypeFromSig.
private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
int nameIndex;
char ch = chars[index];
switch(ch) {
case 'I':
typeStack.push(BTypes.typeInt);
return index + 1;
case 'F':
typeStack.push(BTypes.typeFloat);
return index + 1;
case 'S':
typeStack.push(BTypes.typeString);
return index + 1;
case 'B':
typeStack.push(BTypes.typeBoolean);
return index + 1;
case 'L':
typeStack.push(BTypes.typeBlob);
return index + 1;
case 'Y':
typeStack.push(BTypes.typeDesc);
return index + 1;
case 'A':
typeStack.push(BTypes.typeAny);
return index + 1;
case 'R':
// TODO Improve this logic
index++;
nameIndex = index;
while (chars[nameIndex] != ';') {
nameIndex++;
}
String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
typeStack.push(BTypes.getTypeFromName(typeName));
return nameIndex + 1;
case 'C':
case 'J':
case 'T':
case 'E':
case 'D':
case 'H':
case 'Z':
char typeChar = chars[index];
// TODO Improve this logic
index++;
nameIndex = index;
int colonIndex = -1;
while (chars[nameIndex] != ';') {
if (chars[nameIndex] == ':') {
colonIndex = nameIndex;
}
nameIndex++;
}
String pkgPath;
String name;
PackageInfo packageInfoOfType;
if (colonIndex != -1) {
pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
packageInfoOfType = programFile.getPackageInfo(pkgPath);
} else {
name = new String(Arrays.copyOfRange(chars, index, nameIndex));
// Setting the current package;
packageInfoOfType = packageInfo;
}
if (typeChar == 'C') {
typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
} else if (typeChar == 'J') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeJSON);
} else {
typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'D') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeTable);
} else {
typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'H') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeStream);
} else {
typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'E') {
typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
} else {
// This is a struct type
typeStack.push(packageInfoOfType.getStructInfo(name).getType());
}
return nameIndex + 1;
case '[':
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
BType elemType = typeStack.pop();
BArrayType arrayType = new BArrayType(elemType);
typeStack.push(arrayType);
return index;
case 'M':
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
BType constrainedType = typeStack.pop();
BType mapType;
if (constrainedType == BTypes.typeAny) {
mapType = BTypes.typeMap;
} else {
mapType = new BMapType(constrainedType);
}
typeStack.push(mapType);
return index;
case 'U':
// TODO : Fix this for type casting.
typeStack.push(new BFunctionType());
return index + 1;
case 'O':
case 'P':
typeChar = chars[index];
index++;
nameIndex = index;
while (chars[nameIndex] != ';') {
nameIndex++;
}
List<BType> memberTypes = new ArrayList<>();
int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
index = nameIndex;
for (int i = 0; i < memberCount; i++) {
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
memberTypes.add(typeStack.pop());
}
if (typeChar == 'O') {
typeStack.push(new BUnionType(memberTypes));
} else if (typeChar == 'P') {
typeStack.push(new BTupleType(memberTypes));
}
return index + 1;
case 'N':
typeStack.push(BTypes.typeNull);
return index + 1;
default:
throw new ProgramFileFormatException("unsupported base type char: " + ch);
}
}
use of org.ballerinalang.model.types.BMapType in project ballerina by ballerina-lang.
the class JSONUtils method jsonNodeToBArray.
/**
* Convert a JSON node to an array.
*
* @param arrayNode JSON to convert
* @param targetArrayType Type of the target array
* @return If the provided JSON is of array type, this method will return a {@link BArrayType} containing the values
* of the JSON array. Otherwise the method will throw a {@link BallerinaException}.
*/
@SuppressWarnings("rawtypes")
private static BNewArray jsonNodeToBArray(JsonNode arrayNode, BArrayType targetArrayType) {
if (!arrayNode.isArray()) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.ARRAY), getTypeName(arrayNode));
}
BType elementType = targetArrayType.getElementType();
BRefValueArray refValueArray;
switch(elementType.getTag()) {
case TypeTags.INT_TAG:
return jsonNodeToBIntArray(arrayNode);
case TypeTags.FLOAT_TAG:
return jsonNodeToBFloatArray(arrayNode);
case TypeTags.STRING_TAG:
return jsonNodeToBStringArray(arrayNode);
case TypeTags.BOOLEAN_TAG:
return jsonNodeToBBooleanArray(arrayNode);
case TypeTags.ANY_TAG:
refValueArray = new BRefValueArray(elementType);
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode element = arrayNode.get(i);
refValueArray.add(i, (BRefType) getBValue(element));
}
return refValueArray;
default:
refValueArray = new BRefValueArray(elementType);
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode element = arrayNode.get(i);
if (elementType.getTag() == TypeTags.MAP_TAG) {
refValueArray.add(i, jsonNodeToBMap(element, (BMapType) elementType));
} else if (elementType instanceof BStructType) {
refValueArray.add(i, convertJSONNodeToStruct(element, (BStructType) elementType));
} else if (elementType instanceof BArrayType) {
refValueArray.add(i, jsonNodeToBArray(element, (BArrayType) elementType));
} else {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, elementType, getTypeName(element));
}
}
return refValueArray;
}
}
Aggregations