mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-08-26 19:42:02 +00:00
fix(parser): harden runtime resource handling
This commit is contained in:
@@ -46,34 +46,62 @@ public class JsExecUtils {
|
||||
|
||||
/**
|
||||
* 调用执行蓝奏云js文件(每次动态JS代码,无法复用引擎)
|
||||
* 注意:使用后清理引擎引用,帮助 GC 回收 Nashorn 引擎内部资源
|
||||
*/
|
||||
public static ScriptObjectMirror executeDynamicJs(String jsText, String funName) throws ScriptException,
|
||||
NoSuchMethodException {
|
||||
ScriptEngine engine = ENGINE_MANAGER.getEngineByName("JavaScript"); // 得到脚本引擎
|
||||
engine.eval(JsContent.lz + "\n" + jsText);
|
||||
Invocable inv = (Invocable) engine;
|
||||
//调用js中的函数
|
||||
if (StringUtils.isNotEmpty(funName)) {
|
||||
inv.invokeFunction(funName);
|
||||
try {
|
||||
engine.eval(JsContent.lz + "\n" + jsText);
|
||||
Invocable inv = (Invocable) engine;
|
||||
//调用js中的函数
|
||||
if (StringUtils.isNotEmpty(funName)) {
|
||||
inv.invokeFunction(funName);
|
||||
}
|
||||
return (ScriptObjectMirror) engine.get("signObj");
|
||||
} finally {
|
||||
// 清理引擎持有的引用,帮助 GC 回收
|
||||
clearEngineBindings(engine);
|
||||
}
|
||||
|
||||
return (ScriptObjectMirror) engine.get("signObj");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用执行js文件(使用缓存的 ScriptEngineManager 创建新引擎实例)
|
||||
* 注意:使用后清理引擎引用,帮助 GC 回收 Nashorn 引擎内部资源
|
||||
*/
|
||||
public static Object executeOtherJs(String jsText, String funName, Object ... args) throws ScriptException,
|
||||
NoSuchMethodException {
|
||||
ScriptEngine engine = ENGINE_MANAGER.getEngineByName("JavaScript"); // 得到脚本引擎
|
||||
engine.eval(jsText);
|
||||
Invocable inv = (Invocable) engine;
|
||||
//调用js中的函数
|
||||
if (StringUtils.isNotEmpty(funName)) {
|
||||
return inv.invokeFunction(funName, args);
|
||||
try {
|
||||
engine.eval(jsText);
|
||||
Invocable inv = (Invocable) engine;
|
||||
//调用js中的函数
|
||||
if (StringUtils.isNotEmpty(funName)) {
|
||||
return inv.invokeFunction(funName, args);
|
||||
}
|
||||
throw new ScriptException("funName is null");
|
||||
} finally {
|
||||
// 清理引擎持有的引用,帮助 GC 回收
|
||||
clearEngineBindings(engine);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理 ScriptEngine 的 bindings,帮助 GC 回收 Nashorn 引擎资源
|
||||
*/
|
||||
private static void clearEngineBindings(ScriptEngine engine) {
|
||||
try {
|
||||
if (engine != null) {
|
||||
// 清理全局 bindings
|
||||
var bindings = engine.getBindings(javax.script.ScriptContext.ENGINE_SCOPE);
|
||||
if (bindings != null) {
|
||||
bindings.clear();
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// 清理失败不影响主流程
|
||||
}
|
||||
throw new ScriptException("funName is null");
|
||||
}
|
||||
|
||||
public static String getKwSign(String s, String pwd) {
|
||||
|
||||
Reference in New Issue
Block a user