load(["foo.js", ...]) Load JavaScript source files named by
string arguments.
Good enough for a start - you can write some library functions/objects/whatever and load them as needed. A far cry from what one would really like to have, which is some sort of module framework, but still. What's sorely missing is some facility for autoloading some initialization code on startup, I haven't yet figured out how to do that. Maybe later.
To load data from files, there is
readFile(fileName [, encoding])
Returns the content of the file as a string.
Encoding of the string can be optionally specified.
readUrl(url [, encoding])
Similar to readFile, reads the contents of the url.
Not bad - but there are still many times when you would want to read the list of file names in a directory, optionally filtered by a wildcard pattern (or better - a regexp) and process them in a loop, or something. So let's get started on a stdio object, containing a readDir function and with time, hopefully, more of the missing file i/o functionality:
this.stdio = {(Ok, trying to format code in Blogger is painful).
get moduleName() { return 'stdio'; },
readDir : /* string[] */
function ( /* string */ path,
/* opt RegExp */ filterRx
)
{
var f = new java.io.File( path );
var jlist = f.list(); // a java String[]
if ( jlist === null )
{
return null; // path is not a directory
}
var list = [];
if ( arguments.length > 1 )
{
list = jlist.filter( RegExp.prototype.test, filterRx );
} else
{
list = [].concat( jlist );
}
list = list.map(String);
return list; // a js array of js strings
}
};
Note that the job gets done by reaching into a Java class (java.io.File). But what Java code returns are Java (not Javascript) data types. So I put some effort into conversion: a handy way to convert a Java array into a Javascript one is by concating it with the empty array; but at that point, the array still contains Java strings (represented by somewhat opaque JS objects), in practice this means the entries don't have quite the same methods you would expect in JS strings. This is fixed by applying the Javascript String function, which (to remind you) when called not as a constructor, essentially calls the toString method of its argument.