From 75ab43d9b4051b50178e80d100981fde95bf3d1e Mon Sep 17 00:00:00 2001 From: Odboy Date: Mon, 26 Jan 2026 20:05:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(KitScriptHelper):=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=20Rhino=20JavaScript=20=E5=BC=95=E6=93=8E=E4=B8=BA=20EvalEx=20?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/context/KitScriptHelper.java | 39 +++++-------------- .../src/{test => main/resources}/logback.xml | 11 ++++-- .../framework/context/KitScriptHelper.java | 38 +++++------------- 3 files changed, 25 insertions(+), 63 deletions(-) rename cutejava-springboot3/cutejava-starter/src/{test => main/resources}/logback.xml (87%) diff --git a/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java index def59dd9..9bd822eb 100644 --- a/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java +++ b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java @@ -13,13 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package cn.odboy.framework.context; +import com.ezylang.evalex.EvaluationException; +import com.ezylang.evalex.Expression; +import com.ezylang.evalex.data.EvaluationValue; +import com.ezylang.evalex.parser.ParseException; import java.util.Map; import lombok.extern.slf4j.Slf4j; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.springframework.stereotype.Component; /** @@ -32,32 +33,10 @@ import org.springframework.stereotype.Component; @Component public class KitScriptHelper { - public Object evalObj(String script, Map args) { - try (Context rhino = Context.enter()) { - Scriptable scope = rhino.initStandardObjects(); - bindArgumentsToScope(scope, args); - Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null); - log.info("执行结果: {}", Context.toString(result)); - return result; + public Object evalObj(String script, Map args) throws EvaluationException, ParseException { + Expression expression = new Expression(script); + expression.withValues(args); + EvaluationValue result = expression.evaluate(); + return result.getValue(); } - } - - private void bindArgumentsToScope(Scriptable scope, Map args) { - if (args == null) { - return; - } - for (Map.Entry entry : args.entrySet()) { - Object jsValue = Context.javaToJS(entry.getValue(), scope); - scope.put(entry.getKey().toString(), scope, jsValue); - } - } - - private void bindArgumentsToScope(Scriptable scope, Object args) { - if (args == null) { - return; - } - // 单个对象参数 - Object jsValue = Context.javaToJS(args, scope); - scope.put("args", scope, jsValue); - } } diff --git a/cutejava-springboot3/cutejava-starter/src/test/logback.xml b/cutejava-springboot3/cutejava-starter/src/main/resources/logback.xml similarity index 87% rename from cutejava-springboot3/cutejava-starter/src/test/logback.xml rename to cutejava-springboot3/cutejava-starter/src/main/resources/logback.xml index acdd235a..caac7410 100644 --- a/cutejava-springboot3/cutejava-starter/src/test/logback.xml +++ b/cutejava-springboot3/cutejava-starter/src/main/resources/logback.xml @@ -4,15 +4,16 @@ - + + - + + @@ -20,6 +21,7 @@ ${log.charset} + ${log.home}/${log.name}.log @@ -31,10 +33,11 @@ ${log.pattern} + - \ No newline at end of file + diff --git a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java index 1e1bc040..9bd822eb 100644 --- a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java +++ b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/context/KitScriptHelper.java @@ -15,10 +15,12 @@ */ package cn.odboy.framework.context; +import com.ezylang.evalex.EvaluationException; +import com.ezylang.evalex.Expression; +import com.ezylang.evalex.data.EvaluationValue; +import com.ezylang.evalex.parser.ParseException; import java.util.Map; import lombok.extern.slf4j.Slf4j; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.springframework.stereotype.Component; /** @@ -31,32 +33,10 @@ import org.springframework.stereotype.Component; @Component public class KitScriptHelper { - public Object evalObj(String script, Map args) { - try (Context rhino = Context.enter()) { - Scriptable scope = rhino.initStandardObjects(); - bindArgumentsToScope(scope, args); - Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null); - log.info("执行结果: {}", Context.toString(result)); - return result; + public Object evalObj(String script, Map args) throws EvaluationException, ParseException { + Expression expression = new Expression(script); + expression.withValues(args); + EvaluationValue result = expression.evaluate(); + return result.getValue(); } - } - - private void bindArgumentsToScope(Scriptable scope, Map args) { - if (args == null) { - return; - } - for (Map.Entry entry : args.entrySet()) { - Object jsValue = Context.javaToJS(entry.getValue(), scope); - scope.put(entry.getKey().toString(), scope, jsValue); - } - } - - private void bindArgumentsToScope(Scriptable scope, Object args) { - if (args == null) { - return; - } - // 单个对象参数 - Object jsValue = Context.javaToJS(args, scope); - scope.put("args", scope, jsValue); - } }