Fix issue #112 (Support for Nashorn in Java8)

This commit is contained in:
walterhiggins 2014-01-13 21:06:17 +00:00
parent 2e9ce31713
commit 7cb679cfd1
8 changed files with 51 additions and 56 deletions

View file

@ -25,8 +25,8 @@ Walter Higgins
* [Global functions](#global-functions) * [Global functions](#global-functions)
* [echo function](#echo-function) * [echo function](#echo-function)
* [require() function](#require-function) * [require() function](#require-function)
* [load() function](#load-function) * [scload() function](#scload-function)
* [save() function](#save-function) * [scsave() function](#scsave-function)
* [plugin() function](#plugin-function) * [plugin() function](#plugin-function)
* [command() function](#command-function) * [command() function](#command-function)
* [setTimeout() function](#settimeout-function) * [setTimeout() function](#settimeout-function)
@ -348,11 +348,11 @@ to load the named module.
require() will return the loaded module's exports. require() will return the loaded module's exports.
### load() function ### scload() function
#### No longer recommended for use by Plugin/Module developers (deprecated) #### No longer recommended for use by Plugin/Module developers (deprecated)
load() should only be used to load .json data. scload() should only be used to load .json data.
#### Parameters #### Parameters
@ -361,13 +361,13 @@ load() should only be used to load .json data.
#### Returns #### Returns
load() will return the result of the last statement evaluated in the file. scload() will return the result of the last statement evaluated in the file.
#### Example #### Example
load("myFile.js"); // loads a javascript file and evaluates it. scload("myFile.js"); // loads a javascript file and evaluates it.
var myData = load("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. var myData = scload("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned.
##### myData.json contents... ##### myData.json contents...
@ -380,15 +380,15 @@ load() will return the result of the last statement evaluated in the file.
} }
} }
### save() function ### scsave() function
The save() function saves an in-memory javascript object to a The scsave() function saves an in-memory javascript object to a
specified file. Under the hood, save() uses JSON (specifically specified file. Under the hood, scsave() uses JSON (specifically
json2.js) to save the object. Again, there will usually be no need to json2.js) to save the object. Again, there will usually be no need to
call this function directly as all javascript plugins' state are saved call this function directly as all javascript plugins' state are saved
automatically if they are declared using the `plugin()` function. Any automatically if they are declared using the `plugin()` function. Any
in-memory object saved using the `save()` function can later be in-memory object saved using the `scsave()` function can later be
restored using the `load()` function. restored using the `scload()` function.
#### Parameters #### Parameters
@ -400,7 +400,7 @@ restored using the `load()` function.
var myObject = { name: 'John Doe', var myObject = { name: 'John Doe',
aliases: ['John Ray', 'John Mee'], aliases: ['John Ray', 'John Mee'],
date_of_birth: '1982/01/31' }; date_of_birth: '1982/01/31' };
save(myObject, 'johndoe.json'); scsave(myObject, 'johndoe.json');
##### johndoe.json contents... ##### johndoe.json contents...

View file

@ -1,3 +1,8 @@
# 2014 01 13
Bug Fix: Make ScriptCraft work with Nashorn javascript engine bundled with Java 8
<https://github.com/walterhiggins/ScriptCraft/issues/112>
# 2014 01 12 # 2014 01 12
## Important ## Important

View file

@ -93,11 +93,7 @@ exports.on = function(
priority = bkEvent.EventPriority[priority]; priority = bkEvent.EventPriority[priority];
} }
if (typeof eventType == "string"){ if (typeof eventType == "string"){
var subPkgs = eventType.split('.'); eventType = eval('org.bukkit.event.' + eventType);
eventType = bkEvent[subPkgs[0]];
for (var i = 1;i < subPkgs.length; i++){
eventType = eventType[subPkgs[i]];
}
} }
var handlerList = eventType.getHandlerList(); var handlerList = eventType.getHandlerList();
var listener = {}; var listener = {};

View file

@ -13,7 +13,7 @@ module.exports = function( rootDir, $ ){
if (typeof write == 'undefined') if (typeof write == 'undefined')
write = false; write = false;
if (!write){ if (!write){
dataFromFile = $.load(_dataDir.canonicalPath + '/' + name + '-store.json'); dataFromFile = $.scload(_dataDir.canonicalPath + '/' + name + '-store.json');
if (dataFromFile){ if (dataFromFile){
for (i in dataFromFile){ for (i in dataFromFile){
data[i] = dataFromFile[i]; data[i] = dataFromFile[i];
@ -21,7 +21,7 @@ module.exports = function( rootDir, $ ){
} }
}else{ }else{
// flush data to file // flush data to file
$.save(data, _dataDir.canonicalPath + '/' + name + '-store.json'); $.scsave(data, _dataDir.canonicalPath + '/' + name + '-store.json');
} }
_persistentData[name] = data; _persistentData[name] = data;
return data; return data;
@ -30,7 +30,7 @@ module.exports = function( rootDir, $ ){
$.addUnloadHandler(function(){ $.addUnloadHandler(function(){
for (var name in _persistentData){ for (var name in _persistentData){
var data = _persistentData[name]; var data = _persistentData[name];
$.save(data, _dataDir.canonicalPath + '/' + name + '-store.json'); $.scsave(data, _dataDir.canonicalPath + '/' + name + '-store.json');
} }
}); });
}; };

View file

@ -65,7 +65,6 @@ exports.autoload = function(dir) {
*/ */
var _reload = function(pluginDir) var _reload = function(pluginDir)
{ {
_loaded = [];
var sourceFiles = []; var sourceFiles = [];
_listSourceFiles(sourceFiles,pluginDir); _listSourceFiles(sourceFiles,pluginDir);

View file

@ -68,7 +68,7 @@ module specification, the '.js' suffix is optional.
// look for a package.json file // look for a package.json file
var pkgJsonFile = new File(dir, './package.json'); var pkgJsonFile = new File(dir, './package.json');
if (pkgJsonFile.exists()){ if (pkgJsonFile.exists()){
var pkg = load(pkgJsonFile); var pkg = scload(pkgJsonFile);
var mainFile = new File(dir, pkg.main); var mainFile = new File(dir, pkg.main);
if (mainFile.exists()){ if (mainFile.exists()){
return mainFile; return mainFile;

View file

@ -207,11 +207,11 @@ to load the named module.
require() will return the loaded module's exports. require() will return the loaded module's exports.
### load() function ### scload() function
#### No longer recommended for use by Plugin/Module developers (deprecated) #### No longer recommended for use by Plugin/Module developers (deprecated)
load() should only be used to load .json data. scload() should only be used to load .json data.
#### Parameters #### Parameters
@ -220,13 +220,13 @@ load() should only be used to load .json data.
#### Returns #### Returns
load() will return the result of the last statement evaluated in the file. scload() will return the result of the last statement evaluated in the file.
#### Example #### Example
load("myFile.js"); // loads a javascript file and evaluates it. scload("myFile.js"); // loads a javascript file and evaluates it.
var myData = load("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. var myData = scload("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned.
##### myData.json contents... ##### myData.json contents...
@ -239,15 +239,15 @@ load() will return the result of the last statement evaluated in the file.
} }
} }
### save() function ### scsave() function
The save() function saves an in-memory javascript object to a The scsave() function saves an in-memory javascript object to a
specified file. Under the hood, save() uses JSON (specifically specified file. Under the hood, scsave() uses JSON (specifically
json2.js) to save the object. Again, there will usually be no need to json2.js) to save the object. Again, there will usually be no need to
call this function directly as all javascript plugins' state are saved call this function directly as all javascript plugins' state are saved
automatically if they are declared using the `plugin()` function. Any automatically if they are declared using the `plugin()` function. Any
in-memory object saved using the `save()` function can later be in-memory object saved using the `scsave()` function can later be
restored using the `load()` function. restored using the `scload()` function.
#### Parameters #### Parameters
@ -259,7 +259,7 @@ restored using the `load()` function.
var myObject = { name: 'John Doe', var myObject = { name: 'John Doe',
aliases: ['John Ray', 'John Mee'], aliases: ['John Ray', 'John Mee'],
date_of_birth: '1982/01/31' }; date_of_birth: '1982/01/31' };
save(myObject, 'johndoe.json'); scsave(myObject, 'johndoe.json');
##### johndoe.json contents... ##### johndoe.json contents...
@ -415,11 +415,6 @@ var server = org.bukkit.Bukkit.server;
*/ */
function __onEnable (__engine, __plugin, __script) function __onEnable (__engine, __plugin, __script)
{ {
/*
don't execute this more than once
*/
if (typeof load == "function")
return ;
var File = java.io.File var File = java.io.File
,FileReader = java.io.FileReader ,FileReader = java.io.FileReader
,BufferedReader = java.io.BufferedReader ,BufferedReader = java.io.BufferedReader
@ -450,8 +445,15 @@ function __onEnable (__engine, __plugin, __script)
out.println( objectToStr ); out.println( objectToStr );
out.close(); out.close();
}; };
/*
make sure eval is present
*/
if (typeof eval == 'undefined'){
global.eval = function(str){
return __engine.eval(str);
};
}
var _loaded = {};
/* /*
Load the contents of the file and evaluate as javascript Load the contents of the file and evaluate as javascript
*/ */
@ -465,11 +467,6 @@ function __onEnable (__engine, __plugin, __script)
file = new File(filename); file = new File(filename);
var canonizedFilename = _canonize(file); var canonizedFilename = _canonize(file);
/*
wph 20130123 don't load the same file more than once.
*/
if (_loaded[canonizedFilename])
return _loaded[canonizedFilename];
if (file.exists()) { if (file.exists()) {
var parent = file.getParentFile(); var parent = file.getParentFile();
@ -484,9 +481,6 @@ function __onEnable (__engine, __plugin, __script)
wrappedCode = "(" + code + ")"; wrappedCode = "(" + code + ")";
result = __engine.eval(wrappedCode); result = __engine.eval(wrappedCode);
// issue #103 avoid side-effects of || operator on Mac Rhino // issue #103 avoid side-effects of || operator on Mac Rhino
_loaded[canonizedFilename] = result ;
if (!_loaded[canonizedFilename])
_loaded[canonizedFilename]= true;
}catch (e){ }catch (e){
__plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e ); __plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e );
} }
@ -544,12 +538,12 @@ function __onEnable (__engine, __plugin, __script)
global.echo = _echo; global.echo = _echo;
global.alert = _echo; global.alert = _echo;
global.load = _load; global.scload = _load;
global.save = _save; global.scsave = _save;
global.addUnloadHandler = _addUnloadHandler; global.addUnloadHandler = _addUnloadHandler;
var fnRequire = load(jsPluginsRootDirName + '/lib/require.js',true); var fnRequire = _load(jsPluginsRootDirName + '/lib/require.js',true);
/* /*
setup paths to search for modules setup paths to search for modules
*/ */
@ -570,15 +564,13 @@ function __onEnable (__engine, __plugin, __script)
global.command = require('command').command; global.command = require('command').command;
var plugins = require('plugin'); var plugins = require('plugin');
global.__onTabComplete = require('tabcomplete'); global.__onTabComplete = require('tabcomplete');
global.plugin = plugins.plugin; global.plugin = plugins.plugin;
var events = require('events'); var events = require('events');
events.on('server.PluginDisableEvent',function(l,e){ events.on('server.PluginDisableEvent',function(l,e){
// save config // save config
save(global.config, new File(jsPluginsRootDir, "data/global-config.json" )); _save(global.config, new File(jsPluginsRootDir, "data/global-config.json" ));
_runUnloadHandlers(); _runUnloadHandlers();
org.bukkit.event.HandlerList["unregisterAll(org.bukkit.plugin.Plugin)"](__plugin); org.bukkit.event.HandlerList["unregisterAll(org.bukkit.plugin.Plugin)"](__plugin);
@ -586,7 +578,6 @@ function __onEnable (__engine, __plugin, __script)
// wph 20131226 - make events global as it is used by many plugins/modules // wph 20131226 - make events global as it is used by many plugins/modules
global.events = events; global.events = events;
plugins.autoload(jsPluginsRootDir);
global.__onCommand = function( sender, cmd, label, args) { global.__onCommand = function( sender, cmd, label, args) {
var jsArgs = []; var jsArgs = [];
@ -620,6 +611,8 @@ function __onEnable (__engine, __plugin, __script)
} }
return result; return result;
}; };
plugins.autoload(jsPluginsRootDir);
/* /*
wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present
*/ */

View file

@ -76,6 +76,8 @@ var _getProperties = function(o)
var onTabCompleteJS = function( result, cmdSender, pluginCmd, cmdAlias, cmdArgs) { var onTabCompleteJS = function( result, cmdSender, pluginCmd, cmdAlias, cmdArgs) {
cmdArgs = Array.prototype.slice.call(cmdArgs, 0);
if (pluginCmd.name == 'jsp') if (pluginCmd.name == 'jsp')
return tabCompleteJSP( result, cmdSender, pluginCmd, cmdAlias, cmdArgs ); return tabCompleteJSP( result, cmdSender, pluginCmd, cmdAlias, cmdArgs );