X-BeenThere: envjs@googlegroups.com Received: by 10.86.239.10 with SMTP id m10ls404924fgh.0.p; Tue, 17 Nov 2009 15:09:33 -0800 (PST) Received: by 10.86.10.12 with SMTP id 12mr23236fgj.29.1258499373412; Tue, 17 Nov 2009 15:09:33 -0800 (PST) Received: by 10.86.10.12 with SMTP id 12mr23235fgj.29.1258499373374; Tue, 17 Nov 2009 15:09:33 -0800 (PST) Return-Path: Received: from mail-fx0-f228.google.com (mail-fx0-f228.google.com [209.85.220.228]) by gmr-mx.google.com with ESMTP id 11si791971fxm.2.2009.11.17.15.09.32; Tue, 17 Nov 2009 15:09:32 -0800 (PST) Received-SPF: pass (google.com: domain of thatcher.christop...@gmail.com designates 209.85.220.228 as permitted sender) client-ip=209.85.220.228; Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of thatcher.christop...@gmail.com designates 209.85.220.228 as permitted sender) smtp.mail=thatcher.christop...@gmail.com; dkim=pass (test mode) header...@gmail.com Received: by mail-fx0-f228.google.com with SMTP id 28so604196fxm.25 for ; Tue, 17 Nov 2009 15:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=1uJDF4P2l1UmBeWYllHNc+ONczBCdJdYsPcGHlRcwic=; b=Oh+qJESZSPjGdgfqPOe4qEPGbq/o5y4aLewJwJCKK/rCPZ5HV6/ll3thBtfy2dE+uu 04I7xZVSMivKPNVW0qzOHKZUKS1iWsFRSbT97DQAOpiu9/O9/jL03Hk3zxZ/8Z1w1ED4 QZNSiwNko7U2mkQT/MrFaMleXz2Y9kNbFcFXc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=ffpIcjp8ROeU6YHQ4A1bMJIx6dFM4r8hqLwdhzf1uFniiKQJn20hQ+iVNGtWkd7KJ6 ZrBINWIv5FEqYxlZJn8iqP2DfxQEmdzNNKgc4+YKx1oQSm4/kUxvllsUPO8P9SICbk3C SHzk5Zd2VEUVSQQjXgCdlzJVsQ5zz6YOx3VIs= MIME-Version: 1.0 Received: by 10.213.51.140 with SMTP id d12mr902213ebg.9.1258499371688; Tue, 17 Nov 2009 15:09:31 -0800 (PST) In-Reply-To: References: Date: Tue, 17 Nov 2009 18:09:31 -0500 Message-ID: Subject: Re: [env-js] Embedding EnvJs - how to get the Rhino Shell functions From: chris thatcher To: envjs@googlegroups.com Content-Type: multipart/alternative; boundary=00163600cf8cffe4a10478993763 --00163600cf8cffe4a10478993763 Content-Type: text/plain; charset=UTF-8 That would be great! On Tue, Nov 17, 2009 at 6:54 AM, woody 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 . > For more options, visit this group at > http://groups.google.com/group/envjs?hl=. > > > -- Christopher Thatcher --00163600cf8cffe4a10478993763 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable That would be great!

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. =C2=A0I've had problems due to EnvJs's reliance upon function= s
provided by the Rhino shell (e.g. sync, print, load etc). =C2=A0I 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 :

=C2=A0Context cx =3D ContextFactory.getGlobal().enterContext();
=C2=A0// set Rhino to work in interpreted mode, i.e. don't try and
=C2=A0// compile the JavaScript source into Java, some of it exceeds the =C2=A0// Java 64kb method size limit.
=C2=A0cx.setOptimizationLevel(-1);
=C2=A0cx.setLanguageVersion(Context.VERSION_1_5);
=C2=A0Scriptable scope =3D cx.initStandardObjects();


And here's what I needed to change it to to get the global shell
functions :

Global global =3D new Global();
Context cx =3D 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 =3D 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 =C2=A0:

(function () {

=C2=A0 =C2=A0load("env-rhino.js");
=C2=A0 =C2=A0window.djConfig =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0locale : "en_GB",
=C2=A0 =C2=A0 =C2=A0 =C2=A0baseUrl : "foo/dojo/"
=C2=A0 =C2=A0};

=C2=A0 =C2=A0load("path/to/foo/dojo/dojo.js");

=C2=A0 =C2=A0window.location =3D "test.html";

=C2=A0 =C2=A0var nl =3D dojo.query("div.myClass");
=C2=A0 =C2=A0print("There are "+nl.length+" divs with '= myClass'");


}) ();

Note how it uses "load" and "print" :) Hopefully this i= s of use to
other people trying to achieve the same thing. =C2=A0Chris - 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 &= quot;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.
For more options, visit this group at http://groups.google.com/group/envjs?hl= =3D.





--
Christopher That= cher