use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.
the class BTestRunner method extractArguments.
/**
* Extract function arguments from the values sets.
* @param valueSets user provided value sets
* @return a list of function arguments
*/
private List<BValue[]> extractArguments(BValue[] valueSets) {
List<BValue[]> argsList = new ArrayList<>();
for (BValue value : valueSets) {
if (value instanceof BRefValueArray) {
BRefValueArray array = (BRefValueArray) value;
for (BIterator it = array.newIterator(); it.hasNext(); ) {
BValue[] vals = it.getNext(0);
if (vals[1] instanceof BNewArray) {
BNewArray bNewArray = (BNewArray) vals[1];
BValue[] args = new BValue[(int) bNewArray.size()];
for (int j = 0; j < bNewArray.size(); j++) {
args[j] = bNewArray.getBValue(j);
}
argsList.add(args);
} else {
// cannot happen due to validations done at annotation processor
}
}
} else if (value instanceof BJSON) {
BJSON jsonArrayOfArrays = (BJSON) value;
for (BIterator it = jsonArrayOfArrays.newIterator(); it.hasNext(); ) {
BValue[] vals = it.getNext(0);
if (vals[1] instanceof BJSON) {
List<BValue> args = new ArrayList<>();
BJSON jsonArray = (BJSON) vals[1];
for (BIterator it2 = jsonArray.newIterator(); it2.hasNext(); ) {
BValue[] vals2 = it2.getNext(0);
args.add(vals2[1]);
}
argsList.add(args.toArray(new BValue[0]));
} else {
// cannot happen due to validations done at annotation processor
}
}
} else {
argsList.add(new BValue[] { value });
}
}
return argsList;
}
use of org.ballerinalang.model.values.BIterator 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;
}
}
use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.
the class StructImpl method getArrayField.
@Override
public Value[] getArrayField(String fieldName) {
final BNewArray refField = (BNewArray) value.getRefField(getFieldIndex(fieldName));
if (refField == null) {
return null;
}
List<Value> list = new ArrayList<>();
final BIterator bIterator = refField.newIterator();
while (bIterator.hasNext()) {
list.add(ValueImpl.createValue(bIterator.getNext(1)[0]));
}
return list.toArray(new Value[0]);
}
use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.
the class StructImpl method getMapField.
@Override
public Map<String, Value> getMapField(String fieldName) {
final BMap refField = (BMap) value.getRefField(getFieldIndex(fieldName));
if (refField == null) {
return null;
}
Map<String, Value> valueMap = new LinkedHashMap<>();
final BIterator bIterator = refField.newIterator();
while (bIterator.hasNext()) {
final BValue[] next = bIterator.getNext(2);
valueMap.put(next[0].stringValue(), ValueImpl.createValue(next[1]));
}
return valueMap;
}
Aggregations