Search in sources :

Example 6 with NativeCallableUnit

use of org.ballerinalang.model.NativeCallableUnit in project ballerina by ballerina-lang.

the class ProgramFileReader method readConnectorActionInfoEntries.

private void readConnectorActionInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    for (ConnectorInfo connectorInfo : packageInfo.getConnectorInfoEntries()) {
        // Read action info entries
        int actionCount = dataInStream.readShort();
        for (int j = 0; j < actionCount; j++) {
            // Read action name;
            int actionNameCPIndex = dataInStream.readInt();
            UTF8CPEntry actionNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(actionNameCPIndex);
            String actionName = actionNameUTF8Entry.getValue();
            ActionInfo actionInfo = new ActionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), actionNameCPIndex, actionName, connectorInfo);
            actionInfo.setPackageInfo(packageInfo);
            connectorInfo.addActionInfo(actionName, actionInfo);
            // Read action signature
            int actionSigCPIndex = dataInStream.readInt();
            UTF8CPEntry actionSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(actionSigCPIndex);
            actionInfo.setSignatureCPIndex(actionSigCPIndex);
            actionInfo.setSignature(actionSigUTF8Entry.getValue());
            int flags = dataInStream.readInt();
            // actionInfo.setflags(flags);
            setCallableUnitSignature(actionInfo, actionSigUTF8Entry.getValue(), packageInfo);
            // TODO Temp solution.
            boolean nativeAction = dataInStream.readByte() == 1;
            actionInfo.setNative(nativeAction);
            int workerDataChannelsLength = dataInStream.readShort();
            for (int k = 0; k < workerDataChannelsLength; k++) {
                readWorkerDataChannelEntries(dataInStream, packageInfo, actionInfo);
            }
            // Read worker info entries
            readWorkerInfoEntries(dataInStream, packageInfo, actionInfo);
            if (nativeAction) {
                NativeCallableUnit nativeActionObj = NativeUnitLoader.getInstance().loadNativeAction(actionInfo.getPkgPath(), actionInfo.getConnectorInfo().getName(), actionInfo.getName());
                if (nativeActionObj == null && !actionInfo.name.equals("<init>")) {
                    throw new BLangRuntimeException("native action not available " + actionInfo.getPkgPath() + ":" + actionInfo.getConnectorInfo().getName() + "." + actionName);
                }
                actionInfo.setNativeCallableUnit(nativeActionObj);
            }
            // Read attributes of the struct info
            readAttributeInfoEntries(dataInStream, packageInfo, actionInfo);
        }
        // Read attributes of the struct info
        readAttributeInfoEntries(dataInStream, packageInfo, connectorInfo);
    }
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

Example 7 with NativeCallableUnit

use of org.ballerinalang.model.NativeCallableUnit 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()));
    }
}
Also used : InitWorkerResponseContext(org.ballerinalang.bre.bvm.InitWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) WorkerResponseContext(org.ballerinalang.bre.bvm.WorkerResponseContext) Context(org.ballerinalang.bre.Context) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext) WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback) BType(org.ballerinalang.model.types.BType) TraceableCallbackWrapper(org.ballerinalang.util.tracer.TraceableCallbackWrapper) BLangNullReferenceException(org.ballerinalang.util.exceptions.BLangNullReferenceException) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) WorkerData(org.ballerinalang.bre.bvm.WorkerData) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback)

Example 8 with NativeCallableUnit

use of org.ballerinalang.model.NativeCallableUnit in project ballerina by ballerina-lang.

the class AppointmentJob method execute.

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
    NativeCallableUnit fn = (NativeCallableUnit) jobDataMap.get(AppointmentConstants.BALLERINA_FUNCTION);
    Context balParentContext = (Context) jobDataMap.get(AppointmentConstants.BALLERINA_PARENT_CONTEXT);
    FunctionRefCPEntry onTriggerFunction = (FunctionRefCPEntry) jobDataMap.get(AppointmentConstants.BALLERINA_ON_TRIGGER_FUNCTION);
    FunctionRefCPEntry onErrorFunction = (FunctionRefCPEntry) jobDataMap.get(AppointmentConstants.BALLERINA_ON_ERROR_FUNCTION);
    ProgramFile programFile = balParentContext.getProgramFile();
    TaskExecutor.execute(fn, balParentContext, onTriggerFunction, onErrorFunction, programFile);
}
Also used : Context(org.ballerinalang.bre.Context) JobExecutionContext(org.quartz.JobExecutionContext) JobDataMap(org.quartz.JobDataMap) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 9 with NativeCallableUnit

use of org.ballerinalang.model.NativeCallableUnit in project ballerina by ballerina-lang.

the class NativeConstructLoadingTest method testLoadingNonExistingFunction.

@Test
public void testLoadingNonExistingFunction() {
    NativeCallableUnit function = this.nativeLoader.loadNativeFunction("ballerina.lang.system", "foo");
    Assert.assertNull(function);
}
Also used : NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) Test(org.testng.annotations.Test)

Example 10 with NativeCallableUnit

use of org.ballerinalang.model.NativeCallableUnit in project ballerina by ballerina-lang.

the class NativeConstructLoadingTest method testLoadingFunctionInNonExistingPackage.

@Test
public void testLoadingFunctionInNonExistingPackage() {
    NativeCallableUnit function = this.nativeLoader.loadNativeFunction("ballerina.lang.foo", "println");
    Assert.assertNull(function);
}
Also used : NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) Test(org.testng.annotations.Test)

Aggregations

NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)10 Context (org.ballerinalang.bre.Context)3 Test (org.testng.annotations.Test)3 NativeCallContext (org.ballerinalang.bre.NativeCallContext)2 AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)2 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)2 ForkJoinWorkerResponseContext (org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext)2 InitWorkerResponseContext (org.ballerinalang.bre.bvm.InitWorkerResponseContext)2 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)2 WorkerData (org.ballerinalang.bre.bvm.WorkerData)2 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)2 WorkerResponseContext (org.ballerinalang.bre.bvm.WorkerResponseContext)2 NativeFunctionDef (org.ballerinalang.natives.NativeElementRepository.NativeFunctionDef)2 UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 BLangCallableUnitCallback (org.ballerinalang.bre.BLangCallableUnitCallback)1 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)1 BStructType (org.ballerinalang.model.types.BStructType)1 BType (org.ballerinalang.model.types.BType)1 BCallableFuture (org.ballerinalang.model.values.BCallableFuture)1