feat: 缓存编译结果 使用load载入脚本

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-09-23 18:30:14 +08:00
parent 7c5ad40497
commit 9556008bee
7 changed files with 32 additions and 23 deletions

4
.gitignore vendored
View File

@ -35,4 +35,6 @@
/src/main/resources/Soulbound
# Atlassian Stuff
/atlassian-ide-plugin.xml
/atlassian-ide-plugin.xml
/src/main/resources/cache/

View File

@ -57,7 +57,6 @@ public class MiaoScript extends JavaPlugin {
this.engine.eval(new InputStreamReader(this.getResource("bios.js")));
engine.invokeFunction("boot", this);
} catch (Exception e) {
Log.w("脚本引擎初始化失败! %s:%s", e.getClass().getName(), e.getMessage());
Log.d(e);
} finally {
currentThread.setContextClassLoader(previousClassLoader);

View File

@ -13,7 +13,12 @@ var disable;
root = plugin.dataFolder.canonicalPath;
}
load(root + '/core/init.js');
init(root, plugin);
try {
init(root, plugin);
} catch (ex) {
log.w("MiaoScript 初始化失败! %s", ex);
throw ex;
}
disable = disablePlugins
};
})();

View File

@ -13,6 +13,9 @@ var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
* @returns {*}
*/
exports.file = function () {
if (!arguments[0]) {
log.w("文件名称不得为 undefined 或者 null !");
}
if (exports.canonical(arguments[0])) {
return arguments[0];
}
@ -28,7 +31,8 @@ exports.file = function () {
* @param file
*/
exports.mkdirs = function (file) {
file.getParentFile().mkdirs();
// noinspection JSUnresolvedVariable
file.parentFile.mkdirs();
};
/**
* 创建文件

View File

@ -47,8 +47,10 @@ function loadPlugins(plugin) {
// 初始化本体插件
var self = require('modules/plugin');
self.init(plugin, plugins_dir);
self.load();
self.enable();
if (!self.$) {
self.load();
self.enable();
}
}
// noinspection JSUnusedLocalSymbols

View File

@ -8,6 +8,7 @@
var File = Java.type("java.io.File");
var Files = Java.type("java.nio.file.Files");
var String = Java.type("java.lang.String");
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
/**
* 解析模块名称为文件
@ -48,26 +49,20 @@
log.w("模块目录: %s", _canonical(moduleFile));
}
/**
* 使用NIO读取文件内容
* @param file 文件
* @private
*/
function _readFile(file) {
// noinspection JSPrimitiveTypeWrapperUsage
return new String(Files.readAllBytes(file.toPath()), "UTF-8");
}
/**
* 预编译模块
* @param name
* @param src
* @returns {Object}
*/
function compileJs(src) {
function compileJs(name, src) {
var head = "(function (module, exports, require) {\n";
var tail = "\n});";
var fulljs = head + src + tail;
return eval(fulljs);
var cacheFile = cacheDir + "/" + name + ".js";
log.d(cacheFile);
base.save(cacheFile, fulljs);
return load(cacheFile);
}
/**
@ -104,16 +99,17 @@
exports: {},
require: exports(file.parentFile)
};
var src = _readFile(file);
var src = base.read(file);
try {
// 预编译模块
var compiledWrapper = compileJs(src);
var compiledWrapper = compileJs(name, src);
compiledWrapper.apply(module.exports, [
module, module.exports, module.require
]);
} catch (ex) {
log.w("模块 %s 编译失败!", name);
log.w(ex);
log.d(ex);
return;
}
log.d('模块 %s 编译成功!', name);
module.loaded = true;
@ -132,6 +128,8 @@
};
}
var cacheDir = parent + "/cache";
// 等于 undefined 说明 parent 是一个字符串 需要转成File
// 可能更加准确的方案
if (_canonical(parent) === undefined) {

View File

@ -11,7 +11,7 @@ var fs = require('core/fs');
function loadPlugins(path) {
path = fs.file(path);
log.i("开始扫描 %s 下的插件...", path);
updatePlugins();
updatePlugins(path);
var files = [];
fs.list(path).forEach(function (file) {
files.push(file.toFile());
@ -54,7 +54,6 @@ function loadJsPlugin(files) {
return file.name.endsWith(".js")
}).forEach(function (file) {
var p = require(file);
log.d(JSON.stringify(p));
if (!p.description || !p.description.name) {
log.w("文件 %s 不存在 description 描述信息 无法加载插件!");
} else {