简体   繁体   English

为什么Rhino Javascript引擎抱怨功能不存在?

[英]Why does Rhino Javascript engine complain a function does not exist?

Please forgive me, as I am a Java man dabbling in Javascript business :) 请原谅我,因为我是一名涉足Javascript业务的Java人:)

I wanted to be able to define a set of integration test cases to be easy to script against a Java application. 我希望能够定义一组集成测试用例,以便易于针对Java应用程序编写脚本。 I thought Javascript would be a perfect language to script against. 我认为Javascript将是一种完美的脚本语言。 To that end, I am using the Rhino engine that comes with JDK 7, via Java's Scripting API. 为此,我正在通过Java的Scripting API使用JDK 7附带的Rhino引擎。 The scripts would have access to Java classes already defined in the application, and could be reused to define use case scenarios for integration testing. 这些脚本可以访问应用程序中已经定义的Java类,并且可以重复使用以定义用于集成测试的用例场景。

In the Java application, I have binded the javascript engine itself to the script as jsengine, so that I can load javascript files ( Including a JavaScript file during Rhino eval ). 在Java应用程序中,我将javascript引擎本身绑定为jsengine脚本,以便可以加载javascript文件( 包括Rhino eval期间的JavaScript文件 )。

I have two Javascript files, as defined below: 我有两个Javascript文件,定义如下:

Function.js: Function.js:

function send(msg) {
    send.sendMessage(msg);
}

TestCase.js TestCase.js

jsengine.eval(new java.io.FileReader("Function.js");

sendMsg("Test Message");

I also have the following object defined and binded to the script as "javaobj": 我还定义了以下对象并将其绑定为“ javaobj”脚本:

public class TestConnection {

    ...

    public void send(String message) {
        // Code to send the string message via JMS
    }

}

However, the Rhino engine complains with the following Exception. 但是,Rhino引擎抱怨以下异常。 It seems to not like calling the javaobj's send method, for some reason. 由于某种原因,它似乎不喜欢调用javaobj的send方法。

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function send in object 
function sendMsg(msg) {...}. (TestCase.js#3) in TestCase.js at line number 3
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:212)
    at com.foo.test.scenario.JavaScriptEngine.execute(JavaScriptEngine.java:56)
    at com.foo.test.TestSuite.start(TestSuite.java:88)
    at com.foo.test.TestSuite.main(TestSuite.java:41)
Caused by: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function send in object 
function sendMsg(msg) {...}. (TestCase.js#3) in TestCase.js at line number 3
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3773)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3751)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError(ScriptRuntime.java:3779)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError2(ScriptRuntime.java:3798)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.notFunctionError(ScriptRuntime.java:3869)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2345)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2312)
    at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1524)
    at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:854)
    at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:164)
    at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429)
    at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
    at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3163)
    at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:175)
    at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159)
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214)
    ... 4 more

Has anyone ever encountered this type of issue with Rhino? 有人遇到过Rhino这类问题吗?

PS This question seems related, but no answer given as well ( TypeError in Rhino: migration from Java 6 to Java 7 ) PS这个问题似乎相关,但是也没有给出答案( Rhino中的TypeError:从Java 6到Java 7的迁移

Looks like I found my own answer. 看来我找到了自己的答案。 There was a name conflict between the Javascript function and the name of the binded Java object. Javascript函数和绑定的Java对象的名称之间存在名称冲突。 Both having the same name, the engine tries to call a non-existent method on a Function object! 两者都具有相同的名称,引擎会尝试在Function对象上调用不存在的方法!

Silly me... :P 傻我...:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM