// $ javac -cp bin/ examples/CallFromJava.java && java -cp nqp-runtime.jar:3rdparty/asm/asm-4.1.jar:3rdparty/asm/asm-tree-4.1.jar:. examples.CallFromJava
// 4

// Note: curently nqp only functions properly in the application class path.

package examples;
import org.perl6.nqp.runtime.*;
import static org.perl6.nqp.runtime.CallSiteDescriptor.*;
import org.perl6.nqp.sixmodel.*;

public class CallFromJava {
    private GlobalContext g;
    private ThreadContext t;
    private SixModelObject nqpComp;

    private CallFromJava() {
        g = new GlobalContext();
        t = g.getCurrentThreadContext();

        Ops.loadbytecode("nqp.jar", t);
        nqpComp = Ops.getcomp("nqp", t);
    }

    private SixModelObject eval(String nqp) {
        Ops.invokeDirect(t, Ops.findmethod(t, nqpComp, "compile"),
                new CallSiteDescriptor(new byte[] { ARG_OBJ, ARG_STR }, null),
                new Object[] { nqpComp, nqp });

        Ops.invokeDirect(t, Ops.result_o(t.resultFrame()), Ops.emptyCallSite, Ops.emptyArgList);
        return Ops.result_o(t.resultFrame());
    }

    public static void main(String[] args) {
        CallFromJava nqp = new CallFromJava();

        nqp.eval("say(2 + 2)");
    }
}



@@ -1,11 +1,14 @@
 // 
+nqp
 $ javac 
@@ -46,11 +46,16 @@
 java
- &&
+%0A// nqp$
  jav
@@ -158,13 +158,248 @@
 Java
-%0A// 4
+ nqp.jar nqp 'say(2+2)'%0A// 4%0A// rakudo$ java -cp ../nqp-jvm:../nqp-jvm/3rdparty/asm/asm-4.1.jar:../nqp-jvm/3rdparty/asm/asm-tree-4.1.jar:../nqp-jvm/nqp-runtime.jar:rakudo-runtime.jar:. examples.CallFromJava perl6.jar perl6 'say 2 + 2'%0A// 4%0A
 %0A%0A//
@@ -764,16 +764,43 @@
 romJava(
+String bytecode, String hll
 ) %7B%0A    
@@ -899,17 +899,16 @@
 ode(
-%22nqp.jar%22
+bytecode
 , t)
@@ -943,13 +943,11 @@
 omp(
-%22nqp%22
+hll
 , t)
@@ -1435,16 +1435,32 @@
 romJava(
+args%5B0%5D, args%5B1%5D
 );%0A%0A    
@@ -1476,20 +1476,15 @@
 val(
-%22say(2 + 2)%22
+args%5B2%5D
 );%0A 

make examples/CallFromJava.java work with any HLL