Accessing Java fields from embedded Rhino

50 views
Skip to first unread message

Angelo Borsotti

unread,
Dec 9, 2023, 2:47:14 AM12/9/23
to mozilla-rhino
Hi,

I have a Java program that embeds Rhino, and calls it
to evaluate a js expression.
The java program creates a class and an object of it.
I would like to access the fields of that object from within
the js expression. E.g.:

    static class MyClass {
        int info;
    }

    public static void main(String[] args){
        Context context = Context.enter();
        ScriptableObject scope = context.initStandardObjects();
        MyClass mc = new MyClass();
        mc.info = 10;
        scope.defineProperty("mc",mc,ScriptableObject.READONLY);
        System.out.println(
            context.evaluateString(scope,"mc.info", "script", 1, null));
    }

The result of the execution of this is:

org.mozilla.javascript.Undefined@0

Does someone know if it is possible to refer to the fields of
the Java object form js, like. e.g. the "mc.info" above, and what
must be done to allow it?

Thank you
-Angelo Borsotti

Reply all
Reply to author
Forward
0 new messages