Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File I/O using Rhino/SpiderMonkey

1,934 views
Skip to first unread message

JJ Merelo

unread,
Dec 28, 2006, 3:35:32 PM12/28/06
to
Hi,
This is probably quite straightforward, but I haven't found a way to
operate with files from stand-alone (i.e. outside the browser) Rhino or
SpiderMonkey scripts. I've seen a File class in JSLIB, which doesn't
work in SM, and I haven't found the way to make it work in Rhino.
I haven't found either a tutorial or manual for using JS with any of
those interpreters (or, for that matter, KJS). Any help here?

Thanks!

JJ

Mook

unread,
Dec 28, 2006, 5:20:31 PM12/28/06
to
If you build SpiderMonkey yourself, you can get a mostly-working but
sorta broken File global constructor/object thing if you build with
JS_HAS_FILE_OBJECT (which also requires NSPR).

I've got some random jsdoc at http://mook.moz.googlepages.com/File.html
but I'm not sure if it's all correct.

Be aware that if you do this for, say, a build of Firefox, then every
page would also have access to this :)

...This, of course, still is no tutorial.
http://developer.mozilla.org/en/docs/Introduction_to_the_JavaScript_shell
might help though...

--
Mook
mook dot moz plus stuff at gmail dot more stuff

JJ Merelo

unread,
Dec 29, 2006, 6:52:26 AM12/29/06
to
HI,

Mook ha escrito:


> If you build SpiderMonkey yourself, you can get a mostly-working but
> sorta broken File global constructor/object thing if you build with
> JS_HAS_FILE_OBJECT (which also requires NSPR).
>

I have the Ubuntu build, probably from ultraverse (or suchlike). What
do you mean by mostly-working but sorta broken? And how do I find out
if that's been the case with the build? string /usr/bin/smjs?

> I've got some random jsdoc at http://mook.moz.googlepages.com/File.html
> but I'm not sure if it's all correct.

There's a broken link, but I gather from it that there must be a
File.js file lying somewhere; it's not included with the smjs packet.
Any idea?

> Be aware that if you do this for, say, a build of Firefox, then every
> page would also have access to this :)

My intention is to use JS stand-alone, mostly.

Thanks for the answers!

JJ

Attila Szegedi

unread,
Dec 29, 2006, 11:15:00 AM12/29/06
to dev-tech-...@lists.mozilla.org
ECMAScript doesn't specify any standardized I/O library. In Rhino, you can
use java.io.* classes directly, i.e. you can do stuff like

var f1 = new java.io.File("/home/bob/file1.txt");
var f2 = new java.io.File("/home/bob/file2.txt");
f1.renameTo(f2);

Additionally, you can assign often-used class objects to shorter-named
variables:

var File = java.io.File;
var f1 = new File("/home/bob/file1.txt");
var f2 = new File("/home/bob/file2.txt");
f1.renameTo(f2);

You should be able to read write files too:

var w = new java.io.FileWriter("/home/bob/hello.txt");
try
{
w.write("Hello world\n");
}
finally
{
w.close();
}

etc.

Of course, this pretty much ties your JavaScript code to Rhino.
Attila.

> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>

--
home: http://www.szegedi.org
weblog: http://constc.blogspot.com
Visit Szegedi Butterfly fractals at:
http://www.szegedi.org/fractals/butterfly/index.html

JJ Merelo

unread,
Jan 2, 2007, 2:30:46 PM1/2/07
to

Attila Szegedi ha escrito:

> ECMAScript doesn't specify any standardized I/O library. In Rhino, you can
> use java.io.* classes directly, i.e. you can do stuff like
>
> var f1 = new java.io.File("/home/bob/file1.txt");
> var f2 = new java.io.File("/home/bob/file2.txt");
> f1.renameTo(f2);
>
> Additionally, you can assign often-used class objects to shorter-named
> variables:
>
> var File = java.io.File;
> var f1 = new File("/home/bob/file1.txt");
> var f2 = new File("/home/bob/file2.txt");
> f1.renameTo(f2);
>
> You should be able to read write files too:
>
> var w = new java.io.FileWriter("/home/bob/hello.txt");
> try
> {
> w.write("Hello world\n");
> }
> finally
> {
> w.close();
> }

Thanks a lot, it seems to work!

JJ

sha...@shantirao.com

unread,
Jan 3, 2007, 12:45:01 AM1/3/07
to jjme...@gmail.com
Hi JJ,

You might try a 3rd party SpiderMonkey shell. These work on Linux and
Windows, and there are probably others. Maybe somebody could add some
links to http://www.mozilla.org/js/projects.html.

JSDB: www.jsdb.org
wxJS: http://users.skynet.be/saw/wxJS/

Shanti

JJ Merelo

unread,
Jan 3, 2007, 2:03:43 PM1/3/07
to
Finally, I managed to make this work:
--
var FileReader = java.io.FileReader;
var BufferedReader =java.io.BufferedReader;

var file_name = arguments[0];

var f = new FileReader(file_name);
var br = new BufferedReader( f );
var resultados= new Array;
var line = new String;
while ((line = br.readLine()) != null) {
var estaLinea = new String( line );
var resultado = estaLinea.split(" ");
switch (resultado[2]) {
case '1':
if ( resultados[resultado[0]] ) {
resultados[resultado[0]]+=3;
} else {
resultados[resultado[0]]=3;
}
break;
case 'x':
if ( resultados[resultado[0]] ) {
resultados[resultado[0]]+=1;
} else {
resultados[resultado[0]]=1;
}
if ( resultados[resultado[1]] ) {
resultados[resultado[1]]+=1;
} else {
resultados[resultado[1]]=1;
}
break;
default:
if ( resultados[resultado[1]] ) {
resultados[resultado[1]]+=3;
} else {
resultados[resultado[1]]=3;
}
break
}
}

for ( var i in resultados ) {
print( i + ": " + resultados[i])
}


--
It's a bit complicated, but I guess reading files in Java is. The
tricky part was figuring out that Strings in Java are _not_ Strings in
JS, but it's a straightforward conversion.

JJ

franck.fr

unread,
Jan 4, 2007, 5:05:57 AM1/4/07
to
and jslibs : http://code.google.com/p/jslibs/

Soubok

On Jan 3, 6:45 am, sha...@shantirao.com wrote:
> Hi JJ,
>
> You might try a 3rd party SpiderMonkey shell. These work on Linux and
> Windows, and there are probably others. Maybe somebody could add some

> links tohttp://www.mozilla.org/js/projects.html.

0 new messages