Why is Rhino unhappy with this javascript?

I have successfully used my javascript library code in the ANTLR javascript target in several browsers, but now I want to use Rhino on the server and I am having problems. I have simple Java code that references a js-14.jar file of Rhino version 1.7R2.

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

This does not work with EcmaError whose message is:

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

In the javascript line it refers to is:

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

This org.antlr.namespace was declared as a function earlier in the file, so I'm not sure what to think about it. I also do not see that the "namespace" is a reserved word in javascript or in Rhino in particular.

Here's the declaration of org.antlr.namespace on 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 landing page mentions that Rhino is a proven platform, so I think I can just abuse Rhino. Does anyone have any clues?

+4
source share
1 answer

TypeError: Unable to call the namespace property on the [JavaPackage org.antlr] object.

It takes your org.antlr as a java package and tries to make a call to the object namespace. Therefore, the definition of such a function does not work.

Defining each part of the function 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 not give a more detailed answer, I do not know mutch about javascript ^^. I assume that since org and org.antlr are undefined, you cannot assign them.

+5
source

Source: https://habr.com/ru/post/1312363/


All Articles