Search in sources :

Example 11 with UTF8CPEntry

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

the class ProgramFileReader method getLocalVariableInfo.

private LocalVariableInfo getLocalVariableInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
    int varNameCPIndex = dataInStream.readInt();
    UTF8CPEntry varNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(varNameCPIndex);
    int variableIndex = dataInStream.readInt();
    int typeSigCPIndex = dataInStream.readInt();
    UTF8CPEntry typeSigCPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeSigCPIndex);
    BType type = getBTypeFromDescriptor(typeSigCPEntry.getValue());
    LocalVariableInfo localVariableInfo = new LocalVariableInfo(varNameCPEntry.getValue(), varNameCPIndex, variableIndex, typeSigCPIndex, type);
    int attchmntIndexesLength = dataInStream.readShort();
    int[] attachmentIndexes = new int[attchmntIndexesLength];
    for (int i = 0; i < attchmntIndexesLength; i++) {
        attachmentIndexes[i] = dataInStream.readInt();
    }
    localVariableInfo.setAttachmentIndexes(attachmentIndexes);
    return localVariableInfo;
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BType(org.ballerinalang.model.types.BType)

Example 12 with UTF8CPEntry

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

the class ProgramFileReader method readEnumInfoEntries.

private void readEnumInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int enumCount = dataInStream.readShort();
    for (int i = 0; i < enumCount; i++) {
        // Create enum info entry
        int enumNameCPIndex = dataInStream.readInt();
        int flags = dataInStream.readInt();
        UTF8CPEntry enumNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumNameCPIndex);
        String enumName = enumNameUTF8Entry.getValue();
        EnumInfo enumInfo = new EnumInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), enumNameCPIndex, enumName, flags);
        packageInfo.addEnumInfo(enumName, enumInfo);
        // Set enum type
        BEnumType enumType = new BEnumType(enumName, packageInfo.getPkgPath());
        enumInfo.setType(enumType);
        int enumeratorCount = dataInStream.readShort();
        BEnumerator[] enumerators = new BEnumerator[enumeratorCount];
        for (int j = 0; j < enumeratorCount; j++) {
            int enumeratorNameCPIndex = dataInStream.readInt();
            UTF8CPEntry enumeratorNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumeratorNameCPIndex);
            String enumeratorName = enumeratorNameUTF8Entry.getValue();
            BEnumerator enumerator = new BEnumerator(enumeratorName, enumType);
            enumerators[j] = enumerator;
        }
        enumType.setEnumerators(enumerators);
        // Read attributes of the struct info
        readAttributeInfoEntries(dataInStream, packageInfo, enumInfo);
    }
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BEnumerator(org.ballerinalang.model.values.BEnumerator) BEnumType(org.ballerinalang.model.types.BEnumType)

Example 13 with UTF8CPEntry

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

the class ProgramFileReader method readCPEntry.

private ConstantPoolEntry readCPEntry(DataInputStream dataInStream, ConstantPool constantPool, ConstantPoolEntry.EntryType cpEntryType) throws IOException {
    int cpIndex;
    int pkgCPIndex;
    UTF8CPEntry utf8CPEntry;
    PackageRefCPEntry packageRefCPEntry;
    Optional<PackageInfo> packageInfoOptional;
    switch(cpEntryType) {
        case CP_ENTRY_UTF8:
            short length = dataInStream.readShort();
            String strValue = null;
            // Therefore we read the UTF value only if the length >= 0.
            if (length >= 0) {
                strValue = dataInStream.readUTF();
            }
            return new UTF8CPEntry(strValue);
        case CP_ENTRY_INTEGER:
            long longVal = dataInStream.readLong();
            return new IntegerCPEntry(longVal);
        case CP_ENTRY_FLOAT:
            double doubleVal = dataInStream.readDouble();
            return new FloatCPEntry(doubleVal);
        case CP_ENTRY_STRING:
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            return new StringCPEntry(cpIndex, utf8CPEntry.getValue());
        case CP_ENTRY_PACKAGE:
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            return new PackageRefCPEntry(cpIndex, utf8CPEntry.getValue());
        case CP_ENTRY_FUNCTION_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String funcName = utf8CPEntry.getValue();
            FunctionRefCPEntry functionRefCPEntry = new FunctionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, funcName);
            // Find the functionInfo
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<FunctionInfo> funcInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getFunctionInfo(funcName));
            if (!funcInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(functionRefCPEntry);
                return functionRefCPEntry;
            }
            functionRefCPEntry.setFunctionInfo(funcInfoOptional.get());
            return functionRefCPEntry;
        case CP_ENTRY_TRANSFORMER_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String transformerName = utf8CPEntry.getValue();
            TransformerRefCPEntry transformerRefCPEntry = new TransformerRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, transformerName);
            // Find the transformerInfo
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<TransformerInfo> transInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getTransformerInfo(transformerName));
            if (!transInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(transformerRefCPEntry);
                return transformerRefCPEntry;
            }
            transformerRefCPEntry.setTransformerInfo(transInfoOptional.get());
            return transformerRefCPEntry;
        case CP_ENTRY_ACTION_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            UTF8CPEntry nameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String actionName = nameCPEntry.getValue();
            return new ActionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, actionName);
        case CP_ENTRY_STRUCTURE_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, utf8CPEntry.getValue());
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<StructureTypeInfo> structInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getStructureTypeInfo(utf8CPEntry.getValue()));
            if (!structInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(structureRefCPEntry);
                return structureRefCPEntry;
            }
            structureRefCPEntry.setStructureTypeInfo(structInfoOptional.get());
            return structureRefCPEntry;
        case CP_ENTRY_TYPE_REF:
            int typeSigCPIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeSigCPIndex);
            TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex, utf8CPEntry.getValue());
            unresolvedCPEntries.add(typeRefCPEntry);
            return typeRefCPEntry;
        case CP_ENTRY_FORK_JOIN:
            int forkJoinCPIndex = dataInStream.readInt();
            return new ForkJoinCPEntry(forkJoinCPIndex);
        case CP_ENTRY_WRKR_DATA_CHNL_REF:
            int uniqueNameCPIndex = dataInStream.readInt();
            UTF8CPEntry wrkrDtChnlTypesSigCPEntry = (UTF8CPEntry) constantPool.getCPEntry(uniqueNameCPIndex);
            return new WorkerDataChannelRefCPEntry(uniqueNameCPIndex, wrkrDtChnlTypesSigCPEntry.getValue());
        default:
            throw new ProgramFileFormatException("invalid constant pool entry " + cpEntryType.getValue());
    }
}
Also used : StringCPEntry(org.ballerinalang.util.codegen.cpentries.StringCPEntry) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) ForkJoinCPEntry(org.ballerinalang.util.codegen.cpentries.ForkJoinCPEntry) PackageRefCPEntry(org.ballerinalang.util.codegen.cpentries.PackageRefCPEntry) TransformerRefCPEntry(org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) ActionRefCPEntry(org.ballerinalang.util.codegen.cpentries.ActionRefCPEntry) FloatCPEntry(org.ballerinalang.util.codegen.cpentries.FloatCPEntry) WorkerDataChannelRefCPEntry(org.ballerinalang.util.codegen.cpentries.WorkerDataChannelRefCPEntry) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry) IntegerCPEntry(org.ballerinalang.util.codegen.cpentries.IntegerCPEntry)

Example 14 with UTF8CPEntry

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

the class ProgramFileReader method readResourceInfoEntries.

private void readResourceInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    for (ServiceInfo serviceInfo : packageInfo.getServiceInfoEntries()) {
        int actionCount = dataInStream.readShort();
        for (int j = 0; j < actionCount; j++) {
            // Read action name;
            int resNameCPIndex = dataInStream.readInt();
            UTF8CPEntry resNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(resNameCPIndex);
            String resName = resNameUTF8Entry.getValue();
            ResourceInfo resourceInfo = new ResourceInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), resNameCPIndex, resName);
            resourceInfo.setServiceInfo(serviceInfo);
            resourceInfo.setPackageInfo(packageInfo);
            serviceInfo.addResourceInfo(resName, resourceInfo);
            // Read action signature
            int resSigCPIndex = dataInStream.readInt();
            resourceInfo.setSignatureCPIndex(resSigCPIndex);
            UTF8CPEntry resSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(resSigCPIndex);
            String resSig = resSigUTF8Entry.getValue();
            resourceInfo.setSignature(resSig);
            setCallableUnitSignature(resourceInfo, resSig, packageInfo);
            // Read parameter names
            // TODO Find a better alternative. Storing just param names is like a hack.
            int paramNameCPIndexesCount = dataInStream.readShort();
            int[] paramNameCPIndexes = new int[paramNameCPIndexesCount];
            String[] paramNames = new String[paramNameCPIndexesCount];
            for (int k = 0; k < paramNameCPIndexesCount; k++) {
                int paramNameCPIndex = dataInStream.readInt();
                paramNameCPIndexes[k] = paramNameCPIndex;
                UTF8CPEntry paramNameCPEntry = (UTF8CPEntry) packageInfo.getCPEntry(paramNameCPIndex);
                paramNames[k] = paramNameCPEntry.getValue();
            }
            resourceInfo.setParamNameCPIndexes(paramNameCPIndexes);
            resourceInfo.setParamNames(paramNames);
            int workerDataChannelsLength = dataInStream.readShort();
            for (int k = 0; k < workerDataChannelsLength; k++) {
                readWorkerDataChannelEntries(dataInStream, packageInfo, resourceInfo);
            }
            // Read workers
            readWorkerInfoEntries(dataInStream, packageInfo, resourceInfo);
            // Read attributes of the struct info
            readAttributeInfoEntries(dataInStream, packageInfo, resourceInfo);
        }
        // Read attributes of the struct info
        readAttributeInfoEntries(dataInStream, packageInfo, serviceInfo);
    }
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)

Example 15 with UTF8CPEntry

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

the class ProgramFileReader method readFunctionInfo.

private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int funcNameCPIndex = dataInStream.readInt();
    UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
    String funcName = funcNameUTF8Entry.getValue();
    FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
    functionInfo.setPackageInfo(packageInfo);
    int funcSigCPIndex = dataInStream.readInt();
    UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
    setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
    int flags = dataInStream.readInt();
    boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
    functionInfo.setNative(nativeFunc);
    String uniqueFuncName;
    boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
    if (attached) {
        int attachedToTypeCPIndex = dataInStream.readInt();
        functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
        functionInfo.attachedToType = typeRefCPEntry.getType();
        uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
        // Update the attachedFunctionInfo
        if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
            BStructType structType = (BStructType) typeRefCPEntry.getType();
            AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
            attachedFuncInfo.functionInfo = functionInfo;
        }
    } else {
        uniqueFuncName = funcName;
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
    }
    int workerDataChannelsLength = dataInStream.readShort();
    for (int i = 0; i < workerDataChannelsLength; i++) {
        readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
    }
    // Read worker info entries
    readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
    if (nativeFunc) {
        NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
        if (nativeFunction == null) {
            throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
        }
        functionInfo.setNativeCallableUnit(nativeFunction);
    }
    // Read attributes
    readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

Aggregations

UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)19 WorkerDataChannelRefCPEntry (org.ballerinalang.util.codegen.cpentries.WorkerDataChannelRefCPEntry)4 ProgramFileFormatException (org.ballerinalang.util.exceptions.ProgramFileFormatException)4 ForkJoinCPEntry (org.ballerinalang.util.codegen.cpentries.ForkJoinCPEntry)3 TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)3 NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)2 BStructType (org.ballerinalang.model.types.BStructType)2 BType (org.ballerinalang.model.types.BType)2 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)2 ActionRefCPEntry (org.ballerinalang.util.codegen.cpentries.ActionRefCPEntry)2 FloatCPEntry (org.ballerinalang.util.codegen.cpentries.FloatCPEntry)2 FunctionRefCPEntry (org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry)2 IntegerCPEntry (org.ballerinalang.util.codegen.cpentries.IntegerCPEntry)2 StructureRefCPEntry (org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)2 TransformerRefCPEntry (org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 BConnectorType (org.ballerinalang.model.types.BConnectorType)1 BEnumType (org.ballerinalang.model.types.BEnumType)1