使用Rhino从已评估的JS中获取函数名及其参数

Getting function names and their arguments from evaluated JS with Rhino

本文关键字:函数 获取 参数 JS Rhino 评估 使用      更新时间:2023-11-10

我正在使用Rhino。并且在进行之后

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

我想知道有一个函数名为f,有两个参数xy但找不到任何可以帮助我的方法。

尝试使用下一个代码。

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>

输出为:

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