use of org.ballerinalang.model.values.BRefType in project ballerina by ballerina-lang.
the class MethodListener method generateRequestStruct.
private BValue generateRequestStruct(Message request, ProgramFile programFile, String fieldName, BType structType) {
BValue bValue = null;
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
if (structType instanceof BStructType) {
BStruct requestStruct = BLangConnectorSPIUtil.createBStruct(programFile, structType.getPackagePath(), structType.getName());
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, programFile, structFieldName, structField.getFieldType()));
}
} 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 (fields.containsKey(fieldName)) {
String fieldType = structType.getName();
switch(fieldType) {
case STRING:
{
bValue = new BString((String) fields.get(fieldName));
break;
}
case INT:
{
bValue = new BInteger((Long) fields.get(fieldName));
break;
}
case FLOAT:
{
Float value = (Float) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
bValue = new BBoolean((Boolean) fields.get(fieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
}
}
}
}
return bValue;
}
use of org.ballerinalang.model.values.BRefType in project ballerina by ballerina-lang.
the class GetClient method execute.
@Override
public void execute(Context context) {
BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
// Service client responder is populated in method listener. There we create new service endpoint instance
// and bind client responder instance.
BRefType clientType = serviceEndpoint.getRefField(CLIENT_RESPONDER_REF_INDEX);
if (clientType instanceof BStruct) {
BStruct endpointClient = (BStruct) clientType;
context.setReturnValues(endpointClient);
} else {
context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("Error while " + "retrieving endpoint client.")));
}
}
use of org.ballerinalang.model.values.BRefType 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.BRefType 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.BRefType in project ballerina by ballerina-lang.
the class CPU method execLoadOpcodes.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void execLoadOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
int k;
// Index of the local variable
int lvIndex;
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.IMOVE:
lvIndex = operands[0];
i = operands[1];
sf.longRegs[i] = sf.longRegs[lvIndex];
break;
case InstructionCodes.FMOVE:
lvIndex = operands[0];
i = operands[1];
sf.doubleRegs[i] = sf.doubleRegs[lvIndex];
break;
case InstructionCodes.SMOVE:
lvIndex = operands[0];
i = operands[1];
sf.stringRegs[i] = sf.stringRegs[lvIndex];
break;
case InstructionCodes.BMOVE:
lvIndex = operands[0];
i = operands[1];
sf.intRegs[i] = sf.intRegs[lvIndex];
break;
case InstructionCodes.LMOVE:
lvIndex = operands[0];
i = operands[1];
sf.byteRegs[i] = sf.byteRegs[lvIndex];
break;
case InstructionCodes.RMOVE:
lvIndex = operands[0];
i = operands[1];
sf.refRegs[i] = sf.refRegs[lvIndex];
break;
case InstructionCodes.IALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bIntArray = (BIntArray) sf.refRegs[i];
if (bIntArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.longRegs[k] = bIntArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.FALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bFloatArray = (BFloatArray) sf.refRegs[i];
if (bFloatArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.doubleRegs[k] = bFloatArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.SALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bStringArray = (BStringArray) sf.refRegs[i];
if (bStringArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.stringRegs[k] = bStringArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.BALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bBooleanArray = (BBooleanArray) sf.refRegs[i];
if (bBooleanArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.intRegs[k] = bBooleanArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.LALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bBlobArray = (BBlobArray) sf.refRegs[i];
if (bBlobArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.byteRegs[k] = bBlobArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.RALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bArray = (BRefValueArray) sf.refRegs[i];
if (bArray == null) {
handleNullRefError(ctx);
break;
}
try {
sf.refRegs[k] = bArray.get(sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.JSONALOAD:
i = operands[0];
j = operands[1];
k = operands[2];
jsonVal = (BJSON) sf.refRegs[i];
if (jsonVal == null) {
handleNullRefError(ctx);
break;
}
try {
sf.refRegs[k] = JSONUtils.getArrayElement(jsonVal, sf.longRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.IGLOAD:
// Global variable index
i = operands[0];
// Stack registry index
j = operands[1];
sf.longRegs[j] = ctx.programFile.getGlobalMemoryBlock().getIntField(i);
break;
case InstructionCodes.FGLOAD:
i = operands[0];
j = operands[1];
sf.doubleRegs[j] = ctx.programFile.getGlobalMemoryBlock().getFloatField(i);
break;
case InstructionCodes.SGLOAD:
i = operands[0];
j = operands[1];
sf.stringRegs[j] = ctx.programFile.getGlobalMemoryBlock().getStringField(i);
break;
case InstructionCodes.BGLOAD:
i = operands[0];
j = operands[1];
sf.intRegs[j] = ctx.programFile.getGlobalMemoryBlock().getBooleanField(i);
break;
case InstructionCodes.LGLOAD:
i = operands[0];
j = operands[1];
sf.byteRegs[j] = ctx.programFile.getGlobalMemoryBlock().getBlobField(i);
break;
case InstructionCodes.RGLOAD:
i = operands[0];
j = operands[1];
sf.refRegs[j] = ctx.programFile.getGlobalMemoryBlock().getRefField(i);
break;
case InstructionCodes.IFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.longRegs[j] = structureType.getIntField(fieldIndex);
break;
case InstructionCodes.FFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.doubleRegs[j] = structureType.getFloatField(fieldIndex);
break;
case InstructionCodes.SFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.stringRegs[j] = structureType.getStringField(fieldIndex);
break;
case InstructionCodes.BFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.intRegs[j] = structureType.getBooleanField(fieldIndex);
break;
case InstructionCodes.LFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.byteRegs[j] = structureType.getBlobField(fieldIndex);
break;
case InstructionCodes.RFIELDLOAD:
i = operands[0];
fieldIndex = operands[1];
j = operands[2];
structureType = (StructureType) sf.refRegs[i];
if (structureType == null) {
handleNullRefError(ctx);
break;
}
sf.refRegs[j] = structureType.getRefField(fieldIndex);
break;
case InstructionCodes.MAPLOAD:
i = operands[0];
j = operands[1];
k = operands[2];
bMap = (BMap<String, BRefType>) sf.refRegs[i];
if (bMap == null) {
handleNullRefError(ctx);
break;
}
sf.refRegs[k] = bMap.get(sf.stringRegs[j]);
break;
case InstructionCodes.JSONLOAD:
i = operands[0];
j = operands[1];
k = operands[2];
jsonVal = (BJSON) sf.refRegs[i];
if (jsonVal == null) {
handleNullRefError(ctx);
break;
}
sf.refRegs[k] = JSONUtils.getElement(jsonVal, sf.stringRegs[j]);
break;
case InstructionCodes.ENUMERATORLOAD:
i = operands[0];
j = operands[1];
k = operands[2];
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[i];
BEnumType enumType = (BEnumType) typeRefCPEntry.getType();
sf.refRegs[k] = enumType.getEnumerator(j);
break;
default:
throw new UnsupportedOperationException();
}
}
Aggregations