On Tue, Nov 17, 2009 at 6:54 AM, woody <sdwood
...@googlemail.com> wrote:
> Hi All,
> I've been running env js from Rhino embedded within Java for some
> time. I've had problems due to EnvJs's reliance upon functions
> provided by the Rhino shell (e.g. sync, print, load etc). I was
> mocking these out myself, however after some investigation it emerges
> it's dead easy to get these functions into your context created from
> Java - here's what I had before :
> Context cx = ContextFactory.getGlobal().enterContext();
> // set Rhino to work in interpreted mode, i.e. don't try and
> // compile the JavaScript source into Java, some of it exceeds the
> // Java 64kb method size limit.
> cx.setOptimizationLevel(-1);
> cx.setLanguageVersion(Context.VERSION_1_5);
> Scriptable scope = cx.initStandardObjects();
> And here's what I needed to change it to to get the global shell
> functions :
> Global global = new Global();
> Context cx = ContextFactory.getGlobal().enterContext();
> global.init(cx);
> // set Rhino to work in interpreted mode, i.e. don't try and
> // compile the JavaScript source into Java, some of it exceeds the
> // Java 64kb method size limit.
> cx.setOptimizationLevel(-1);
> cx.setLanguageVersion(Context.VERSION_1_5);
> Scriptable scope = cx.initStandardObjects(global);
> global is in org.mozilla.javascript.tools.shell.Global
> The variable "scope" is the thing you want to interpret your
> JavaScript files against using context.evaluateReader. e.g. having
> done the above I then do :
> cx.evaluateReader(scope, new FileReader("test.js"), "test.js", 1,
> null);
> where test.js is a file that could be :
> (function () {
> load("env-rhino.js");
> window.djConfig = {
> locale : "en_GB",
> baseUrl : "foo/dojo/"
> };
> load("path/to/foo/dojo/dojo.js");
> window.location = "test.html";
> var nl = dojo.query("div.myClass");
> print("There are "+nl.length+" divs with 'myClass'");
> }) ();
> Note how it uses "load" and "print" :) Hopefully this is of use to
> other people trying to achieve the same thing. Chris - i can do a
> more detailed write up if you want to include in the section on the
> website under the "embed" section of the docs.
> cheers,
> Steve.
> --
> You received this message because you are subscribed to the Google Groups
> "Env.js" group.
> To post to this group, send email to envjs@googlegroups.com.
> To unsubscribe from this group, send email to
> envjs+unsubscribe@googlegroups.com <envjs%2Bunsubscribe@googlegroups.com>.
> For more options, visit this group at
> http://groups.google.com/group/envjs?hl=.