fix : js 执行函数报错

This commit is contained in:
clay
2024-04-29 22:07:31 +08:00
parent 6953324c3f
commit 41fbf21128
4 changed files with 32 additions and 6 deletions

View File

@@ -41,9 +41,15 @@ public class JavaScriptEngine {
* @return 返回结构
*/
public static Object execute(String script, String functionName, Object args) {
Value executeFunction = getFunction(functionName, script);
Value result = executeFunction.execute(JSON.toJSONString(args));
return result.as(Object.class);
try {
Value executeFunction = getFunction(functionName, script);
Value javaObjectAsValue = Value.asValue(args);
Value result = executeFunction.execute(javaObjectAsValue);
return result.as(Object.class);
} catch (Exception e) {
remove(functionName);
throw new CustomException("运行失败!");
}
}
@@ -68,7 +74,7 @@ public class JavaScriptEngine {
Value result = executeFunction.execute(javaObjectAsValue);
return result.as(Object.class);
});
}catch (CustomException e){
} catch (CustomException e) {
EngineResult result = new EngineResult();
result.setSuccess(Boolean.FALSE);
result.setConsole(e.getMessage());
@@ -90,4 +96,12 @@ public class JavaScriptEngine {
}
private static void remove(String functionName) {
SegmentLock.lock(functionName, () -> {
functionMap.remove(functionName);
return null;
});
}
}