Getting function names and their arguments from evaluated JS with Rhino

可紊 提交于 2019-12-12 13:28:22

问题


I'm 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'd like to know that there's a function with the name f and two parameters x and y but could not find any methods that can help me with that.


回答1:


Try to use next 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>

The output is:

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


来源:https://stackoverflow.com/questions/11515710/getting-function-names-and-their-arguments-from-evaluated-js-with-rhino

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!