从评估的JS与Rhino获取函数名称及其参数 [英] Getting function names and their arguments from evaluated JS with Rhino

查看:75
本文介绍了从评估的JS与Rhino获取函数名称及其参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用犀牛。并在做完之后

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);

我想知道有一个名为 f 和两个参数 x y
,但找不到任何方法帮助我。

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.

推荐答案

尝试使用下一个代码。

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;
}

这篇关于从评估的JS与Rhino获取函数名称及其参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆