Javascript Mozilla Rhino函数内部函数提取

Javascript Mozilla Rhino函数内部函数提取,javascript,java,mozilla,rhino,Javascript,Java,Mozilla,Rhino,我试图弄明白为什么Rhino不能在函数中获取函数对象 根据Rhino文档,这就是如何从java端提取javascript中的函数 Object fObj = scope.get("f", scope); if (!(fObj instanceof Function)) { System.out.println("f is undefined or not a function."); } else { Object functionArgs[] = { "my arg" };

我试图弄明白为什么Rhino不能在函数中获取函数对象

根据Rhino文档,这就是如何从java端提取javascript中的函数

Object fObj = scope.get("f", scope);
if (!(fObj instanceof Function)) {
    System.out.println("f is undefined or not a function.");
} else {
    Object functionArgs[] = { "my arg" };
    Function f = (Function)fObj;
    Object result = f.call(cx, scope, scope, functionArgs);
    String report = "f('my args') = " + Context.toString(result);
    System.out.println(report);
}
如果我有这个javascript,它会工作得很好:

function f(){
    //some lines
}
然而,问题是:

假设我有这样一个java函数:

class RemoteJavaClass{
public void extractJavaScript(String targetFunctionName){
    Object fObj = scope.get(targetFunctionName, scope);
    if (!(fObj instanceof Function)) {
        System.out.println(targetFunctionName + " is undefined or not a function.");
    }
}
} 
在abc.js文件中使用如下javascript:

function foo(){
    function inner(){
        //something
    }

    remrem.extractJavaScript("inner");
}
foo()
在从java执行abc.js之前,我必须“注入”变量remrem,如下所示(以便javascript能够调用java函数):

其结果是:

 "inner is undefined or not a function."
然而,如果脚本看起来像这样,那么Rhino将能够提取内部代码。如果整件事都不在函数内,那么Rhino提取内部函数不会有任何问题

function inner(){
    //something
}
remrem.extractJavaScript("inner");
我已经玩了足够多的范围,并尝试了这一点,但没有工作。假设Rhino在全局范围内寻找内部,所以我继续尝试在函数范围内查找,但没有成功,它没有工作

Object fObj = scope.get("f", scope);
if (!(fObj instanceof Function)) {
    System.out.println("f is undefined or not a function.");
} else {
    Object functionArgs[] = { "my arg" };
    Function f = (Function)fObj;
    Object result = f.call(cx, **fObj**, **fObj**, functionArgs);
    String report = "f('my args') = " + Context.toString(result);
    System.out.println(report);
}
但我还是犯了一个错误:

org.mozilla.javascript.UniqueTag@11769f4c: NOT_FOUND
有谁对犀牛有很好的经验并帮助我吗


多谢各位

我知道这个问题真的很老了,但由于上周我遇到了一个类似的问题,我希望我的回答能帮助其他人解决同样的问题。您正确地假设Rhino正在查看全局范围,因此您需要首先访问foo函数的范围。但是,您不能像那样访问JavaScript中的内部函数。一种方法是遵循显示模块模式。您可以通过以下链接查看有关此模式的更多信息:

因此,一种方法是按照以下方式编写脚本:

// Define the Foo module
var Foo = (function() {
    // Variables and other module functions
    // ...

    function inner() {
    }

    // Export public functions of the module
    return {
        inner: inner
    };
})();
Context context = Context.enter();

// Assume that the script is stored in a String variable called "s"

try {
    ScriptableObject globalScope = context.initStandardObjects();
    context.evaluateString(globalScope, s, "script", 1, null);

    // We now have access to the scope of the Foo module
    ScriptableObject fooScope = (ScriptableObject) globalScope.get("Foo", globalScope);
    final Function function = (Function) fooScope.get("inner", fooScope);
    final Object result = function.call(context, fooScope, fooScope, new Object[] {});

    // ...          
    } finally {
        Context.exit();
    }
}
然后,使用Rhino,您可以访问内部函数,如下所示:

// Define the Foo module
var Foo = (function() {
    // Variables and other module functions
    // ...

    function inner() {
    }

    // Export public functions of the module
    return {
        inner: inner
    };
})();
Context context = Context.enter();

// Assume that the script is stored in a String variable called "s"

try {
    ScriptableObject globalScope = context.initStandardObjects();
    context.evaluateString(globalScope, s, "script", 1, null);

    // We now have access to the scope of the Foo module
    ScriptableObject fooScope = (ScriptableObject) globalScope.get("Foo", globalScope);
    final Function function = (Function) fooScope.get("inner", fooScope);
    final Object result = function.call(context, fooScope, fooScope, new Object[] {});

    // ...          
    } finally {
        Context.exit();
    }
}