为什么 Rhino 对这个 javascript 不满意?
我已经成功地将我的代码与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将您的 org.antlr 作为 java 包并尝试调用对象名称空间。所以这样定义函数是行不通的。
单独定义函数命名空间的每个部分对我来说都很有用:
抱歉,我无法给出更详细的答案,我对 javascript 不太了解^^。
我想由于 org 和 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:
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.