Skip to content

Commit

Permalink
Simplify running NQP code from Java.
Browse files Browse the repository at this point in the history
This adds two 'run' methods to the EvalServer which lets us use the EvalServer
from any Java application.
  • Loading branch information
peschwa committed Mar 20, 2015
1 parent 9d3e790 commit 34fdea8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/tools/EvalServer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.perl6.nqp.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -37,6 +38,40 @@ public class EvalServer {
private WritableByteChannel tokenCh;
private Path tokenPath;

public String run(String appPath, String code) throws Exception {
String[] argv = new String[2];
argv[0] = "-e";
argv[1] = code;
return run(appPath, argv);
}

public String run(String appPath, String[] argv) throws Exception {
try {
cuType = LibraryLoader.loadFile( appPath, true );
} catch (ThreadDeath td) {
throw new RuntimeException("Couldn't loadFile. Your CLASSPATH might not be set up correctly.");
}

GlobalContext gc = new GlobalContext();
gc.in = new ByteArrayInputStream(new byte[0]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
gc.out = gc.err = new PrintStream( baos, true, "UTF-8" );
gc.interceptExit = true;
gc.sharingHint = true;

CompilationUnit cu = CompilationUnit.setupCompilationUnit(gc.mainThread, cuType, true);
CodeRef entryRef = null;
if (cu.entryQbid() >= 0) entryRef = cu.lookupCodeRef(cu.entryQbid());
if (entryRef == null)
throw new RuntimeException("This class is not an entry point");
try {
Ops.invokeMain(gc.mainThread, entryRef, cuType.getName(), argv);
} catch (ThreadDeath td) {
baos.flush();
}
return baos.toString("UTF-8");
}

public static void main(String[] args) throws Exception {
String executableName = System.getProperty("perl6.execname");
if (executableName != null) {
Expand Down

0 comments on commit 34fdea8

Please sign in to comment.