fix issue #113
This commit is contained in:
parent
7457cd58b8
commit
7ab34980e4
1 changed files with 85 additions and 75 deletions
|
@ -56,7 +56,9 @@ module specification, the '.js' suffix is optional.
|
||||||
***/
|
***/
|
||||||
(function ( rootDir, modulePaths, hooks ) {
|
(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 ) {
|
var readModuleFromDirectory = function( dir ) {
|
||||||
|
|
||||||
|
@ -175,33 +177,41 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
|
||||||
*/
|
*/
|
||||||
var _loadedModules = {};
|
var _loadedModules = {};
|
||||||
var _format = java.lang.String.format;
|
var _format = java.lang.String.format;
|
||||||
var _require = function(parentFile, path)
|
/*
|
||||||
{
|
require() function implementation
|
||||||
var file = resolveModuleToFile(path, parentFile);
|
*/
|
||||||
if (!file){
|
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' " +
|
var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
|
||||||
"in working directory '%s' ", [path, parentFile.canonicalPath]);
|
"in working directory '%s' ", [path, parentFile.canonicalPath]);
|
||||||
if (! ( (''+path).match(/^\./) )){
|
if (! ( (''+path).match( /^\./ ) ) ) {
|
||||||
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
|
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
|
||||||
}
|
}
|
||||||
throw errMsg;
|
throw errMsg;
|
||||||
}
|
}
|
||||||
var canonizedFilename = _canonize(file);
|
canonizedFilename = _canonize(file);
|
||||||
|
|
||||||
var moduleInfo = _loadedModules[canonizedFilename];
|
moduleInfo = _loadedModules[canonizedFilename];
|
||||||
if (moduleInfo){
|
if ( moduleInfo ) {
|
||||||
return moduleInfo;
|
return moduleInfo;
|
||||||
}
|
}
|
||||||
if (hooks)
|
if ( hooks ) {
|
||||||
hooks.loading(canonizedFilename);
|
hooks.loading( canonizedFilename );
|
||||||
var reader = new java.io.FileReader(file);
|
}
|
||||||
var br = new java.io.BufferedReader(reader);
|
buffered = new BufferedReader(new FileReader(file));
|
||||||
var code = "";
|
while ( (line = buffered.readLine()) !== null ) {
|
||||||
var r = null;
|
code += line + '\n';
|
||||||
while ((r = br.readLine()) !== null)
|
}
|
||||||
code += r + "\n";
|
buffered.close(); // close the stream so there's no file locks
|
||||||
|
|
||||||
var head = "(function(exports,module,require,__filename,__dirname){ ";
|
|
||||||
|
|
||||||
moduleInfo = {
|
moduleInfo = {
|
||||||
loaded: false,
|
loaded: false,
|
||||||
|
@ -209,17 +219,17 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
|
||||||
exports: {},
|
exports: {},
|
||||||
require: _requireClosure(file.parentFile)
|
require: _requireClosure(file.parentFile)
|
||||||
};
|
};
|
||||||
var tail = "})";
|
var tail = '})';
|
||||||
code = head + code + tail;
|
code = head + code + tail;
|
||||||
|
|
||||||
_loadedModules[canonizedFilename] = moduleInfo;
|
_loadedModules[canonizedFilename] = moduleInfo;
|
||||||
var compiledWrapper = null;
|
var compiledWrapper = null;
|
||||||
try {
|
try {
|
||||||
compiledWrapper = eval(code);
|
compiledWrapper = eval(code);
|
||||||
}catch (e){
|
} 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 = [
|
var parameters = [
|
||||||
moduleInfo.exports, /* exports */
|
moduleInfo.exports, /* exports */
|
||||||
moduleInfo, /* module */
|
moduleInfo, /* module */
|
||||||
|
@ -231,21 +241,21 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
|
||||||
compiledWrapper
|
compiledWrapper
|
||||||
.apply(moduleInfo.exports, /* this */
|
.apply(moduleInfo.exports, /* this */
|
||||||
parameters);
|
parameters);
|
||||||
} catch (e){
|
} catch (e) {
|
||||||
throw 'Error:' + e + ' while executing module ' + canonizedFilename;
|
throw 'Error:' + e + ' while executing module ' + canonizedFilename;
|
||||||
}
|
}
|
||||||
if (hooks)
|
if ( hooks ) {
|
||||||
hooks.loaded(canonizedFilename);
|
hooks.loaded( canonizedFilename );
|
||||||
|
}
|
||||||
moduleInfo.loaded = true;
|
moduleInfo.loaded = true;
|
||||||
return moduleInfo;
|
return moduleInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
var _requireClosure = function(parent){
|
var _requireClosure = function( parent ) {
|
||||||
return function(path){
|
return function( path ) {
|
||||||
var module = _require(parent, path);
|
var module = _require( parent, path );
|
||||||
return module.exports;
|
return module.exports;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
return _requireClosure(new java.io.File(rootDir));
|
return _requireClosure( new java.io.File(rootDir) );
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Reference in a new issue