Adding experimental require() function like commonjs (barebones support for require() and exports)

This commit is contained in:
walterhiggins 2013-12-15 21:26:28 +00:00
parent 5263ad1b5d
commit bcf44fe345

View file

@ -310,6 +310,31 @@ var server = org.bukkit.Bukkit.server;
var jsPluginsRootDir = parentFileObj.getParentFile(); var jsPluginsRootDir = parentFileObj.getParentFile();
var jsPluginsRootDirName = _canonize(jsPluginsRootDir); var jsPluginsRootDirName = _canonize(jsPluginsRootDir);
/*
wph 20131215 Experimental
*/
var _loadedModules = {};
var _require = function(path)
{
var file = new java.io.File(path);
var canonizedFilename = _canonize(file);
if (_loadedModules[canonizedFilename]){
return _loadedModules[canonizedFilename];
}
if (verbose){
print("loading module " + canonizedFilename);
}
var reader = new java.io.FileReader(file);
var br = new java.io.BufferedReader(reader);
var code = "";
while ((r = br.readLine()) !== null) code += r + "\n";
code = "var result = {};(function(exports){ " + code + "; return exports;}(result))";
_loadedModules[canonizedFilename] = __engine.eval(code);
return _loadedModules[canonizedFilename];
};
global.loadedModules = _loadedModules;
global.require = _require;
var _loaded = {}; var _loaded = {};
/* /*
@ -317,23 +342,15 @@ var server = org.bukkit.Bukkit.server;
*/ */
var _load = function(filename,warnOnFileNotFound) var _load = function(filename,warnOnFileNotFound)
{ {
var filenames = [];
if (filename.constructor == Array)
filenames = filename;
else
filenames = [filename];
var result = null; var result = null;
for (var i =0;i < filenames.length; i++) { var file = new java.io.File(filename);
var file = new java.io.File(filenames[0]);
var canonizedFilename = _canonize(file); var canonizedFilename = _canonize(file);
// //
// wph 20130123 don't load the same file more than once. // wph 20130123 don't load the same file more than once.
// //
if (_loaded[canonizedFilename]) if (_loaded[canonizedFilename])
continue; return _loaded[canonizedFilename];
if (verbose) if (verbose)
print("loading " + canonizedFilename); print("loading " + canonizedFilename);
@ -357,17 +374,21 @@ var server = org.bukkit.Bukkit.server;
} }
result = __engine.eval(code); result = __engine.eval(code);
_loaded[canonizedFilename] = true; _loaded[canonizedFilename] = result || true;
reader.close();
}catch (e){ }catch (e){
__plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e ); __plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e );
} }
finally {
try {
reader.close();
}catch (re){
// fail silently on reader close error
}
}
}else{ }else{
if (warnOnFileNotFound) if (warnOnFileNotFound)
__plugin.logger.warning(canonizedFilename + " not found"); __plugin.logger.warning(canonizedFilename + " not found");
} }
}
return result; return result;
}; };
/* /*
@ -472,7 +493,7 @@ var server = org.bukkit.Bukkit.server;
// don't load plugin more than once // don't load plugin more than once
// //
if (typeof _plugins[moduleName] != "undefined") if (typeof _plugins[moduleName] != "undefined")
return; return _plugins[moduleName].module;
var pluginData = {persistent: isPersistent, module: moduleObject}; var pluginData = {persistent: isPersistent, module: moduleObject};
moduleObject.store = moduleObject.store || {}; moduleObject.store = moduleObject.store || {};