use of org.ballerinalang.bre.bvm.WorkerData in project ballerina by ballerina-lang.
the class BLangVMUtils method populateArgAndReturnData.
@SuppressWarnings("rawtypes")
public static int[][] populateArgAndReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, BValue[] args) {
WorkerDataIndex wdi1 = callableUnitInfo.paramWorkerIndex;
WorkerDataIndex wdi2 = callableUnitInfo.retWorkerIndex;
WorkerData local = createWorkerData(wdi1, wdi2);
BType[] types = callableUnitInfo.getParamTypes();
int longParamCount = 0, doubleParamCount = 0, stringParamCount = 0, intParamCount = 0, byteParamCount = 0, refParamCount = 0;
for (int i = 0; i < types.length; i++) {
switch(types[i].getTag()) {
case TypeTags.INT_TAG:
if (args[i] instanceof BString) {
local.longRegs[longParamCount] = ((BString) args[i]).intValue();
} else {
local.longRegs[longParamCount] = ((BInteger) args[i]).intValue();
}
longParamCount++;
break;
case TypeTags.FLOAT_TAG:
if (args[i] instanceof BString) {
local.doubleRegs[doubleParamCount] = ((BString) args[i]).floatValue();
} else {
local.doubleRegs[doubleParamCount] = ((BFloat) args[i]).floatValue();
}
doubleParamCount++;
break;
case TypeTags.STRING_TAG:
local.stringRegs[stringParamCount] = args[i].stringValue();
stringParamCount++;
break;
case TypeTags.BOOLEAN_TAG:
if (args[i] instanceof BString) {
local.intRegs[intParamCount] = ((BString) args[i]).value().toLowerCase().equals("true") ? 1 : 0;
} else {
local.intRegs[intParamCount] = ((BBoolean) args[i]).booleanValue() ? 1 : 0;
}
intParamCount++;
break;
case TypeTags.BLOB_TAG:
local.byteRegs[byteParamCount] = ((BBlob) args[i]).blobValue();
byteParamCount++;
break;
default:
local.refRegs[refParamCount] = (BRefType) args[i];
refParamCount++;
break;
}
}
ctx.workerLocal = local;
return new int[][] { wdi1.retRegs, BLangVMUtils.createReturnRegValues(wdi1, wdi2, callableUnitInfo.getRetParamTypes()) };
}
use of org.ballerinalang.bre.bvm.WorkerData in project ballerina by ballerina-lang.
the class BLangVMUtils method createWorkerDataForLocal.
static WorkerData createWorkerDataForLocal(WorkerInfo workerInfo, WorkerExecutionContext parentCtx, int[] argRegs) {
WorkerData wd = createWorkerData(workerInfo);
BLangVMUtils.copyValuesForForkJoin(parentCtx.workerLocal, wd, argRegs);
return wd;
}
use of org.ballerinalang.bre.bvm.WorkerData in project ballerina by ballerina-lang.
the class BLangVMUtils method populateReturnData.
public static BValue[] populateReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, int[] retRegs) {
WorkerData data = ctx.workerLocal;
BType[] retTypes = callableUnitInfo.getRetParamTypes();
BValue[] returnValues = new BValue[retTypes.length];
for (int i = 0; i < returnValues.length; i++) {
BType retType = retTypes[i];
switch(retType.getTag()) {
case TypeTags.INT_TAG:
returnValues[i] = new BInteger(data.longRegs[retRegs[i]]);
break;
case TypeTags.FLOAT_TAG:
returnValues[i] = new BFloat(data.doubleRegs[retRegs[i]]);
break;
case TypeTags.STRING_TAG:
returnValues[i] = new BString(data.stringRegs[retRegs[i]]);
break;
case TypeTags.BOOLEAN_TAG:
boolean boolValue = data.intRegs[retRegs[i]] == 1;
returnValues[i] = new BBoolean(boolValue);
break;
case TypeTags.BLOB_TAG:
returnValues[i] = new BBlob(data.byteRegs[retRegs[i]]);
break;
default:
returnValues[i] = data.refRegs[retRegs[i]];
break;
}
}
return returnValues;
}
use of org.ballerinalang.bre.bvm.WorkerData in project ballerina by ballerina-lang.
the class BLangFunctions method invokeNativeCallable.
private static WorkerExecutionContext invokeNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, int flags) {
WorkerData parentLocalData = parentCtx.workerLocal;
BType[] retTypes = callableUnitInfo.getRetParamTypes();
WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
Context ctx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
if (nativeCallable == null) {
return parentCtx;
}
try {
if (nativeCallable.isBlocking()) {
nativeCallable.execute(ctx, null);
BLangVMUtils.populateWorkerDataWithValues(parentLocalData, retRegs, ctx.getReturnValues(), retTypes);
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
TraceUtil.finishTraceSpan(TraceUtil.getTracer(parentCtx));
}
/* we want the parent to continue, since we got the response of the native call already */
return parentCtx;
} else {
CallableUnitCallback callback;
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
callback = new TraceableCallbackWrapper(parentCtx, new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes));
} else {
callback = new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes);
}
nativeCallable.execute(ctx, callback);
/* we want the parent to suspend (i.e. go to wait for response state) and stay until notified */
return null;
}
} catch (BLangNullReferenceException e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createNullRefException(callableUnitInfo));
} catch (Throwable e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createError(callableUnitInfo, e.getMessage()));
}
}
use of org.ballerinalang.bre.bvm.WorkerData in project ballerina by ballerina-lang.
the class BLangFunctions method invokeCallable.
/**
* This method does not short circuit the execution of the first worker to execute in the
* same calling thread, but rather executes all the workers in their own separate threads.
* This is specifically useful in executing service resources, where the calling transport
* threads shouldn't be blocked, but rather the worker threads should be used.
*/
public static void invokeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, CallableUnitCallback responseCallback) {
WorkerSet workerSet = callableUnitInfo.getWorkerSet();
SyncCallableWorkerResponseContext respCtx = new SyncCallableWorkerResponseContext(callableUnitInfo.getRetParamTypes(), workerSet.generalWorkers.length);
respCtx.registerResponseCallback(responseCallback);
if (TraceManagerWrapper.getInstance().isTraceEnabled()) {
respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
}
respCtx.joinTargetContextInfo(parentCtx, retRegs);
WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
/* execute the init worker and extract the local variables created by it */
WorkerData initWorkerLocalData = null;
CodeAttributeInfo initWorkerCAI = null;
if (workerSet.initWorker != null) {
initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
if (initWorkerLocalData == null) {
handleError(parentCtx);
return;
}
initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
}
for (int i = 0; i < workerSet.generalWorkers.length; i++) {
executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
}
}
Aggregations