Search in sources :

Example 1 with NativeCallableUnit

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

the class NativeConstructLoadingTest method testLoadingExistingFunction.

@Test
public void testLoadingExistingFunction() {
    NativeCallableUnit function = this.nativeLoader.loadNativeFunction("ballerina.math", "pow");
    Assert.assertNotNull(function);
}
Also used : NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) Test(org.testng.annotations.Test)

Example 2 with NativeCallableUnit

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

the class NativeUnitLoader method loadNativeAction.

public NativeCallableUnit loadNativeAction(String pkgName, String connectorName, String actionName) {
    String key = NativeElementRepository.actionToKey(pkgName, connectorName, actionName);
    NativeCallableUnit result = this.nativeUnitsCache.get(key);
    if (result == null) {
        NativeFunctionDef actionDef = this.nativeElementRepo.lookupNativeAction(pkgName, connectorName, actionName);
        if (actionDef != null) {
            try {
                result = (NativeCallableUnit) Class.forName(actionDef.getClassName()).newInstance();
                this.nativeUnitsCache.put(key, result);
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                throw new RuntimeException("Error in loading native action: " + e.getMessage(), e);
            }
        }
    }
    return result;
}
Also used : NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) NativeFunctionDef(org.ballerinalang.natives.NativeElementRepository.NativeFunctionDef)

Example 3 with NativeCallableUnit

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

the class NativeUnitLoader method loadNativeFunction.

public NativeCallableUnit loadNativeFunction(String pkgName, String functionName) {
    String key = NativeElementRepository.functionToKey(pkgName, functionName);
    NativeCallableUnit result = this.nativeUnitsCache.get(key);
    if (result == null) {
        NativeFunctionDef functionDef = this.nativeElementRepo.lookupNativeFunction(pkgName, functionName);
        if (functionDef != null) {
            try {
                result = (NativeCallableUnit) Class.forName(functionDef.getClassName()).newInstance();
                this.nativeUnitsCache.put(key, result);
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                throw new RuntimeException("Error in loading native function: " + e.getMessage(), e);
            }
        }
    }
    return result;
}
Also used : NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) NativeFunctionDef(org.ballerinalang.natives.NativeElementRepository.NativeFunctionDef)

Example 4 with NativeCallableUnit

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

the class BLangFunctions method invokeNativeCallableAsync.

private static void invokeNativeCallableAsync(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs) {
    WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
    Context nativeCtx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
    NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
    if (nativeCallable == null) {
        return;
    }
    AsyncInvocableWorkerResponseContext respCtx;
    if (nativeCallable.isBlocking()) {
        respCtx = BLangScheduler.executeBlockingNativeAsync(nativeCallable, nativeCtx);
    } else {
        respCtx = BLangScheduler.executeNonBlockingNativeAsync(nativeCallable, nativeCtx);
    }
    BLangVMUtils.populateWorkerDataWithValues(parentCtx.workerLocal, retRegs, new BValue[] { new BCallableFuture(callableUnitInfo.getName(), respCtx) }, new BType[] { BTypes.typeFuture });
}
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) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) BCallableFuture(org.ballerinalang.model.values.BCallableFuture) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 5 with NativeCallableUnit

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

the class ProgramFileReader method readFunctionInfo.

private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int funcNameCPIndex = dataInStream.readInt();
    UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
    String funcName = funcNameUTF8Entry.getValue();
    FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
    functionInfo.setPackageInfo(packageInfo);
    int funcSigCPIndex = dataInStream.readInt();
    UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
    setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
    int flags = dataInStream.readInt();
    boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
    functionInfo.setNative(nativeFunc);
    String uniqueFuncName;
    boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
    if (attached) {
        int attachedToTypeCPIndex = dataInStream.readInt();
        functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
        functionInfo.attachedToType = typeRefCPEntry.getType();
        uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
        // Update the attachedFunctionInfo
        if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
            BStructType structType = (BStructType) typeRefCPEntry.getType();
            AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
            attachedFuncInfo.functionInfo = functionInfo;
        }
    } else {
        uniqueFuncName = funcName;
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
    }
    int workerDataChannelsLength = dataInStream.readShort();
    for (int i = 0; i < workerDataChannelsLength; i++) {
        readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
    }
    // Read worker info entries
    readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
    if (nativeFunc) {
        NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
        if (nativeFunction == null) {
            throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
        }
        functionInfo.setNativeCallableUnit(nativeFunction);
    }
    // Read attributes
    readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

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