views:

238

answers:

1

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?

+2  A: 

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.

josefx
Thanks... Your pre-edited answer gave me the hint. The first part of the antlr script file does `if (typeof org == "undefined" || !org) { var org = {}; }`. Apparently org wasn't really undefined, so it didn't get created as an empty javascript object. Removing the `if` around the initialization seems to work.
Chris Farmer