Hi,

I'm trying to find a way to fulfill the following scenario: A Java class is
created from some JavaScript code using jsc. When a method on that class is
called, it returns a Scriptable js object, which itself contains a function
as a property. I'm trying to find a way to call that function from Java
code. Right now, I'm able to get everything to work, up to the point where
I store the js function in a Java Function variable, and attempt to call it
with Function.call. The problem is that Function.call requires one to pass
in the Context as an argument, and it's not clear to me how to retrieve the
current context, as the class generated by jsc seems to abstract out the
process of Context creation. I've tried to use Context.getCurrentContext,
but it always returns null.

Please consider the following reduced example which illustrates this:

JavaScript:

function getObj(){
    //create a new object with a property and method, and return it
    //the goal will be to call the method from java
    return {
        x : 1,
        getX : function(){
            return this.x;
        }
    };
}

https://gist.github.com/2781615

Java:

import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import test.Reduced;

class TestReduced {
    public static void main(String[] args){
        Reduced reduced = new Reduced();

        Scriptable o = (Scriptable) reduced.getObj();
        System.out.println("js object:");
        System.out.println(o);

        Scriptable scope = o.getParentScope();
        System.out.println("scope:");
        System.out.println(scope);

        Function getXFn = (Function) ScriptableObject.getProperty(o,"getX");
        System.out.println("getX function:");
        System.out.println(getXFn);

        //this will always be null.
        //how do we get the context?
        Context context = Context.getCurrentContext();
        System.out.println("context:");
        System.out.println(context);

        Object[] getXFnArgs = {};

        getXFn.call(context,scope,o,getXFnArgs);

    }
}


https://gist.github.com/2781612


Here is how I've compiled everything:

jbeard@jbeard-VirtualBox:~/workspace/scion/scion-js$ java -cp
~/Downloads/rhino1_7R3/js.jar org.mozilla.javascript.tools.jsc.Main
-extends java.lang.Object -package test Reduced.js
jbeard@jbeard-VirtualBox:~/workspace/scion/scion-js$ javac -classpath
~/Downloads/rhino1_7R3/js.jar:. TestReduced.java

Here is the output when it is run:

jbeard@jbeard-VirtualBox:~/workspace/scion/scion-js$ java -cp
~/Downloads/rhino1_7R3/js.jar:. TestReducedjs
object:
[object Object]
scope:
org.mozilla.javascript.tools.shell.Global@12f0999
getX function:
test.Reduced1@11f2ee1
context:
null
Exception in thread "main" java.lang.NullPointerException
 at org.mozilla.javascript.ScriptRuntime.hasTopCall(ScriptRuntime.java:3153)
at test.Reduced1.call(Unknown Source)
 at TestReduced.main(TestReduced.java:31)



I'd appreciate any insight anyone can offer into this. Thanks!

Jake
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to