use of org.ballerinalang.bre.bvm.CallableUnitCallback 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.CallableUnitCallback in project ballerina by ballerina-lang.
the class Close method closeResponse.
private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class CloseDelimitedRecordChannel method closeResponse.
private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class NextTextRecord method response.
/*
* Response obtained after reading record.
*
* @param result the result obtained after processing the record.
* @return the response obtained after reading record.
*/
private static EventResult response(EventResult<String[], EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
String[] fields = result.getResponse();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
context.setReturnValues(new BStringArray(fields));
}
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class Read method readResponse.
/*
* Function which will be notified on the response obtained after the async operation.
*
* @param result context of the callback.
* @return Once the callback is processed we further return back the result.
*/
private static EventResult readResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct;
BRefValueArray contentTuple = new BRefValueArray(readTupleType);
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
Integer numberOfBytes = result.getResponse();
CallableUnitCallback callback = eventContext.getCallback();
byte[] content = (byte[]) eventContext.getProperties().get(ReadBytesEvent.CONTENT_PROPERTY);
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
contentTuple.add(0, new BBlob(content));
contentTuple.add(1, new BInteger(numberOfBytes));
context.setReturnValues(contentTuple);
}
callback.notifySuccess();
return result;
}
Aggregations