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);
}
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;
}
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;
}
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 });
}
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);
}
Aggregations