javarascal

How do I use the Rascal Evaluator to access Rascal functions from a compiled JAR?


I have the a jar with the following structure:

main-app/
├─ META-INF/
│  ├─ maven.org.rascalmpl.runmain/
│  │  ├─ pom.xml
│  │  ├─ pom.properties
│  ├─ MANIFEST.MF
├─ rascal/
│  ├─ Main.tpl
│  ├─ MyModule.tpl
├─ Main.rsc
├─ MyModule.rsc

and I'm writing an Evaluator to run function from the JAR, so far I have the following:

    final GlobalEnvironment heap = new GlobalEnvironment();
    final ModuleEnvironment top = new ModuleEnvironment("test", heap);
    Evaluator eval = new Evaluator(vf, System.in, System.err, System.out, top, heap);
    eval.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
    eval.addRascalSearchPath(URIUtil.rootLocation("std"));



    ISourceLocation tmp =  URIUtil.correctLocation("jar", "", "/path/to/runmain-0.1.0-SNAPSHOT.jar!");

    eval.addRascalSearchPath(tmp);

    eval.doImport(null, "Main");
    eval.doImport(null, "MyModule");
    
    System.out.println(eval.getHeap().existsModule("Main")); // Check if module has been added to heap

    eval.call("main", tmp, null);

When I try to run this, it just prints out false, and then the following exception:

Exception in thread "main" org.rascalmpl.interpreter.staticErrors.ModuleImport: Could not import module Main: can not find in search path

How can I access either the .tpl files or .rsc files in the JAR to call arbitrary functions?

P.S: When I compiled the JAR the RASCAL.MF was not included in the output, is it necessary?


Solution

  • If the run-main jar has a META-INF/RASCAL.MF in it with Project-Name: run-main in it, then the Rascal URI name resolver automatically registers lib://run-main and lets it point to the root of the jar. So that would enable you to do this:

    ISourceLocation tmp =  URIUtil.correctLocation("lib","run-main","");
    

    Instead of this, you can also use the jar scheme. But this requires you to find the jar file location:

    jar+file:///path/to/run-main.jar/!/
    

    After the ! is where the path into the jar files starts, and where .rsc files can be found. The earlier lib scheme does exactly that, but automatically based on searching through the classpath for RASCAL.MF files.