Show correct lineNumber in require() errors on Nashorn (JRE8)

This commit is contained in:
walterhiggins 2014-03-13 22:49:03 +00:00
parent fce28567e6
commit b7352ed962
2 changed files with 28 additions and 16 deletions

View file

@ -60,7 +60,7 @@ module specification, the '.js' suffix is optional.
[cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1. [cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1.
***/ ***/
(function ( rootDir, modulePaths, hooks ) { (function ( rootDir, modulePaths, hooks, evaluate ) {
var File = java.io.File, var File = java.io.File,
FileReader = java.io.FileReader, FileReader = java.io.FileReader,
@ -155,7 +155,7 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
resolvedFile = new File(modulePaths[i] + moduleName + '.js'); resolvedFile = new File(modulePaths[i] + moduleName + '.js');
if ( resolvedFile.exists() ) { if ( resolvedFile.exists() ) {
return resolvedFile; return resolvedFile;
} }
} }
} }
} else { } else {
@ -173,7 +173,7 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
file = new File(pathWithJSExt); file = new File(pathWithJSExt);
if ( file.exists() ) { if ( file.exists() ) {
return file; return file;
} }
} }
} }
@ -187,18 +187,18 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
*/ */
var _require = function( parentFile, path, options ) { var _require = function( parentFile, path, options ) {
var file, var file,
canonizedFilename, canonizedFilename,
moduleInfo, moduleInfo,
buffered, buffered,
head = '(function(exports,module,require,__filename,__dirname){ ', head = '(function(exports,module,require,__filename,__dirname){ ',
code = '', code = '',
line = null; line = null;
if ( typeof options == 'undefined' ) { if ( typeof options == 'undefined' ) {
options = { cache: true }; options = { cache: true };
} else { } else {
if ( typeof options.cache == 'undefined' ) { if ( typeof options.cache == 'undefined' ) {
options.cache = true; options.cache = true;
} }
} }
@ -216,7 +216,7 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
moduleInfo = _loadedModules[canonizedFilename]; moduleInfo = _loadedModules[canonizedFilename];
if ( moduleInfo ) { if ( moduleInfo ) {
if ( options.cache ) { if ( options.cache ) {
return moduleInfo; return moduleInfo;
} }
} }
if ( hooks ) { if ( hooks ) {
@ -242,8 +242,12 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
} }
var compiledWrapper = null; var compiledWrapper = null;
try { try {
compiledWrapper = eval(code); compiledWrapper = evaluate(code);
} catch (e) { } catch (e) {
/*
wph 20140313 JRE8 (nashorn) gives misleading linenumber of evaluating code not evaluated code.
This can be fixed by instead using __engine.eval
*/
throw new Error( "Error evaluating module " + path throw new Error( "Error evaluating module " + path
+ " line #" + e.lineNumber + " line #" + e.lineNumber
+ " : " + e.message, canonizedFilename, e.lineNumber ); + " : " + e.message, canonizedFilename, e.lineNumber );

View file

@ -428,7 +428,9 @@ function __onEnable ( __engine, __plugin, __script )
BufferedReader = java.io.BufferedReader, BufferedReader = java.io.BufferedReader,
PrintWriter = java.io.PrintWriter, PrintWriter = java.io.PrintWriter,
FileWriter = java.io.FileWriter; FileWriter = java.io.FileWriter;
var debug = function(msg){
java.lang.System.out.println('DEBUG:' + msg);
};
var _canonize = function( file ) { var _canonize = function( file ) {
return '' + file.getCanonicalPath().replaceAll( '\\\\', '/' ); return '' + file.getCanonicalPath().replaceAll( '\\\\', '/' );
}; };
@ -457,14 +459,13 @@ function __onEnable ( __engine, __plugin, __script )
out.close(); out.close();
}; };
/* /*
make sure eval is present make sure eval is present: it's present on JRE 6, 7, and 8 on Linux
*/ */
if ( typeof eval == 'undefined' ) { if ( typeof eval == 'undefined' ) {
global.eval = function( str ) { global.eval = function( str ) {
return __engine.eval( str ); return __engine.eval( str );
}; };
} }
/* /*
Load the contents of the file and evaluate as javascript Load the contents of the file and evaluate as javascript
*/ */
@ -593,7 +594,14 @@ function __onEnable ( __engine, __plugin, __script )
} }
} }
}; };
global.require = configRequire( jsPluginsRootDirName, modulePaths, requireHooks ); global.require = configRequire(
jsPluginsRootDirName,
modulePaths,
requireHooks,
function(code){
return __engine.eval(code);
}
);
require('js-patch')( global ); require('js-patch')( global );
global.console = require('console'); global.console = require('console');