use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.
the class VectorTest method testIsEmpty.
@Test(description = "Test case for testing isEmpty() function")
public void testIsEmpty() {
final int booleanTrue = 1;
int vectorSize = 10;
boolean[] expectedVals = new boolean[] { false, true, true };
BValue[] returns = BRunUtil.invoke(compileResult, "testIsEmpty", new BValue[] { new BInteger(vectorSize) });
Assert.assertNotNull(returns);
BBooleanArray isEmptyVals = (BBooleanArray) returns[0];
for (int i = 0; i < expectedVals.length; i++) {
Assert.assertEquals(isEmptyVals.get(i) == booleanTrue, expectedVals[i]);
}
}
use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.
the class CPU method calculateLength.
@SuppressWarnings("rawtypes")
private static void calculateLength(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int cpIndex = operands[1];
int j = operands[2];
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
int typeTag = typeRefCPEntry.getType().getTag();
if (typeTag == TypeTags.STRING_TAG) {
String value = sf.stringRegs[i];
if (value == null) {
handleNullRefError(ctx);
} else {
sf.longRegs[j] = value.length();
}
return;
} else if (typeTag == TypeTags.BLOB_TAG) {
// Here it is assumed null is not supported for blob type
sf.longRegs[j] = sf.byteRegs[i].length;
return;
}
BValue entity = sf.refRegs[i];
if (entity == null) {
handleNullRefError(ctx);
return;
}
if (typeTag == TypeTags.XML_TAG) {
sf.longRegs[j] = ((BXML) entity).length();
return;
} else if (entity instanceof BJSON) {
if (JSONUtils.isJSONArray((BJSON) entity)) {
sf.longRegs[j] = JSONUtils.getJSONArrayLength((BJSON) sf.refRegs[i]);
} else {
sf.longRegs[j] = -1;
}
return;
} else if (typeTag == TypeTags.MAP_TAG) {
sf.longRegs[j] = ((BMap) entity).size();
return;
}
BNewArray newArray = (BNewArray) entity;
sf.longRegs[j] = newArray.size();
return;
}
use of org.ballerinalang.model.values.BValue 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;
}
use of org.ballerinalang.model.values.BValue 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;
}
use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.
the class CPU method execIteratorOperation.
private static void execIteratorOperation(WorkerExecutionContext ctx, WorkerData sf, Instruction instruction) {
int i, j;
BCollection collection;
BIterator iterator;
InstructionIteratorNext nextInstruction;
switch(instruction.getOpcode()) {
case InstructionCodes.ITR_NEW:
// collection
i = instruction.getOperands()[0];
// iterator variable (ref) index.
j = instruction.getOperands()[1];
collection = (BCollection) sf.refRegs[i];
if (collection == null) {
handleNullRefError(ctx);
return;
}
sf.refRegs[j] = collection.newIterator();
break;
case InstructionCodes.ITR_HAS_NEXT:
// iterator
i = instruction.getOperands()[0];
// boolean variable index to store has next result
j = instruction.getOperands()[1];
iterator = (BIterator) sf.refRegs[i];
if (iterator == null) {
sf.intRegs[j] = 0;
return;
}
sf.intRegs[j] = iterator.hasNext() ? 1 : 0;
break;
case InstructionCodes.ITR_NEXT:
nextInstruction = (InstructionIteratorNext) instruction;
iterator = (BIterator) sf.refRegs[nextInstruction.iteratorIndex];
if (iterator == null) {
return;
}
BValue[] values = iterator.getNext(nextInstruction.arity);
copyValuesToRegistries(nextInstruction.typeTags, nextInstruction.retRegs, values, sf);
break;
}
}
Aggregations