Skip to content

Commit

Permalink
Resolve conflicts and merge branch 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gabilang committed Feb 22, 2024
2 parents b96f125 + a9b994b commit f5ab4a1
Show file tree
Hide file tree
Showing 380 changed files with 9,740 additions and 1,047 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ codecov:
max_report_age: off

fixes:
- "ballerina/lang$0046*/*/::langlib/lang.*/src/main/ballerina/"
- "ballerina/lang&0046*/*/::langlib/lang.*/src/main/ballerina/"

ignore:
- "**/tests"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ProfilerMethodWrapper extends ClassLoader {
public void invokeMethods(String debugArg) throws IOException, InterruptedException {
String balJarArgs = Main.getBalJarArgs();
List<String> commands = new ArrayList<>();
commands.add("java");
commands.add(System.getenv("java.command"));
commands.add("-jar");
if (debugArg != null) {
commands.add(debugArg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public class PredefinedTypes {
TYPE_TEXT)), EMPTY_MODULE);

public static final Type TYPE_READONLY_XML = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(TYPE_XML);
public static final Type TYPE_XML_ELEMENT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_ELEMENT, false);
public static final Type TYPE_XML_COMMENT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_COMMENT, false);
public static final Type TYPE_XML_PI_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_PROCESSING_INSTRUCTION, false);
public static final Type TYPE_XML_TEXT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_TEXT, false);

public static final AnyType TYPE_ANY = new BAnyType(TypeConstants.ANY_TNAME, EMPTY_MODULE, false);
public static final AnyType TYPE_READONLY_ANY = new BAnyType(TypeConstants.READONLY_ANY_TNAME, EMPTY_MODULE, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@
*/
public interface Type {

// TODO: remove default implementations when standard library types are updated
/**
* Set the referred type for this type once it has been calculated. This must be called the first time this
* calculation is done for {@code Type#getReferredTypeCache()} to work properly. This is non-blocking and
* will eventually become consistent. Expect {@code TypeUtils#getReferredType(Type)} to be referentially
* transparent.
*
* @param type Type referred by this type. For non-reference types, this is the same type.
*/
default void setCachedReferredType(Type type) {
}
/**
* Get the type referred by this type if it has been already calculated. If it has not been already calculated, it
* will return null. For non-reference types, this will return the same type. This is non-blocking and will
* eventually become consistent. Expect {@code TypeUtils#getReferredType(Type)} to be referentially transparent.
*
* @return Referred type of the type
*/
default Type getCachedReferredType() {
return null;
}

/**
* Set the implied type for this type once it has been calculated. This must be called the first time this
* calculation is done for {@code Type#getImpliedTypeCache()} to work properly. This is non-blocking and
* will eventually become consistent. Expect {@code TypeUtils#getImpliedType(Type)} to be referentially transparent.
*
* @param type Type implied by this type. For non-intersection types, this is the same type.
*/
default void setCachedImpliedType(Type type) {
}

/**
* Get the type implied by this type if it has been already calculated. If it has not been already calculated, it
* will return null. For non-intersection types, this will return the same type. This is non-blocking and will
* eventually become consistent. Expect {@code TypeUtils#getImpliedType(Type)} to be referentially transparent.
*
* @return Implied type of the type
*/
default Type getCachedImpliedType() {
return null;
}

/**
* Get the default value of the type. This is the value of an uninitialized variable of this type.
* For value types, this is same as the value get from {@code BType#getInitValue()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,17 @@ public static boolean isSameType(Type sourceType, Type targetType) {
* @return the referred type if provided with a type reference type, else returns the original type
*/
public static Type getReferredType(Type type) {
Type referredType = type.getCachedReferredType();
if (referredType != null) {
return referredType;
}
if (type.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) {
return getReferredType(((ReferenceType) type).getReferredType());
referredType = getReferredType(((ReferenceType) type).getReferredType());
} else {
referredType = type;
}
return type;
type.setCachedReferredType(referredType);
return referredType;
}

/**
Expand All @@ -167,12 +174,18 @@ public static Type getReferredType(Type type) {
* else returns the original type
*/
public static Type getImpliedType(Type type) {
Type impliedType = type.getCachedImpliedType();
if (impliedType != null) {
return impliedType;
}
type = getReferredType(type);

if (type.getTag() == TypeTags.INTERSECTION_TAG) {
return getImpliedType(((IntersectionType) type).getEffectiveType());
impliedType = getImpliedType(((IntersectionType) type).getEffectiveType());
} else {
impliedType = type;
}

return type;
type.setCachedImpliedType(impliedType);
return impliedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public static Object convertJSON(Object jsonValue, Type targetType) {
if (jsonValue instanceof BString) {
return jsonValue;
}
return jsonValue.toString();
return StringUtils.fromString(jsonValue.toString());
case TypeTags.BOOLEAN_TAG:
return jsonNodeToBoolean(jsonValue);
case TypeTags.JSON_TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,7 @@ public static boolean isInherentlyImmutableType(Type sourceType) {
case TypeTags.TYPEDESC_TAG:
case TypeTags.FUNCTION_POINTER_TAG:
case TypeTags.HANDLE_TAG:
case TypeTags.REG_EXP_TYPE_TAG:
return true;
case TypeTags.XML_TAG:
return ((BXmlType) sourceType).constraint.getTag() == TypeTags.NEVER_TAG;
Expand Down Expand Up @@ -2979,8 +2980,8 @@ private static boolean checkValueEquals(Object lhsValue, Object rhsValue, Set<Va
return checkDecimalEqual((DecimalValue) lhsValue, (DecimalValue) rhsValue);
case TypeTags.XML_TAG:
// Instance of xml never
if (lhsValue instanceof XmlText) {
return TypeTags.isXMLTypeTag(rhsValTypeTag) && ((XmlText) lhsValue).equals(rhsValue, checkedValues);
if (lhsValue instanceof XmlText xmlText) {
return TypeTags.isXMLTypeTag(rhsValTypeTag) && xmlText.equals(rhsValue, checkedValues);
}
return TypeTags.isXMLTypeTag(rhsValTypeTag) && ((XmlSequence) lhsValue).equals(rhsValue, checkedValues);
case TypeTags.XML_ELEMENT_TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -368,12 +369,12 @@ public static Type getConvertibleTypeInTargetUnionType(Object inputValue, BUnion

for (Type memType : memberTypes) {
if (TypeChecker.checkIsLikeType(inputValue, memType, false)) {
return getConvertibleType(inputValue, memType, varName, unresolvedValues, errors, false);
return getConvertibleType(inputValue, memType, varName, new HashSet<>(unresolvedValues), errors, false);
}
}
for (Type memType : memberTypes) {
Type convertibleTypeInUnion = getConvertibleType(inputValue, memType, varName,
unresolvedValues, errors, allowNumericConversion);
new HashSet<>(unresolvedValues), errors, allowNumericConversion);
if (convertibleTypeInUnion != null) {
return convertibleTypeInUnion;
}
Expand All @@ -391,7 +392,7 @@ private static Type getConvertibleStructuredTypeInUnion(Object inputValue, Strin
for (Type memType : memberTypes) {
initialErrorCount = errors.size();
Type convertibleTypeInUnion = getConvertibleType(inputValue, memType, varName,
unresolvedValues, errors, allowNumericConversion);
new HashSet<>(unresolvedValues), errors, allowNumericConversion);
currentErrorListSize = errors.size();
if (convertibleTypeInUnion != null) {
errors.subList(initialErrorListSize - 1, currentErrorListSize).clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,14 @@ private List<TomlTableNode> getRootModuleNode(Toml baseToml) {
Optional<Toml> table = baseToml.getTable(moduleKey);
List<TomlTableNode> moduleNodes = new ArrayList<>();
if (table.isPresent()) {
moduleNodes.add(table.get().rootNode());
addToModuleNodesList(table.get(), moduleNodes);
} else if (moduleInfo.hasModuleAmbiguity()) {
throw new ConfigException(ErrorCodes.CONFIG_TOML_MODULE_AMBIGUITY, getLineRange(baseToml.rootNode()),
moduleName, moduleKey);
}
table = baseToml.getTable(moduleName);
table.ifPresent(toml -> addToModuleNodesList(toml, moduleNodes));
moduleNodes.add(baseToml.rootNode());
addToModuleNodesList(baseToml, moduleNodes);
return moduleNodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ public void unblockStrand(Strand strand) {
}

private void cleanUp(Strand justCompleted) {
justCompleted.scheduler = null;
justCompleted.frames = null;
justCompleted.waitingContexts = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public String getName() {

@Override
public String getAnnotationKey() {
return Utils.decodeIdentifier(parentObjectType.getAnnotationKey()) + "." +
Utils.decodeIdentifier(funcName);
return Utils.decodeIdentifier(parentObjectType.getAnnotationKey()) + "." + Utils.decodeIdentifier(funcName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public abstract class BType implements Type {
protected Module pkg;
protected Class<? extends Object> valueClass;
private int hashCode;
private Type cachedReferredType = null;
private Type cachedImpliedType = null;

protected BType(String typeName, Module pkg, Class<? extends Object> valueClass) {
this.typeName = typeName;
Expand Down Expand Up @@ -195,4 +197,19 @@ public long getFlags() {
return 0;
}

public void setCachedReferredType(Type type) {
this.cachedReferredType = type;
}

public Type getCachedReferredType() {
return this.cachedReferredType;
}

public void setCachedImpliedType(Type type) {
this.cachedImpliedType = type;
}

public Type getCachedImpliedType() {
return this.cachedImpliedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.ballerina.runtime.internal.errors.ErrorHelper;
import io.ballerina.runtime.internal.types.BArrayType;
import io.ballerina.runtime.internal.types.BUnionType;
import io.ballerina.runtime.internal.types.BXmlType;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -628,17 +627,12 @@ public Object next() {
}

private Type getSequenceType(Type tempExprType) {
switch (tempExprType.getTag()) {
case TypeTags.XML_ELEMENT_TAG:
return new BXmlType(PredefinedTypes.TYPE_ELEMENT, false);
case TypeTags.XML_COMMENT_TAG:
return new BXmlType(PredefinedTypes.TYPE_COMMENT, false);
case TypeTags.XML_PI_TAG:
return new BXmlType(PredefinedTypes.TYPE_PROCESSING_INSTRUCTION, false);
default:
// Since 'xml:Text is same as xml<'xml:Text>
return PredefinedTypes.TYPE_TEXT;
}
return switch (tempExprType.getTag()) {
case TypeTags.XML_ELEMENT_TAG -> PredefinedTypes.TYPE_XML_ELEMENT_SEQUENCE;
case TypeTags.XML_COMMENT_TAG -> PredefinedTypes.TYPE_XML_COMMENT_SEQUENCE;
case TypeTags.XML_PI_TAG -> PredefinedTypes.TYPE_XML_PI_SEQUENCE;
default -> PredefinedTypes.TYPE_XML_TEXT_SEQUENCE;
};
}

private void initializeIteratorNextReturnType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package io.ballerina.cli.cmd;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.ballerina.cli.BLauncherCmd;
import io.ballerina.cli.utils.FileUtils;
import io.ballerina.projects.DependencyManifest;
Expand Down Expand Up @@ -44,14 +46,18 @@
import org.wso2.ballerinalang.util.RepoUtils;
import picocli.CommandLine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand All @@ -60,6 +66,7 @@
import static io.ballerina.cli.utils.CentralUtils.authenticate;
import static io.ballerina.cli.utils.CentralUtils.getBallerinaCentralCliTokenUrl;
import static io.ballerina.cli.utils.CentralUtils.getCentralPackageURL;
import static io.ballerina.projects.util.ProjectConstants.LOCAL_TOOLS_JSON;
import static io.ballerina.projects.util.ProjectConstants.SETTINGS_FILE_NAME;
import static io.ballerina.projects.util.ProjectUtils.getAccessTokenOfCLI;
import static io.ballerina.projects.util.ProjectUtils.initializeProxy;
Expand All @@ -73,6 +80,11 @@
@CommandLine.Command(name = PUSH_COMMAND, description = "Publish a package to Ballerina Central")
public class PushCommand implements BLauncherCmd {

private static final String TOOL_DIR = "tool";
private static final String BAL_TOOL_JSON = "bal-tool.json";
private static final String TOOL_ID = "tool_id";
private static final String ORG = "org";
private static final String PACKAGE_NAME = "name";
@CommandLine.Parameters (arity = "0..1")
private Path balaPath;

Expand Down Expand Up @@ -391,9 +403,11 @@ private void pushBalaToCustomRepo(Path balaFilePath) {
ProjectUtils.deleteDirectory(balaCachesPath);
}
ProjectUtils.extractBala(balaFilePath, balaDestPath);
createLocalToolsJsonIfLocalTool(balaDestPath, org, packageName, repoPath.resolve(
ProjectConstants.BALA_DIR_NAME));
} catch (IOException e) {
throw new ProjectException("error while pushing bala file '" + balaFilePath + "' to '"
+ ProjectConstants.LOCAL_REPOSITORY_NAME + "' repository. " + e.getMessage());
+ ProjectConstants.LOCAL_REPOSITORY_NAME + "' repository: " + e.getMessage());
}

Path relativePathToBalaFile;
Expand All @@ -406,6 +420,51 @@ private void pushBalaToCustomRepo(Path balaFilePath) {
+ " to '" + repositoryName + "' repository.");
}

private void createLocalToolsJsonIfLocalTool(Path balaDestPath, String org, String packageName,
Path localRepoBalaPath) {
Path balToolJsonPath = balaDestPath.resolve(TOOL_DIR).resolve(BAL_TOOL_JSON);
JsonObject balToolJson;
JsonObject localToolJson;
Gson gson = new Gson();
if (!balToolJsonPath.toFile().exists()) {
return;
}
try (BufferedReader bufferedReader = Files.newBufferedReader(balToolJsonPath, StandardCharsets.UTF_8)) {
balToolJson = gson.fromJson(bufferedReader, JsonObject.class);
} catch (IOException e) {
throw new ProjectException("Failed to read bal-tools.json file: " + e.getMessage());
}
Optional<String> optionalToolId = Optional.ofNullable(balToolJson.get(TOOL_ID).getAsString());
if (optionalToolId.isEmpty()) {
return;
}
String toolId = optionalToolId.get();
JsonObject packageDesc = new JsonObject();
packageDesc.addProperty(ORG, org);
packageDesc.addProperty(PACKAGE_NAME, packageName);
Path localToolJsonPath = localRepoBalaPath.resolve(LOCAL_TOOLS_JSON);
if (localToolJsonPath.toFile().exists()) {
try (BufferedReader bufferedReader = Files.newBufferedReader(localToolJsonPath, StandardCharsets.UTF_8)) {
localToolJson = gson.fromJson(bufferedReader, JsonObject.class);
if (localToolJson.has(toolId)) {
localToolJson.remove(toolId);
}
localToolJson.add(toolId, packageDesc);
} catch (IOException e) {
throw new ProjectException("Failed to read local-tools.json file: " + e.getMessage());
}
} else {
localToolJson = new JsonObject();
localToolJson.add(toolId, packageDesc);
}

try (FileWriter writer = new FileWriter(localToolJsonPath.toFile(), StandardCharsets.UTF_8)) {
writer.write(gson.toJson(localToolJson));
} catch (IOException e) {
throw new ProjectException("Failed to write local-tools.json file: " + e.getMessage());
}
}

/**
* Push a bala file to remote repository.
*
Expand Down
Loading

0 comments on commit f5ab4a1

Please sign in to comment.