为什么 Rhino 对这个 javascript 不满意?

发布于 2024-09-05 04:35:10 字数 1457 浏览 4 评论 0原文

我已经成功地将我的代码与 ANTLR javascript 目标 中的 javascript 库一起使用一些浏览器,但现在我想在服务器上使用Rhino,但遇到了一些麻烦。我有一些简单的 java 代码,引用了 Rhino 1.7R2 版本的 js-14.jar 文件。

Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
context.evaluateReader(scope, new FileReader("C:\\antlr3-all.js"), "antlr", 1, null);

这会失败,并出现 EcmaError ,其消息是:

TypeError: Cannot call property namespace in object [JavaPackage org.antlr]. 
It is not a function, it is "object". (antlr#259)

它引用的 javascript 行是:

org.antlr.namespace("org.antlr.runtime.tree");

org.antlr.namespace 已在文件的前面声明为函数,因此我我不知道对此有何看法。我也不认为“命名空间”是 javascript 中的保留字,特别是在 Rhino 中。

以下是第 56 行的 org.antlr.namespace 声明:

org.antlr.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=org.antlr.global;

        // ANTLR is implied, so it is ignored if it is included
        for (j=0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

ANTLR javascript 目标页面 提到Rhino 是一个经过测试的平台,所以我想我可能只是误用了Rhino。有人有什么建议吗?

I have been successfully using my code with the javascript library in the ANTLR javascript target in a few browsers, but now I want to use Rhino on the server and I am having some trouble. I have some simple java code that references the Rhino 1.7R2 release's js-14.jar file.

Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
context.evaluateReader(scope, new FileReader("C:\\antlr3-all.js"), "antlr", 1, null);

This fails with an EcmaError whose message is:

TypeError: Cannot call property namespace in object [JavaPackage org.antlr]. 
It is not a function, it is "object". (antlr#259)

The javascript line that it's referring to is:

org.antlr.namespace("org.antlr.runtime.tree");

This org.antlr.namespace was declared as a function earlier in the file, so I am not sure what to think of this. I also don't see that "namespace" is a reserved word in javascript or in Rhino in particular.

Here's the declaration of org.antlr.namespace at line 56:

org.antlr.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=org.antlr.global;

        // ANTLR is implied, so it is ignored if it is included
        for (j=0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

The ANTLR javascript target page mentions that Rhino is a tested platform, so I am thinking that I might just be misusing Rhino. Does anyone have any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-09-12 04:35:10

类型错误:无法调用属性
对象中的命名空间 [JavaPackage
org.antlr]。

它将您的 org.antlr 作为 java 包并尝试调用对象名称空间。所以这样定义函数是行不通的。

单独定义函数命名空间的每个部分对我来说都很有用:

org = new function() {//Define the structure one piece at a time
    this.antlr = new function(){
         this.namespace = ''; 
         return this;
    };
    return this;
};
org.antlr.namespace = function() {print('Help'); return 0;}

抱歉,我无法给出更详细的答案,我对 javascript 不太了解^^。
我想由于 org 和 org.antlr 未定义,因此您无法分配给它们。

TypeError: Cannot call property
namespace in object [JavaPackage
org.antlr].

It takes your org.antlr as a java package and tries to make a call to the object namespace. So defining the function like this does not work.

Defining each part of the functions namespace by itself worked for me:

org = new function() {//Define the structure one piece at a time
    this.antlr = new function(){
         this.namespace = ''; 
         return this;
    };
    return this;
};
org.antlr.namespace = function() {print('Help'); return 0;}

Sorry that I can't give a more detailed answer, I don't know mutch about javascript^^.
I guess that since org and org.antlr are undefined you can't assign to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文