use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class CPU method extractValues.
@SuppressWarnings("rawtypes")
private static BRefType[] extractValues(WorkerData data, BType[] types, int[] regs) {
BRefType[] result = new BRefType[types.length];
for (int i = 0; i < regs.length; i++) {
BType paramType = types[i];
int argReg = regs[i];
switch(paramType.getTag()) {
case TypeTags.INT_TAG:
result[i] = new BInteger(data.longRegs[argReg]);
break;
case TypeTags.FLOAT_TAG:
result[i] = new BFloat(data.doubleRegs[argReg]);
break;
case TypeTags.STRING_TAG:
result[i] = new BString(data.stringRegs[argReg]);
break;
case TypeTags.BOOLEAN_TAG:
result[i] = new BBoolean(data.intRegs[argReg] > 0);
break;
case TypeTags.BLOB_TAG:
result[i] = new BBlob(data.byteRegs[argReg]);
break;
default:
result[i] = data.refRegs[argReg];
}
}
return result;
}
use of org.ballerinalang.model.values.BInteger 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.BInteger in project ballerina by ballerina-lang.
the class CPU method execTypeConversionOpcodes.
@SuppressWarnings("rawtypes")
private static void execTypeConversionOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
int k;
BRefType bRefType;
String str;
switch(opcode) {
case InstructionCodes.I2F:
i = operands[0];
j = operands[1];
sf.doubleRegs[j] = (double) sf.longRegs[i];
break;
case InstructionCodes.I2S:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = Long.toString(sf.longRegs[i]);
break;
case InstructionCodes.I2B:
i = operands[0];
j = operands[1];
sf.intRegs[j] = sf.longRegs[i] != 0 ? 1 : 0;
break;
case InstructionCodes.I2JSON:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BJSON(Long.toString(sf.longRegs[i]));
break;
case InstructionCodes.F2I:
i = operands[0];
j = operands[1];
sf.longRegs[j] = (long) sf.doubleRegs[i];
break;
case InstructionCodes.F2S:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = Double.toString(sf.doubleRegs[i]);
break;
case InstructionCodes.F2B:
i = operands[0];
j = operands[1];
sf.intRegs[j] = sf.doubleRegs[i] != 0.0 ? 1 : 0;
break;
case InstructionCodes.F2JSON:
i = operands[0];
j = operands[1];
sf.refRegs[j] = new BJSON(Double.toString(sf.doubleRegs[i]));
break;
case InstructionCodes.S2I:
i = operands[0];
j = operands[1];
str = sf.stringRegs[i];
if (str == null) {
handleTypeConversionError(ctx, sf, j, null, TypeConstants.INT_TNAME);
break;
}
try {
sf.refRegs[j] = new BInteger(Long.parseLong(str));
} catch (NumberFormatException e) {
handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.INT_TNAME);
}
break;
case InstructionCodes.S2F:
i = operands[0];
j = operands[1];
str = sf.stringRegs[i];
if (str == null) {
handleTypeConversionError(ctx, sf, j, null, TypeConstants.FLOAT_TNAME);
break;
}
try {
sf.refRegs[j] = new BFloat(Double.parseDouble(str));
} catch (NumberFormatException e) {
handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.FLOAT_TNAME);
}
break;
case InstructionCodes.S2B:
i = operands[0];
j = operands[1];
sf.intRegs[j] = Boolean.parseBoolean(sf.stringRegs[i]) ? 1 : 0;
break;
case InstructionCodes.S2JSON:
i = operands[0];
j = operands[1];
str = StringEscapeUtils.escapeJson(sf.stringRegs[i]);
sf.refRegs[j] = str == null ? null : new BJSON("\"" + str + "\"");
break;
case InstructionCodes.B2I:
i = operands[0];
j = operands[1];
sf.longRegs[j] = sf.intRegs[i];
break;
case InstructionCodes.B2F:
i = operands[0];
j = operands[1];
sf.doubleRegs[j] = sf.intRegs[i];
break;
case InstructionCodes.B2S:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = sf.intRegs[i] == 1 ? "true" : "false";
break;
case InstructionCodes.DT2XML:
i = operands[0];
j = operands[1];
bRefType = sf.refRegs[i];
if (bRefType == null) {
handleNullRefError(ctx);
break;
}
try {
sf.refRegs[j] = XMLUtils.tableToXML((BTable) bRefType, ctx.isInTransaction());
} catch (Exception e) {
sf.refRegs[j] = null;
handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
}
break;
case InstructionCodes.DT2JSON:
i = operands[0];
j = operands[1];
bRefType = sf.refRegs[i];
if (bRefType == null) {
handleNullRefError(ctx);
break;
}
try {
sf.refRegs[j] = JSONUtils.toJSON((BTable) bRefType, ctx.isInTransaction());
} catch (Exception e) {
handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
}
break;
case InstructionCodes.T2MAP:
convertStructToMap(ctx, operands, sf);
break;
case InstructionCodes.T2JSON:
convertStructToJSON(ctx, operands, sf);
break;
case InstructionCodes.MAP2T:
convertMapToStruct(ctx, operands, sf);
break;
case InstructionCodes.JSON2T:
convertJSONToStruct(ctx, operands, sf);
break;
case InstructionCodes.XMLATTRS2MAP:
i = operands[0];
j = operands[1];
bRefType = sf.refRegs[i];
if (bRefType == null) {
sf.refRegs[j] = null;
break;
}
sf.refRegs[j] = ((BXMLAttributes) sf.refRegs[i]).value();
break;
case InstructionCodes.S2XML:
i = operands[0];
j = operands[1];
k = operands[2];
str = sf.stringRegs[i];
if (str == null) {
sf.refRegs[j] = null;
sf.refRegs[k] = null;
break;
}
try {
sf.refRegs[j] = XMLUtils.parse(str);
sf.refRegs[k] = null;
} catch (BallerinaException e) {
sf.refRegs[j] = null;
handleTypeConversionError(ctx, sf, k, e.getMessage());
}
break;
case InstructionCodes.S2JSONX:
i = operands[0];
j = operands[1];
k = operands[2];
str = sf.stringRegs[i];
try {
sf.refRegs[j] = str == null ? null : new BJSON(str);
sf.refRegs[k] = null;
} catch (BallerinaException e) {
sf.refRegs[j] = null;
handleTypeConversionError(ctx, sf, k, e.getMessage());
}
break;
case InstructionCodes.XML2S:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = sf.refRegs[i].stringValue();
break;
case InstructionCodes.ANY2SCONV:
i = operands[0];
j = operands[1];
bRefType = sf.refRegs[i];
if (bRefType == null) {
sf.stringRegs[j] = STRING_NULL_VALUE;
} else {
sf.stringRegs[j] = bRefType.stringValue();
}
break;
default:
throw new UnsupportedOperationException();
}
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class BLangVMUtils method populateReturnData.
public static BValue[] populateReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, int[] retRegs) {
WorkerData data = ctx.workerLocal;
BType[] retTypes = callableUnitInfo.getRetParamTypes();
BValue[] returnValues = new BValue[retTypes.length];
for (int i = 0; i < returnValues.length; i++) {
BType retType = retTypes[i];
switch(retType.getTag()) {
case TypeTags.INT_TAG:
returnValues[i] = new BInteger(data.longRegs[retRegs[i]]);
break;
case TypeTags.FLOAT_TAG:
returnValues[i] = new BFloat(data.doubleRegs[retRegs[i]]);
break;
case TypeTags.STRING_TAG:
returnValues[i] = new BString(data.stringRegs[retRegs[i]]);
break;
case TypeTags.BOOLEAN_TAG:
boolean boolValue = data.intRegs[retRegs[i]] == 1;
returnValues[i] = new BBoolean(boolValue);
break;
case TypeTags.BLOB_TAG:
returnValues[i] = new BBlob(data.byteRegs[retRegs[i]]);
break;
default:
returnValues[i] = data.refRegs[retRegs[i]];
break;
}
}
return returnValues;
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class GetCreationTime method execute.
@Override
public void execute(Context context) {
try {
BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
if (session != null && session.isValid()) {
context.setReturnValues(new BInteger(session.getCreationTime()));
} else {
throw new IllegalStateException("Failed to get creation time: No such session in progress");
}
} catch (IllegalStateException e) {
throw new BallerinaException(e.getMessage(), e);
}
}
Aggregations