Search in sources :

Example 11 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class CPU method invokeVirtualFunction.

private static WorkerExecutionContext invokeVirtualFunction(WorkerExecutionContext ctx, int receiver, FunctionInfo virtualFuncInfo, int[] argRegs, int[] retRegs, int flags) {
    BStruct structVal = (BStruct) ctx.workerLocal.refRegs[receiver];
    if (structVal == null) {
        ctx.setError(BLangVMErrors.createNullRefException(ctx));
        handleError(ctx);
        return null;
    }
    StructInfo structInfo = structVal.getType().structInfo;
    AttachedFunctionInfo attachedFuncInfo = structInfo.funcInfoEntries.get(virtualFuncInfo.getName());
    FunctionInfo concreteFuncInfo = attachedFuncInfo.functionInfo;
    return BLangFunctions.invokeCallable(concreteFuncInfo, ctx, argRegs, retRegs, false, flags);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) AttachedFunctionInfo(org.ballerinalang.util.codegen.AttachedFunctionInfo) StructInfo(org.ballerinalang.util.codegen.StructInfo) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) AttachedFunctionInfo(org.ballerinalang.util.codegen.AttachedFunctionInfo)

Example 12 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo 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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) StructInfo(org.ballerinalang.util.codegen.StructInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 13 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method createCallFailedException.

public static BStruct createCallFailedException(WorkerExecutionContext context, Map<String, BStruct> errors) {
    PackageInfo errorPackageInfo = context.programFile.getPackageInfo(PACKAGE_RUNTIME);
    StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_CALL_FAILED_EXCEPTION);
    return generateError(context, true, errorStructInfo, MSG_CALL_FAILED, createErrorCauseArray(errors));
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 14 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method generateError.

private static BStruct generateError(CallableUnitInfo callableUnitInfo, boolean attachCallStack, Object... values) {
    ProgramFile progFile = callableUnitInfo.getPackageInfo().getProgramFile();
    PackageInfo errorPackageInfo = progFile.getPackageInfo(PACKAGE_BUILTIN);
    StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_GENERIC_ERROR);
    return generateError(callableUnitInfo, attachCallStack, errorStructInfo, values);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 15 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method getStackFrame.

public static BStruct getStackFrame(CallableUnitInfo callableUnitInfo, int ip) {
    if (callableUnitInfo == null) {
        return null;
    }
    ProgramFile progFile = callableUnitInfo.getPackageInfo().getProgramFile();
    PackageInfo runtimePackage = progFile.getPackageInfo(PACKAGE_RUNTIME);
    StructInfo callStackElement = runtimePackage.getStructInfo(STRUCT_CALL_STACK_ELEMENT);
    int currentIP = ip - 1;
    Object[] values;
    values = new Object[4];
    String parentScope = "";
    if (callableUnitInfo instanceof ResourceInfo) {
        parentScope = ((ResourceInfo) callableUnitInfo).getServiceInfo().getName() + ".";
    } else if (callableUnitInfo instanceof ActionInfo) {
        parentScope = ((ActionInfo) callableUnitInfo).getConnectorInfo().getName() + ".";
    }
    values[0] = parentScope + callableUnitInfo.getName();
    values[1] = callableUnitInfo.getPkgPath();
    if (callableUnitInfo.isNative()) {
        values[2] = "<native>";
        values[3] = 0;
    } else {
        LineNumberInfo lineNumberInfo = callableUnitInfo.getPackageInfo().getLineNumberInfo(currentIP);
        if (lineNumberInfo != null) {
            values[2] = lineNumberInfo.getFileName();
            values[3] = lineNumberInfo.getLineNumber();
        }
    }
    return BLangVMStructs.createBStruct(callStackElement, values);
}
Also used : ResourceInfo(org.ballerinalang.util.codegen.ResourceInfo) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ActionInfo(org.ballerinalang.util.codegen.ActionInfo) LineNumberInfo(org.ballerinalang.util.codegen.LineNumberInfo) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Aggregations

StructInfo (org.ballerinalang.util.codegen.StructInfo)40 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)35 BStruct (org.ballerinalang.model.values.BStruct)21 BStructType (org.ballerinalang.model.types.BStructType)6 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)3 BType (org.ballerinalang.model.types.BType)2 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)2 Test (org.testng.annotations.Test)2 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 URL (java.net.URL)1 ByteChannel (java.nio.channels.ByteChannel)1 SocketChannel (java.nio.channels.SocketChannel)1