Skip to content

Commit

Permalink
Merge branch master
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Nov 21, 2023
2 parents b6d8173 + 03b5fb1 commit 6496adc
Show file tree
Hide file tree
Showing 26 changed files with 563 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
import org.wso2.ballerinalang.compiler.tree.BLangPackage;
import org.wso2.ballerinalang.compiler.util.CompilerContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -92,8 +88,6 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) {
dlog.setCurrentPackageId(packageSymbol.pkgID);
final JvmPackageGen jvmPackageGen = new JvmPackageGen(symbolTable, packageCache, dlog, types);

populateExternalMap(jvmPackageGen);

//Rewrite identifier names with encoding special characters
HashMap<String, String> originalIdentifierMap = JvmDesugarPhase.encodeModuleIdentifiers(packageSymbol.bir);

Expand All @@ -104,30 +98,4 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) {
JvmDesugarPhase.replaceEncodedModuleIdentifiers(packageSymbol.bir, originalIdentifierMap);
return compiledJarFile;
}

private void populateExternalMap(JvmPackageGen jvmPackageGen) {

String nativeMap = System.getenv("BALLERINA_NATIVE_MAP");
if (nativeMap == null) {
return;
}
File mapFile = new File(nativeMap);
if (!mapFile.exists()) {
return;
}

try (BufferedReader br = new BufferedReader(new FileReader(mapFile))) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("\"")) {
int firstQuote = line.indexOf('"', 1);
String key = line.substring(1, firstQuote);
String value = line.substring(line.indexOf('"', firstQuote + 1) + 1, line.lastIndexOf('"'));
jvmPackageGen.addExternClassMapping(key, value);
}
}
} catch (IOException e) {
//ignore because this is only important in langlibs users shouldn't see this error
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public class JvmPackageGen {
private final InitMethodGen initMethodGen;
private final ConfigMethodGen configMethodGen;
private final Map<String, BIRFunctionWrapper> birFunctionMap;
private final Map<String, String> externClassMap;
private final Map<String, String> globalVarClassMap;
private final Set<PackageID> dependentModules;
private final BLangDiagnosticLog dlog;
Expand All @@ -152,7 +151,6 @@ public class JvmPackageGen {
JvmPackageGen(SymbolTable symbolTable, PackageCache packageCache, BLangDiagnosticLog dlog, Types types) {
birFunctionMap = new HashMap<>();
globalVarClassMap = new HashMap<>();
externClassMap = new HashMap<>();
dependentModules = new LinkedHashSet<>();
this.symbolTable = symbolTable;
this.packageCache = packageCache;
Expand Down Expand Up @@ -243,7 +241,7 @@ private static void generateLockForVariable(ClassWriter cw) {
private static void generateStaticInitializer(ClassWriter cw, String className, BIRPackage birPackage,
boolean isInitClass, boolean serviceEPAvailable,
AsyncDataCollector asyncDataCollector,
JvmConstantsGen jvmConstantsGen, boolean isTestablePackage) {
JvmConstantsGen jvmConstantsGen) {
if (!isInitClass && asyncDataCollector.getStrandMetadata().isEmpty()) {
return;
}
Expand Down Expand Up @@ -320,15 +318,6 @@ static String computeLockNameFromString(String varName) {
return "$lock" + varName;
}

public static String cleanupPackageName(String pkgName) {
int index = pkgName.lastIndexOf("/");
if (index > 0) {
return pkgName.substring(0, index);
} else {
return pkgName;
}
}

public static BIRFunctionWrapper getFunctionWrapper(BIRFunction currentFunc, PackageID packageID,
String moduleClass) {
BInvokableType functionTypeDesc = currentFunc.type;
Expand Down Expand Up @@ -432,7 +421,7 @@ private void generateModuleClasses(BIRPackage module, Map<String, byte[]> jarEnt
}
JvmCodeGenUtil.visitStrandMetadataFields(cw, asyncDataCollector.getStrandMetadata());
generateStaticInitializer(cw, moduleClass, module, isInitClass, serviceEPAvailable,
asyncDataCollector, jvmConstantsGen, isTestable);
asyncDataCollector, jvmConstantsGen);
cw.visitEnd();

byte[] bytes = getBytes(cw, module);
Expand Down Expand Up @@ -518,7 +507,7 @@ private void linkTypeDefinitions(BIRPackage module, boolean isEntry) {
String className = JvmValueGen.getTypeValueClassName(pkgName, typeName);
try {
BIRFunctionWrapper birFuncWrapperOrError =
getBirFunctionWrapper(isEntry, module.packageID, func, className, lookupKey);
getBirFunctionWrapper(isEntry, module.packageID, func, className);
birFunctionMap.put(pkgName + lookupKey, birFuncWrapperOrError);
} catch (JInteropException e) {
dlog.error(func.pos, e.getCode(), e.getMessage());
Expand Down Expand Up @@ -577,9 +566,7 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
count = count + 1;
// link the bir function for lookup
String birFuncName = birFunc.name.value;

String balFileName;

if (birFunc.pos == null) {
balFileName = MODULE_INIT_CLASS_NAME;
} else {
Expand All @@ -606,8 +593,7 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
}
try {
BIRFunctionWrapper birFuncWrapperOrError = getBirFunctionWrapper(isEntry, packageID, birFunc,
birModuleClassName,
birFuncName);
birModuleClassName);
birFunctionMap.put(pkgName + birFuncName, birFuncWrapperOrError);
} catch (JInteropException e) {
dlog.error(birFunc.pos, e.getCode(), e.getMessage());
Expand All @@ -616,11 +602,11 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
}

private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID packageID,
BIRFunction birFunc, String birModuleClassName, String lookupKey) {
BIRFunction birFunc, String birModuleClassName) {
BIRFunctionWrapper birFuncWrapperOrError;
if (isExternFunc(birFunc) && isEntry) {
birFuncWrapperOrError = createExternalFunctionWrapper(isEntry, birFunc, packageID,
birModuleClassName, lookupKey, this);
birModuleClassName, this);
} else {
if (isEntry && birFunc.receiver == null) {
addDefaultableBooleanVarsToSignature(birFunc, symbolTable.booleanType);
Expand All @@ -630,10 +616,6 @@ private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID pack
return birFuncWrapperOrError;
}

public String lookupExternClassName(String pkgName, String functionName) {
return externClassMap.get(pkgName + "/" + functionName);
}

public byte[] getBytes(ClassWriter cw, BIRNode node) {

byte[] result;
Expand Down Expand Up @@ -664,18 +646,13 @@ public byte[] getBytes(ClassWriter cw, BIRNode node) {
private void clearPackageGenInfo() {
birFunctionMap.clear();
globalVarClassMap.clear();
externClassMap.clear();
dependentModules.clear();
}

public BIRFunctionWrapper lookupBIRFunctionWrapper(String lookupKey) {
return this.birFunctionMap.get(lookupKey);
}

void addExternClassMapping(String key, String value) {
this.externClassMap.put(key, value);
}

BType lookupTypeDef(NewInstance objectNewIns) {

if (!objectNewIns.isExternalDef) {
Expand Down Expand Up @@ -753,7 +730,7 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) {
final Map<String, byte[]> jarEntries = new HashMap<>();

// desugar parameter initialization
injectDefaultParamInits(module, initMethodGen, this);
injectDefaultParamInits(module, initMethodGen);
injectDefaultParamInitsToAttachedFuncs(module, initMethodGen, this);

// create imported modules flat list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.wso2.ballerinalang.compiler.bir.codegen.internal.AsyncDataCollector;
import org.wso2.ballerinalang.compiler.bir.codegen.internal.FieldNameHashComparator;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.BIRFunctionWrapper;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.ExternalMethodGen;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.JFieldBIRFunction;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.JMethodBIRFunction;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.OldStyleExternalFunctionWrapper;
import org.wso2.ballerinalang.compiler.bir.codegen.methodgen.InitMethodGen;
import org.wso2.ballerinalang.compiler.bir.codegen.methodgen.LambdaGen;
import org.wso2.ballerinalang.compiler.bir.codegen.methodgen.MethodGen;
Expand Down Expand Up @@ -122,7 +118,6 @@
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.VALUE_CLASS_INIT;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.VOID_METHOD_DESC;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmTypeGen.getTypeDesc;
import static org.wso2.ballerinalang.compiler.bir.codegen.interop.ExternalMethodGen.desugarOldExternFuncs;
import static org.wso2.ballerinalang.compiler.bir.codegen.interop.InteropMethodGen.desugarInteropFuncs;

/**
Expand All @@ -132,7 +127,6 @@
*/
public class JvmValueGen {

static final FieldNameHashComparator FIELD_NAME_HASH_COMPARATOR = new FieldNameHashComparator();
static final String ENCODED_RECORD_INIT =
Utils.encodeFunctionIdentifier(Names.INIT_FUNCTION_SUFFIX.value);
private final BIRNode.BIRPackage module;
Expand Down Expand Up @@ -163,14 +157,13 @@ static void injectDefaultParamInitsToAttachedFuncs(BIRNode.BIRPackage module, In
BType bType = JvmCodeGenUtil.getImpliedType(optionalTypeDef.type);
if ((bType.tag == TypeTags.OBJECT && Symbols.isFlagOn(
bType.tsymbol.flags, Flags.CLASS)) || bType.tag == TypeTags.RECORD) {
desugarObjectMethods(module.packageID, bType, optionalTypeDef.attachedFuncs, initMethodGen,
desugarObjectMethods(optionalTypeDef.attachedFuncs, initMethodGen,
jvmPackageGen);
}
}
}

private static void desugarObjectMethods(PackageID module, BType bType,
List<BIRNode.BIRFunction> attachedFuncs, InitMethodGen initMethodGen,
private static void desugarObjectMethods(List<BIRFunction> attachedFuncs, InitMethodGen initMethodGen,
JvmPackageGen jvmPackageGen) {
if (attachedFuncs == null) {
return;
Expand All @@ -180,11 +173,7 @@ private static void desugarObjectMethods(PackageID module, BType bType,
continue;
}
if (JvmCodeGenUtil.isExternFunc(birFunc)) {
BIRFunctionWrapper extFuncWrapper = ExternalMethodGen.lookupBIRFunctionWrapper(module, birFunc, bType,
jvmPackageGen);
if (extFuncWrapper instanceof OldStyleExternalFunctionWrapper) {
desugarOldExternFuncs((OldStyleExternalFunctionWrapper) extFuncWrapper, birFunc, initMethodGen);
} else if (birFunc instanceof JMethodBIRFunction) {
if (birFunc instanceof JMethodBIRFunction) {
desugarInteropFuncs((JMethodBIRFunction) birFunc, initMethodGen);
enrichWithDefaultableParamInits(birFunc, initMethodGen);
} else if (!(birFunc instanceof JFieldBIRFunction)) {
Expand All @@ -210,7 +199,6 @@ public static String getFieldIsPresentFlagName(String fieldName) {
}

public static boolean isOptionalRecordField(BField field) {

return (field.symbol.flags & BAL_OPTIONAL) == BAL_OPTIONAL;
}

Expand Down Expand Up @@ -283,7 +271,6 @@ private void createInstantiateMethod(ClassWriter cw, BRecordType recordType,
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(SWAP);


// Invoke the init-functions of referenced types. This is done to initialize the
// defualt values of the fields coming from the referenced types.
for (BType bType : typeDef.referencedTypes) {
Expand All @@ -296,7 +283,6 @@ private void createInstantiateMethod(ClassWriter cw, BRecordType recordType,
}
}


mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, TYPEDESC_VALUE_IMPL, TYPEDESC_VALUE_IMPL_CLOSURES,
GET_MAP_ARRAY);
Expand All @@ -309,14 +295,13 @@ private void createInstantiateMethod(ClassWriter cw, BRecordType recordType,
}
mv.visitInsn(POP);


// Invoke the init-function of this type.
String initFuncName;
String valueClassName;
List<BIRFunction> attachedFuncs = typeDef.attachedFuncs;

// Attached functions are empty for type-labeling. In such cases, call the init() of
// the original type value;
// the original type value
if (!attachedFuncs.isEmpty()) {
initFuncName = attachedFuncs.get(0).name.value;
valueClassName = className;
Expand Down Expand Up @@ -346,7 +331,6 @@ public static String getTypeValueClassName(PackageID packageID, String typeName)
return getTypeValueClassName(JvmCodeGenUtil.getPackageName(packageID), typeName);
}


private StringBuilder calcClosureMapSignature(int size) {
StringBuilder closureParamSignature = new StringBuilder();
for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -403,7 +387,6 @@ private byte[] createRecordValueClass(BRecordType recordType, String className,
private void createRecordMethods(ClassWriter cw, List<BIRNode.BIRFunction> attachedFuncs, String moduleClassName,
JvmTypeGen jvmTypeGen, JvmCastGen jvmCastGen,
JvmConstantsGen jvmConstantsGen, AsyncDataCollector asyncDataCollector) {

for (BIRNode.BIRFunction func : attachedFuncs) {
if (func == null) {
continue;
Expand Down

This file was deleted.

Loading

0 comments on commit 6496adc

Please sign in to comment.