This commit is contained in:
walterhiggins 2014-01-29 20:11:47 +00:00
parent 7457cd58b8
commit 7ab34980e4

View file

@ -56,7 +56,9 @@ module specification, the '.js' suffix is optional.
***/
(function ( rootDir, modulePaths, hooks ) {
var File = java.io.File;
var File = java.io.File,
FileReader = java.io.FileReader,
BufferedReader = java.io.BufferedReader;
var readModuleFromDirectory = function( dir ) {
@ -175,9 +177,19 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
*/
var _loadedModules = {};
var _format = java.lang.String.format;
var _require = function(parentFile, path)
{
var file = resolveModuleToFile(path, parentFile);
/*
require() function implementation
*/
var _require = function( parentFile, path ) {
var file,
canonizedFilename,
moduleInfo,
buffered,
head = '(function(exports,module,require,__filename,__dirname){ ',
code = '',
line = null;
file = resolveModuleToFile(path, parentFile);
if ( !file ) {
var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
"in working directory '%s' ", [path, parentFile.canonicalPath]);
@ -186,22 +198,20 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
}
throw errMsg;
}
var canonizedFilename = _canonize(file);
canonizedFilename = _canonize(file);
var moduleInfo = _loadedModules[canonizedFilename];
moduleInfo = _loadedModules[canonizedFilename];
if ( moduleInfo ) {
return moduleInfo;
}
if (hooks)
if ( hooks ) {
hooks.loading( canonizedFilename );
var reader = new java.io.FileReader(file);
var br = new java.io.BufferedReader(reader);
var code = "";
var r = null;
while ((r = br.readLine()) !== null)
code += r + "\n";
var head = "(function(exports,module,require,__filename,__dirname){ ";
}
buffered = new BufferedReader(new FileReader(file));
while ( (line = buffered.readLine()) !== null ) {
code += line + '\n';
}
buffered.close(); // close the stream so there's no file locks
moduleInfo = {
loaded: false,
@ -209,7 +219,7 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
exports: {},
require: _requireClosure(file.parentFile)
};
var tail = "})";
var tail = '})';
code = head + code + tail;
_loadedModules[canonizedFilename] = moduleInfo;
@ -217,9 +227,9 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
try {
compiledWrapper = eval(code);
} catch (e) {
throw "Error:" + e + " while evaluating module " + canonizedFilename;
throw 'Error:' + e + ' while evaluating module ' + canonizedFilename;
}
var __dirname = "" + file.parentFile.canonicalPath;
var __dirname = '' + file.parentFile.canonicalPath;
var parameters = [
moduleInfo.exports, /* exports */
moduleInfo, /* module */
@ -234,8 +244,9 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
} catch (e) {
throw 'Error:' + e + ' while executing module ' + canonizedFilename;
}
if (hooks)
if ( hooks ) {
hooks.loaded( canonizedFilename );
}
moduleInfo.loaded = true;
return moduleInfo;
};
@ -248,4 +259,3 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
};
return _requireClosure( new java.io.File(rootDir) );
})