Search in sources :

Example 11 with Debugger

use of org.ballerinalang.util.debugger.Debugger in project ballerina by ballerina-lang.

the class StartServices method execute.

/**
 * Starts all the services defined in the package specified in the 'packageName' argument.
 */
@Override
public void execute(Context ctx) {
    String packageName = ctx.getStringArgument(0);
    for (ProgramFile programFile : TesterinaRegistry.getInstance().getProgramFiles()) {
        // Get the service package
        PackageInfo servicesPackage = programFile.getEntryPackage();
        if (servicesPackage == null || !servicesPackage.getPkgPath().equals(packageName)) {
            continue;
        }
        if (!programFile.isServiceEPAvailable()) {
            throw new BallerinaException(String.format("no services found in package: %s", packageName));
        }
        Debugger debugger = new Debugger(programFile);
        initDebugger(programFile, debugger);
        // Invoke package init function
        BLangFunctions.invokePackageInitFunction(servicesPackage.getInitFunctionInfo());
        BLangFunctions.invokeVMUtilFunction(servicesPackage.getStartFunctionInfo());
        ctx.setReturnValues(new BBoolean(true));
        return;
    }
    // package could not be found
    ctx.setReturnValues(new BBoolean(false));
}
Also used : Debugger(org.ballerinalang.util.debugger.Debugger) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BBoolean(org.ballerinalang.model.values.BBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 12 with Debugger

use of org.ballerinalang.util.debugger.Debugger in project ballerina by ballerina-lang.

the class BTestUtils method invoke.

/**
 * Compile and return the semantic errors.
 *
 * @param sourceRoot  root path of the source packages
 * @param packageName name of the package to compile
 * @return Semantic errors
 */
// public static CompileResult compile(String sourceRoot, String packageName) {
// try {
// String effectiveSource;
// Path rootPath = Paths.get(BTestUtils.class.getProtectionDomain().getCodeSource()
// .getLocation().toURI().getPath().concat(sourceRoot));
// if (Files.isDirectory(Paths.get(packageName))) {
// String[] pkgParts = packageName.split("\\/");
// List<Name> pkgNameComps = Arrays.stream(pkgParts)
// .map(part -> {
// if (part.equals("")) {
// return Names.EMPTY;
// } else if (part.equals("_")) {
// return Names.EMPTY;
// }
// return new Name(part);
// })
// .collect(Collectors.toList());
// PackageID pkgId = new PackageID(pkgNameComps, Names.DEFAULT_VERSION);
// effectiveSource = pkgId.getName().getValue();
// } else {
// effectiveSource = packageName;
// }
// return compile(rootPath.toString(), effectiveSource, CompilerPhase.CODE_GEN);
// } catch (URISyntaxException e) {
// throw new IllegalArgumentException("error while running test: " + e.getMessage());
// }
// }
// /**
// * Compile and return the semantic errors.
// *
// * @param sourceFilePath Path to source package/file
// * @param compilerPhase  Compiler phase
// * @return Semantic errors
// */
// public static CompileResult compile(String sourceFilePath, CompilerPhase compilerPhase) {
// Path sourcePath = Paths.get(sourceFilePath);
// String packageName = sourcePath.getFileName().toString();
// Path sourceRoot = resourceDir.resolve(sourcePath.getParent());
// return compile(sourceRoot.toString(), packageName, compilerPhase);
// }
// 
// /**
// * Compile and return the semantic errors.
// *
// * @param sourceRoot    root path of the source packages
// * @param packageName   name of the package to compile
// * @param compilerPhase Compiler phase
// * @return Semantic errors
// */
// public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
// CompilerContext context = new CompilerContext();
// CompilerOptions options = CompilerOptions.getInstance(context);
// options.put(SOURCE_ROOT, resourceDir.resolve(sourceRoot).toString());
// options.put(COMPILER_PHASE, compilerPhase.toString());
// options.put(PRESERVE_WHITESPACE, "false");
// 
// CompileResult comResult = new CompileResult();
// 
// // catch errors
// DiagnosticListener listener = comResult::addDiagnostic;
// context.put(DiagnosticListener.class, listener);
// 
// // compile
// Compiler compiler = Compiler.getInstance(context);
// compiler.compile(packageName);
// org.wso2.ballerinalang.programfile.ProgramFile programFile = compiler.getCompiledProgram();
// if (programFile != null) {
// comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
// }
// 
// return comResult;
// }
// 
// /**
// * Compile and return the compiled package node.
// *
// * @param sourceFilePath Path to source package/file
// * @return compiled package node
// */
// public static BLangPackage compileAndGetPackage(String sourceFilePath) {
// Path sourcePath = Paths.get(sourceFilePath);
// String packageName = sourcePath.getFileName().toString();
// Path sourceRoot = resourceDir.resolve(sourcePath.getParent());
// CompilerContext context = new CompilerContext();
// CompilerOptions options = CompilerOptions.getInstance(context);
// options.put(SOURCE_ROOT, resourceDir.resolve(sourceRoot).toString());
// options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
// options.put(PRESERVE_WHITESPACE, "false");
// 
// CompileResult comResult = new CompileResult();
// 
// // catch errors
// DiagnosticListener listener = comResult::addDiagnostic;
// context.put(DiagnosticListener.class, listener);
// 
// // compile
// Compiler compiler = Compiler.getInstance(context);
// compiler.compile(packageName);
// BLangPackage compiledPkg = (BLangPackage) compiler.getAST();
// 
// return compiledPkg;
// }
/**
 * Invoke a ballerina function.
 *
 * @param compileResult CompileResult instance
 * @param packageName   Name of the package to invoke
 * @param functionName  Name of the function to invoke
 * @param args          Input parameters for the function
 * @return return values of the function
 */
public static BValue[] invoke(CompileResult compileResult, String packageName, String functionName, BValue[] args) {
    if (compileResult.getErrorCount() > 0) {
        String msg = "";
        for (Diagnostic diagnostic : compileResult.getDiagnostics()) {
            msg += diagnostic.getMessage() + "\n";
        }
        throw new IllegalStateException("compilation contains errors.. " + msg);
    }
    ProgramFile programFile = compileResult.getProgFile();
    Debugger debugger = new Debugger(programFile);
    programFile.setDebugger(debugger);
    return BLangFunctions.invokeEntrypointCallable(programFile, packageName, functionName, args);
}
Also used : Debugger(org.ballerinalang.util.debugger.Debugger) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Aggregations

Debugger (org.ballerinalang.util.debugger.Debugger)12 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)7 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 BValue (org.ballerinalang.model.values.BValue)3 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)2 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)2 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)2 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)1 CompileResult (org.ballerinalang.launcher.util.CompileResult)1 BBoolean (org.ballerinalang.model.values.BBoolean)1 LineNumberInfo (org.ballerinalang.util.codegen.LineNumberInfo)1 DebugContext (org.ballerinalang.util.debugger.DebugContext)1