Fixes issue #167

This commit is contained in:
walterhiggins 2014-12-25 14:04:45 +00:00
parent 4fd37054d9
commit 39ce9061f7
4 changed files with 18 additions and 12 deletions

View file

@ -1,3 +1,4 @@
/*global nashorn, exports, require, Packages, __plugin*/
var cmPriority = Packages.net.canarymod.plugin.Priority, var cmPriority = Packages.net.canarymod.plugin.Priority,
cmCanary = Packages.net.canarymod.Canary, cmCanary = Packages.net.canarymod.Canary,
cmDispatcher = Packages.net.canarymod.hook.Dispatcher, cmDispatcher = Packages.net.canarymod.hook.Dispatcher,
@ -40,13 +41,10 @@ exports.on = function(
The workaround is to make the ScriptCraftPlugin java class a Listener. The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code. Should only unregister() registered plugins in ScriptCraft js code.
*/ */
try { if (nashorn){
// nashorn // nashorn
eventType = eventType.class; eventType = require('nashorn-type')(eventType);
} catch ( e ){ }
// non-nashorn
eventType = eventType;
}
regd = new cmPluginListener({}); regd = new cmPluginListener({});
cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority); cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority);
result.unregister = function(){ result.unregister = function(){

View file

@ -0,0 +1,8 @@
/*
The .class operator causes problems for non-nashorn Java on Mac OS X and some other
environments. So need to have it in a separate module which should only be loaded in
nashorn environment.
*/
module.exports = function(t){
return t.class;
};

View file

@ -383,6 +383,7 @@ This function takes a single parameter and returns true if it's an operator or h
*/ */
var global = this; var global = this;
var server; var server;
global.nashorn = typeof Java !== 'undefined';
/* /*
private implementation private implementation
*/ */
@ -624,7 +625,7 @@ function __onEnable ( __engine, __plugin, __script ) {
/* /*
wph 20140312 don't delete self on nashorn until https://bugs.openjdk.java.net/browse/JDK-8034055 is fixed wph 20140312 don't delete self on nashorn until https://bugs.openjdk.java.net/browse/JDK-8034055 is fixed
*/ */
if ( typeof Java === 'undefined' ) { // Java is an object in Nashorn if ( !nashorn ) {
delete global.self; delete global.self;
delete global.__engine; delete global.__engine;
} }

View file

@ -29,11 +29,11 @@ function getMaterialHandler( material ){
} }
}; };
} }
try { if (nashorn){
/* /*
nashorn nashorn
*/ */
var itemTypeClass = ItemType.class; var itemTypeClass = require('nashorn-type')(ItemType);
var materials = itemTypeClass.getDeclaredFields(); var materials = itemTypeClass.getDeclaredFields();
for (var i = 0;i < materials.length; i++ ){ for (var i = 0;i < materials.length; i++ ){
@ -48,19 +48,18 @@ try {
items[name] = getMaterialHandler(materialField.get(ItemType)); items[name] = getMaterialHandler(materialField.get(ItemType));
} }
} catch ( e ){ } else {
// non-nashorn // non-nashorn
for (var field in ItemType){ for (var field in ItemType){
if (ItemType[field] === undefined){ if (ItemType[field] === undefined){
continue; continue;
} }
if (ItemType[field].class != ItemType){ if (!(ItemType[field] instanceof ItemType)){
continue; continue;
} }
var name = (''+field).replace(/^(.)/,function(a){ var name = (''+field).replace(/^(.)/,function(a){
return a.toLowerCase(); return a.toLowerCase();
}); });
console.log(name);
items[name] = getMaterialHandler(ItemType[field]); items[name] = getMaterialHandler(ItemType[field]);
} }
} }