ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Roodt <gro...@gmail.com>
Subject Re: Rhino global.load() in script context
Date Sat, 21 Aug 2010 21:37:29 GMT
This might work for you:

    <project default="hello" name="helloworld" basedir=".">
       <target name="hello">
           <script language="javascript" manager="bsf">
           <classpath>
               <fileset dir="rhino-lib" includes="*.jar"></fileset>
           </classpath><![CDATA[
           importPackage(java.lang, java.util, java.io);
           System.out.println("Hello from JavaScript!!");
           //create shell, execute something and grab global
           var shell = org.mozilla.javascript.tools.shell.Main;
           var args = ["-e","var a='STRING';"];
            shell.exec(args);
           var shellGlobal = shell.global;

           //grab functions from shell global and place in current global
           var load=shellGlobal.load;
           var print=shellGlobal.print;
           var defineClass=shellGlobal.defineClass;
           var deserialize=shellGlobal.deserialize;
           var doctest=shellGlobal.doctest;
           var gc=shellGlobal.gc;
           var help=shellGlobal.help;
           var loadClass=shellGlobal.loadClass;
           var quit=shellGlobal.quit;
           var readFile=shellGlobal.readFile;
           var readUrl=shellGlobal.readUrl;
           var runCommand=shellGlobal.runCommand;
           var seal=shellGlobal.seal;
           var serialize=shellGlobal.serialize;
           var spawn=shellGlobal.spawn;
           var sync=shellGlobal.sync;
           var toint32=shellGlobal.toint32;
           var version=shellGlobal.version;
           var environment=shellGlobal.environment;

           //test your bad self
           load("test.js");

           ]]></script>
       </target>
    </project>

test.js:
var a = function() {
print("test");
help();
var scriptContents = readFile("test.js");
print(scriptContents);
var ver = version();
print("version:"+ver);
print(this);
for(var prop in this){
print(prop);
}
}
a();



On Sat, Aug 21, 2010 at 7:03 PM, Jacob Beard <jbeard4@cs.mcgill.ca> wrote:

> Hi Greg,
>
> Thanks for your response. Replies below:
>
>
> On 10-08-21 01:41 PM, Greg Roodt wrote:
>
>> I believe load() is part of Rhino Shell. I think all that the<script />
>> task runs when using JavaScript is the interpreter. It would only have the
>> pure Javascript standard language features (and a few bits and pieces to
>> interact with Java and the execution context).
>>
>>
> load() is normally exposed as part of the global object when running Rhino,
> in the shell or the interpreter. All the js module loaders that support
> Rhino that I've encountered, including RequireJS and dojo, make use of
> load() to load JavaScript modules.
>
>  It might be easier to run the shell for each test? Like so:
>> java org.mozilla.javascript.tools.shell.Main [options]
>> script-filename-or-url [script-arguments]
>> https://developer.mozilla.org/en/Rhino_Shell#Invoking_the_Shell
>>
>> Or like John Resig does with env.js:
>> http://ejohn.org/blog/bringing-the-browser-to-the-server/
>>
>>
> I'm using that technique for other parts of my code, but it would be much
> easier to simply hook into Ant's ResourceSet data structures for this part,
> as it's possible to register a number of unit tests with dojo before running
> them.
>
>
>  Or maybe, define your own global load() function inside the<script />
>>  tag?
>>
>>
> That's what I'm working on. This seems to work, but I still need to test it
> with the dojo module loader:
>
>        <script language="javascript" manager="bsf">
>
>            <classpath>
>
>                <fileset dir="../../../lib/java/" includes="js.jar"/>
>
>                <fileset dir="../../../lib/build-java/"
> includes="*.jar"></fileset>
>
>            </classpath><![CDATA[
>
>            //define load in global scope
>
>            function readFile(path){
>
>                stream = new java.io.FileInputStream(new
> java.io.File(path));
>
>                fc = stream.getChannel();
>
>                bb = fc.map(java.nio.channels.FileChannel.MapMode.READ_ONLY,
> 0, fc.size());
>
>                return
> java.nio.charset.Charset.defaultCharset().decode(bb).toString();
>
>            }
>
>            load = function(path){
>
>                eval(String(readFile(path)))
>
>            }
>
>            echo = helloworld.createTask("echo");
>
>            var contents = readFile('hello.js')
>
>            echo.setMessage(contents);
>
>            echo.perform();
>
>            load('hello.js')
>
>            echo.perform();
>
>        ]]></script>
>
> hello.js:
>
> echo.setMessage("hello world!");
>
>
> Outputs:
>
> hello:
>
>     [echo] echo.setMessage("hello world!");
>
>     [echo] hello world!
>
>
>
> Thanks,
>
> Jake
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message