don't echo result if undefined/null

This commit is contained in:
walterhiggins 2014-04-28 23:29:53 +01:00
parent 3700c11223
commit dc812c934c
2 changed files with 23 additions and 13 deletions

View file

@ -2,5 +2,8 @@ exports.isJavaObject = function( o ) {
if (o === global){ if (o === global){
return false; return false;
} }
if (o !== undefined && o !== null){
return o.getClass ? true : false;
}
return o instanceof java.lang.Object; return o instanceof java.lang.Object;
}; };

View file

@ -651,25 +651,32 @@ function __onEnable ( __engine, __plugin, __script )
global.self = sender; global.self = sender;
global.__engine = __engine; global.__engine = __engine;
try { try {
// cannot rely on native eval in jre7 and jre8
// because ...
// js var hearts
// js hearts
// ... throws an execption ('hearts' is not defined). vars are not sticky in native eval .
//
jsResult = __engine.eval( fnBody ); jsResult = __engine.eval( fnBody );
if ( typeof jsResult != 'undefined' ) { if ( typeof jsResult != 'undefined' ) {
if ( jsResult == null) { if ( jsResult == null) {
sender.sendMessage('(null)'); // engine eval will return null even if the result should be undefined
// this can be confusing so I think it's better to omit output for this case
// sender.sendMessage('(null)');
} else { } else {
try { try {
if ( isJavaObject(jsResult) || typeof jsResult === 'function') { if ( isJavaObject(jsResult) || typeof jsResult === 'function') {
sender.sendMessage(jsResult); sender.sendMessage(jsResult);
} else { } else {
var replacer = function replacer(key, value){ var replacer = function replacer(key, value){
return this[key] instanceof java.lang.Object ? '' + this[key] : value; return this[key] instanceof java.lang.Object ? '' + this[key] : value;
}; };
sender.sendMessage( JSON.stringify( jsResult, replacer, 2) ); sender.sendMessage( JSON.stringify( jsResult, replacer, 2) );
} }
} catch ( displayError ) { } catch ( displayError ) {
logger.severe( 'Error while trying to display result: ' + jsResult + ', Error: '+ displayError ); logger.severe( 'Error while trying to display result: ' + jsResult + ', Error: '+ displayError );
} }
} }
} }
} catch ( e ) { } catch ( e ) {