Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjana committed May 18, 2023
2 parents c6ab658 + f978fdb commit 3b4552f
Show file tree
Hide file tree
Showing 237 changed files with 6,219 additions and 970 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static String encodeIdentifier(String string) {
* @return Converted string.
*/
public static String getExpressionStringValue(Object object) {
return io.ballerina.runtime.api.utils.StringUtils.getExpressionStringValue(object, null);
return io.ballerina.runtime.api.utils.StringUtils.getExpressionStringValue(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,131 +19,90 @@

import io.ballerina.runtime.api.async.StrandMetadata;
import io.ballerina.runtime.api.types.Parameter;
import io.ballerina.runtime.internal.scheduling.State;
import io.ballerina.runtime.internal.scheduling.Strand;

import java.util.Optional;

/**
* When this class is used as the first argument of an interop method, Ballerina will inject an instance of the class
* when calling. That instance can be used to communicate with currently executing Ballerina runtime.
* When this class is used as the first argument of an interop method, Ballerina will inject an instance of
* the class when calling. That instance can be used to communicate with currently executing Ballerina runtime.
*
* @since 2.0.0
*/
public class Environment {

private final Strand strand;
private Future future;
private Module currentModule;
private String funcName;
private Parameter[] funcPathParams;

public Environment(Strand strand) {
this.strand = strand;
}

public Environment(Strand strand, Module currentModule) {
this.strand = strand;
this.currentModule = currentModule;
future = new Future(this.strand);
}

public Environment(Strand strand, Module currentModule, String funcName, Parameter[] funcPathParams) {
this(strand, currentModule);
this.funcName = funcName;
this.funcPathParams = funcPathParams;
}
public abstract class Environment {

/**
* Returns the Ballerina function name for the corresponding external interop method.
*
* @return function name
*/
public String getFunctionName() {
return funcName;
}
public abstract String getFunctionName();

/**
* Returns an array consisting of the path parameters of the resource function defined as external.
*
* @return array of {@link Parameter}
*/
public Parameter[] getFunctionPathParameters() {
return funcPathParams;
}
public abstract Parameter[] getFunctionPathParameters();

/**
* Mark the current executing strand as async. Execution of Ballerina code after the current
* interop will stop until given BalFuture is completed. However the java thread will not be blocked
* interop will stop until given Ballerina Future is completed. However the java thread will not be blocked
* and will be reused for running other Ballerina code in the meantime. Therefore callee of this method
* must return as soon as possible to avoid starvation of ballerina code execution.
* must return as soon as possible to avoid starvation of Ballerina code execution.
*
* @return BalFuture which will resume the current strand when completed.
* @return {@link Future} which will resume the current strand when completed.
*/
public Future markAsync() {
strand.blockedOnExtern = true;
strand.setState(State.BLOCK_AND_YIELD);
return future;
}
public abstract Future markAsync();

public Runtime getRuntime() {
return new Runtime(strand.scheduler);
}
/**
* Gets an instance of Ballerina runtime.
*
* @return Ballerina runtime instance.
*/
public abstract Runtime getRuntime();

/**
* Gets current module @{@link Module}.
* Gets current module {@link Module}.
*
* @return module of the environment.
*/
public Module getCurrentModule() {
return currentModule;
}
public abstract Module getCurrentModule();

/**
* Gets the strand id. This will be generated on strand initialization.
*
* @return Strand id.
*/
public int getStrandId() {
return strand.getId();
}
public abstract int getStrandId();

/**
* Gets the strand name. This will be optional. Strand name can be either name given in strand annotation or async
* call or function pointer variable name.
*
* @return Optional strand name.
*/
public Optional<String> getStrandName() {
return strand.getName();
}
public abstract Optional<String> getStrandName();

/**
* Gets @{@link StrandMetadata}.
* Gets {@link StrandMetadata}.
*
* @return metadata of the strand.
*/
public StrandMetadata getStrandMetadata() {
return strand.getMetadata();
}
public abstract StrandMetadata getStrandMetadata();

/**
* Sets given local key value pair in strand.
*
* @param key string key
* @param value value to be store in the strand
*/
public void setStrandLocal(String key, Object value) {
strand.setProperty(key, value);
}
public abstract void setStrandLocal(String key, Object value);

/**
* Gets the value stored in the strand on given key.
*
* @param key key
* @return value stored in the strand.
*/
public Object getStrandLocal(String key) {
return strand.getProperty(key);
}
public abstract Object getStrandLocal(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@

package io.ballerina.runtime.api;

import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.internal.scheduling.Strand;
import io.ballerina.runtime.internal.values.MapValueImpl;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* A future that will resume the underling strand when completed.
*
* @since 2.0.0
*/
public class Future {
private final Strand strand;
private final AtomicBoolean visited = new AtomicBoolean();
Future(Strand strand) {
this.strand = strand;
}
public abstract class Future {

public void complete(Object returnValue) {
if (visited.getAndSet(true)) {
throw ErrorCreator.createError(StringUtils.fromString("cannot complete the same future twice."),
new MapValueImpl<>(PredefinedTypes.TYPE_ERROR_DETAIL));
}
strand.returnValue = returnValue;
strand.scheduler.unblockStrand(strand);
}
public abstract void complete(Object returnValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.ballerina.runtime.api.types.StringType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.TypedescType;
import io.ballerina.runtime.api.types.UnionType;
import io.ballerina.runtime.api.types.XmlAttributesType;
import io.ballerina.runtime.api.types.XmlType;
import io.ballerina.runtime.internal.IteratorUtils;
Expand Down Expand Up @@ -175,17 +176,19 @@ public class PredefinedTypes {
public static final MapType TYPE_DETAIL;
public static final Type TYPE_ERROR_DETAIL;
public static final ErrorType TYPE_ERROR;
public static final BUnionType TYPE_CLONEABLE;
public static final UnionType TYPE_CLONEABLE;

public static final BUnionType TYPE_JSON_DECIMAL;
public static final BUnionType TYPE_JSON_FLOAT;
public static final UnionType TYPE_JSON_DECIMAL;
public static final UnionType TYPE_JSON_FLOAT;

public static final RecordType STRING_ITR_NEXT_RETURN_TYPE =
IteratorUtils.createIteratorNextReturnType(PredefinedTypes.TYPE_STRING_CHAR);

public static final Type ANY_AND_READONLY_TYPE = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(TYPE_ANY);
public static final Type ANY_AND_READONLY_OR_ERROR_TYPE;

private PredefinedTypes() {}

// type anydata = ()|boolean|int|float|decimal|string|xml|anydata[]|map<anydata>|table<map<anydata>>
static {
ArrayList<Type> members = new ArrayList<>();
Expand All @@ -201,9 +204,6 @@ public class PredefinedTypes {
TYPE_ANYDATA_ARRAY = new BArrayType(TYPE_ANYDATA);
}

private PredefinedTypes() {
}

private static BAnydataType getAnydataType(List<Type> members, String typeName, boolean readonly) {
BAnydataType anydataType = new BAnydataType(new BUnionType(TypeConstants.ANYDATA_TNAME, EMPTY_MODULE, members
, readonly), typeName, readonly);
Expand Down
Loading

0 comments on commit 3b4552f

Please sign in to comment.