From 7cb679cfd151ada8e9cbce193eb5fc24ac2f095e Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Mon, 13 Jan 2014 21:06:17 +0000 Subject: [PATCH] Fix issue #112 (Support for Nashorn in Java8) --- docs/API-Reference.md | 26 ++++++------ docs/release-notes.md | 5 +++ src/main/javascript/lib/events.js | 6 +-- src/main/javascript/lib/persistence.js | 6 +-- src/main/javascript/lib/plugin.js | 1 - src/main/javascript/lib/require.js | 2 +- src/main/javascript/lib/scriptcraft.js | 59 ++++++++++++-------------- src/main/javascript/lib/tabcomplete.js | 2 + 8 files changed, 51 insertions(+), 56 deletions(-) diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 39a4715..42263b9 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -25,8 +25,8 @@ Walter Higgins * [Global functions](#global-functions) * [echo function](#echo-function) * [require() function](#require-function) - * [load() function](#load-function) - * [save() function](#save-function) + * [scload() function](#scload-function) + * [scsave() function](#scsave-function) * [plugin() function](#plugin-function) * [command() function](#command-function) * [setTimeout() function](#settimeout-function) @@ -348,11 +348,11 @@ to load the named module. require() will return the loaded module's exports. -### load() function +### scload() function #### 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 @@ -361,13 +361,13 @@ load() should only be used to load .json data. #### 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 - 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... @@ -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 -specified file. Under the hood, save() uses JSON (specifically +The scsave() function saves an in-memory javascript object to a +specified file. Under the hood, scsave() uses JSON (specifically 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 automatically if they are declared using the `plugin()` function. Any -in-memory object saved using the `save()` function can later be -restored using the `load()` function. +in-memory object saved using the `scsave()` function can later be +restored using the `scload()` function. #### Parameters @@ -400,7 +400,7 @@ restored using the `load()` function. var myObject = { name: 'John Doe', aliases: ['John Ray', 'John Mee'], date_of_birth: '1982/01/31' }; - save(myObject, 'johndoe.json'); + scsave(myObject, 'johndoe.json'); ##### johndoe.json contents... diff --git a/docs/release-notes.md b/docs/release-notes.md index 5969b29..1232275 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,3 +1,8 @@ +# 2014 01 13 + +Bug Fix: Make ScriptCraft work with Nashorn javascript engine bundled with Java 8 + + # 2014 01 12 ## Important diff --git a/src/main/javascript/lib/events.js b/src/main/javascript/lib/events.js index 4945955..651b1f5 100644 --- a/src/main/javascript/lib/events.js +++ b/src/main/javascript/lib/events.js @@ -93,11 +93,7 @@ exports.on = function( priority = bkEvent.EventPriority[priority]; } if (typeof eventType == "string"){ - var subPkgs = eventType.split('.'); - eventType = bkEvent[subPkgs[0]]; - for (var i = 1;i < subPkgs.length; i++){ - eventType = eventType[subPkgs[i]]; - } + eventType = eval('org.bukkit.event.' + eventType); } var handlerList = eventType.getHandlerList(); var listener = {}; diff --git a/src/main/javascript/lib/persistence.js b/src/main/javascript/lib/persistence.js index 52614e1..cea7147 100644 --- a/src/main/javascript/lib/persistence.js +++ b/src/main/javascript/lib/persistence.js @@ -13,7 +13,7 @@ module.exports = function( rootDir, $ ){ if (typeof write == 'undefined') write = false; if (!write){ - dataFromFile = $.load(_dataDir.canonicalPath + '/' + name + '-store.json'); + dataFromFile = $.scload(_dataDir.canonicalPath + '/' + name + '-store.json'); if (dataFromFile){ for (i in dataFromFile){ data[i] = dataFromFile[i]; @@ -21,7 +21,7 @@ module.exports = function( rootDir, $ ){ } }else{ // flush data to file - $.save(data, _dataDir.canonicalPath + '/' + name + '-store.json'); + $.scsave(data, _dataDir.canonicalPath + '/' + name + '-store.json'); } _persistentData[name] = data; return data; @@ -30,7 +30,7 @@ module.exports = function( rootDir, $ ){ $.addUnloadHandler(function(){ for (var name in _persistentData){ var data = _persistentData[name]; - $.save(data, _dataDir.canonicalPath + '/' + name + '-store.json'); + $.scsave(data, _dataDir.canonicalPath + '/' + name + '-store.json'); } }); }; diff --git a/src/main/javascript/lib/plugin.js b/src/main/javascript/lib/plugin.js index d41a0ef..ba9c9ab 100644 --- a/src/main/javascript/lib/plugin.js +++ b/src/main/javascript/lib/plugin.js @@ -65,7 +65,6 @@ exports.autoload = function(dir) { */ var _reload = function(pluginDir) { - _loaded = []; var sourceFiles = []; _listSourceFiles(sourceFiles,pluginDir); diff --git a/src/main/javascript/lib/require.js b/src/main/javascript/lib/require.js index 30923cb..fa3feaf 100644 --- a/src/main/javascript/lib/require.js +++ b/src/main/javascript/lib/require.js @@ -68,7 +68,7 @@ module specification, the '.js' suffix is optional. // look for a package.json file var pkgJsonFile = new File(dir, './package.json'); if (pkgJsonFile.exists()){ - var pkg = load(pkgJsonFile); + var pkg = scload(pkgJsonFile); var mainFile = new File(dir, pkg.main); if (mainFile.exists()){ return mainFile; diff --git a/src/main/javascript/lib/scriptcraft.js b/src/main/javascript/lib/scriptcraft.js index 6fa7e45..9588338 100644 --- a/src/main/javascript/lib/scriptcraft.js +++ b/src/main/javascript/lib/scriptcraft.js @@ -207,11 +207,11 @@ to load the named module. require() will return the loaded module's exports. -### load() function +### scload() function #### 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 @@ -220,13 +220,13 @@ load() should only be used to load .json data. #### 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 - 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... @@ -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 -specified file. Under the hood, save() uses JSON (specifically +The scsave() function saves an in-memory javascript object to a +specified file. Under the hood, scsave() uses JSON (specifically 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 automatically if they are declared using the `plugin()` function. Any -in-memory object saved using the `save()` function can later be -restored using the `load()` function. +in-memory object saved using the `scsave()` function can later be +restored using the `scload()` function. #### Parameters @@ -259,7 +259,7 @@ restored using the `load()` function. var myObject = { name: 'John Doe', aliases: ['John Ray', 'John Mee'], date_of_birth: '1982/01/31' }; - save(myObject, 'johndoe.json'); + scsave(myObject, 'johndoe.json'); ##### johndoe.json contents... @@ -415,11 +415,6 @@ var server = org.bukkit.Bukkit.server; */ function __onEnable (__engine, __plugin, __script) { - /* - don't execute this more than once - */ - if (typeof load == "function") - return ; var File = java.io.File ,FileReader = java.io.FileReader ,BufferedReader = java.io.BufferedReader @@ -450,8 +445,15 @@ function __onEnable (__engine, __plugin, __script) out.println( objectToStr ); out.close(); }; - - var _loaded = {}; + /* + make sure eval is present + */ + if (typeof eval == 'undefined'){ + global.eval = function(str){ + return __engine.eval(str); + }; + } + /* Load the contents of the file and evaluate as javascript */ @@ -465,11 +467,6 @@ function __onEnable (__engine, __plugin, __script) file = new File(filename); var canonizedFilename = _canonize(file); - /* - wph 20130123 don't load the same file more than once. - */ - if (_loaded[canonizedFilename]) - return _loaded[canonizedFilename]; if (file.exists()) { var parent = file.getParentFile(); @@ -484,9 +481,6 @@ function __onEnable (__engine, __plugin, __script) wrappedCode = "(" + code + ")"; result = __engine.eval(wrappedCode); // issue #103 avoid side-effects of || operator on Mac Rhino - _loaded[canonizedFilename] = result ; - if (!_loaded[canonizedFilename]) - _loaded[canonizedFilename]= true; }catch (e){ __plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e ); } @@ -544,12 +538,12 @@ function __onEnable (__engine, __plugin, __script) global.echo = _echo; global.alert = _echo; - global.load = _load; - global.save = _save; + global.scload = _load; + global.scsave = _save; 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 */ @@ -570,15 +564,13 @@ function __onEnable (__engine, __plugin, __script) global.command = require('command').command; var plugins = require('plugin'); - global.__onTabComplete = require('tabcomplete'); - global.plugin = plugins.plugin; var events = require('events'); events.on('server.PluginDisableEvent',function(l,e){ // save config - save(global.config, new File(jsPluginsRootDir, "data/global-config.json" )); + _save(global.config, new File(jsPluginsRootDir, "data/global-config.json" )); _runUnloadHandlers(); 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 global.events = events; - plugins.autoload(jsPluginsRootDir); global.__onCommand = function( sender, cmd, label, args) { var jsArgs = []; @@ -620,6 +611,8 @@ function __onEnable (__engine, __plugin, __script) } return result; }; + + plugins.autoload(jsPluginsRootDir); /* wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present */ diff --git a/src/main/javascript/lib/tabcomplete.js b/src/main/javascript/lib/tabcomplete.js index 92133b3..04aac7b 100644 --- a/src/main/javascript/lib/tabcomplete.js +++ b/src/main/javascript/lib/tabcomplete.js @@ -76,6 +76,8 @@ var _getProperties = function(o) var onTabCompleteJS = function( result, cmdSender, pluginCmd, cmdAlias, cmdArgs) { + cmdArgs = Array.prototype.slice.call(cmdArgs, 0); + if (pluginCmd.name == 'jsp') return tabCompleteJSP( result, cmdSender, pluginCmd, cmdAlias, cmdArgs );