Getting function names and their arguments from evaluated JS with Rhino

I am using Rhino . and after doing

Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); cx.evaluateString(scope, "function f(x,y){ return x+y}", "<cmd>", 1, null); 

I would like to know that there is a function named f and two parameters x and y but could not find any methods that could help me with this.

+3
source share
1 answer

Try using the following code.

 package com.qarea.rhinotest; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; public class RhinoTest { public static void main(String[] args) { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); cx.evaluateString(scope, "function f(x,y){ return x+y}", "<cmd>", 1, null); try { String result = (String) cx.evaluateString(scope, "f.toString()", "<cmd>", 1, null); System.out.println(result); } catch (org.mozilla.javascript.EcmaError ex) { System.out.println(ex.getMessage()); } } } // Maven dependency // <dependency> // <groupId>org.mozilla</groupId> // <artifactId>rhino</artifactId> // <version>1.7R4</version> // </dependency> 

Output:

 function f(x, y) { return x + y; } 
0
source

All Articles