Search in sources :

Example 36 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BLangFunctions method invokeEntrypointCallable.

/**
 * This method calls a program callable, considering it as an entry point callable. Which means, this callable will
 * be invoked as the first callable in a program, and after it is called, all the cleanup will be done for it to
 * exit from the program. That is, this callable will wait for the response to be fully available, and it will wait
 * till all the workers in the system to finish executing.
 * @param bLangProgram the program file
 * @param packageName the package the callable is residing
 * @param callableName the callable name
 * @param args the callable arguments
 * @return
 */
public static BValue[] invokeEntrypointCallable(ProgramFile bLangProgram, String packageName, String callableName, BValue[] args) {
    PackageInfo packageInfo = bLangProgram.getPackageInfo(packageName);
    FunctionInfo functionInfo = packageInfo.getFunctionInfo(callableName);
    if (functionInfo == null) {
        throw new RuntimeException("Function '" + callableName + "' is not defined");
    }
    return invokeEntrypointCallable(bLangProgram, packageInfo, functionInfo, args);
}
Also used : BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo)

Example 37 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class TransactionUtils method checkTransactionCoordinatorError.

private static void checkTransactionCoordinatorError(BValue value, WorkerExecutionContext ctx, String errMsg) {
    if (value.getType().getTag() == TypeTags.STRUCT_TAG) {
        PackageInfo errorPackageInfo = ctx.programFile.getPackageInfo(PACKAGE_BUILTIN);
        StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_GENERIC_ERROR);
        if (((BStruct) value).getType().structInfo.equals(errorStructInfo)) {
            throw new BallerinaException(errMsg + ((BStruct) value).getStringField(0));
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 38 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class OpenSecureSocket method createReturnStruct.

private BStruct createReturnStruct(Context context, SSLSocket sslSocket, ByteChannel channel) throws IOException {
    PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
    // Create ByteChannel Struct
    StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
    Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
    BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
    channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
    // Create Socket Struct
    StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
    BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
    socketStruct.setRefField(0, channelStruct);
    socketStruct.setIntField(0, sslSocket.getPort());
    socketStruct.setIntField(1, sslSocket.getLocalPort());
    socketStruct.setStringField(0, sslSocket.getInetAddress().getHostAddress());
    socketStruct.setStringField(1, sslSocket.getLocalAddress().getHostAddress());
    socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
    return socketStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) ByteChannel(java.nio.channels.ByteChannel) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)

Example 39 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class ClientSocketTest method testOpenWithProperties.

@Test(dependsOnMethods = "testClosure", description = "Test connection open with properties")
public void testOpenWithProperties() {
    int port = ThreadLocalRandom.current().nextInt(33000, 46000);
    PackageInfo ioPackageInfo = socketClient.getProgFile().getPackageInfo("ballerina.io");
    StructInfo socketProperties = ioPackageInfo.getStructInfo("SocketProperties");
    BStruct propertyStruct = BLangVMStructs.createBStruct(socketProperties, port);
    BValue[] args = { new BString("localhost"), new BInteger(MockSocketServer.SERVER_PORT), propertyStruct };
    final BValue[] returns = BRunUtil.invoke(socketClient, "openSocketConnectionWithProps", args);
    final BStruct socket = (BStruct) returns[0];
    Assert.assertEquals(socket.getIntField(1), port, "Client port didn't bind to assign port.");
    args = new BValue[] { socket };
    BRunUtil.invoke(socketClient, "close", args);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test)

Example 40 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method createNullRefException.

/* Type Specific Errors */
public static BStruct createNullRefException(Context context) {
    PackageInfo errorPackageInfo = context.getProgramFile().getPackageInfo(PACKAGE_RUNTIME);
    StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_NULL_REF_EXCEPTION);
    return generateError(context.getCallableUnitInfo(), true, errorStructInfo, "");
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Aggregations

PackageInfo (org.ballerinalang.util.codegen.PackageInfo)48 StructInfo (org.ballerinalang.util.codegen.StructInfo)35 BStruct (org.ballerinalang.model.values.BStruct)17 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)10 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)5 Debugger (org.ballerinalang.util.debugger.Debugger)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 BString (org.ballerinalang.model.values.BString)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)2 Instruction (org.ballerinalang.util.codegen.Instruction)2 PackageVarInfo (org.ballerinalang.util.codegen.PackageVarInfo)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 Test (org.testng.annotations.Test)2 PrintStream (java.io.PrintStream)1