From 664cb99abaf2ac1f0ec852cb2b890f19c1d7dcde Mon Sep 17 00:00:00 2001 From: zendwang Date: Mon, 28 Nov 2022 10:25:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20=E4=BC=98=E5=8C=96graaljs=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=8F=92=E4=BB=B6=E5=AF=B9JS=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E9=A2=84=E7=BC=96=E8=AF=91=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graaljs/GraalJavaScriptExecutor.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/liteflow-script-plugin/liteflow-script-graaljs/src/main/java/com/yomahub/liteflow/script/graaljs/GraalJavaScriptExecutor.java b/liteflow-script-plugin/liteflow-script-graaljs/src/main/java/com/yomahub/liteflow/script/graaljs/GraalJavaScriptExecutor.java index f7d48fd8..083d48c9 100644 --- a/liteflow-script-plugin/liteflow-script-graaljs/src/main/java/com/yomahub/liteflow/script/graaljs/GraalJavaScriptExecutor.java +++ b/liteflow-script-plugin/liteflow-script-graaljs/src/main/java/com/yomahub/liteflow/script/graaljs/GraalJavaScriptExecutor.java @@ -11,6 +11,8 @@ import com.yomahub.liteflow.slot.DataBus; import com.yomahub.liteflow.slot.Slot; import com.yomahub.liteflow.util.CopyOnWriteHashMap; import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Engine; +import org.graalvm.polyglot.Source; import org.graalvm.polyglot.Value; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,10 +28,13 @@ public class GraalJavaScriptExecutor implements ScriptExecutor { private final Logger log = LoggerFactory.getLogger(this.getClass()); - private final Map scriptMap = new CopyOnWriteHashMap<>(); + private final Map scriptMap = new CopyOnWriteHashMap<>(); + + private Engine engine; @Override public ScriptExecutor init() { + engine = Engine.create(); return this; } @@ -37,7 +42,7 @@ public class GraalJavaScriptExecutor implements ScriptExecutor { public void load(String nodeId, String script) { try{ String wrapScript = StrUtil.format("function process(){{}} process();",script); - scriptMap.put(nodeId, wrapScript); + scriptMap.put(nodeId, Source.create("js", wrapScript)); }catch (Exception e){ String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage()); throw new ScriptLoadException(errorMsg); @@ -46,13 +51,11 @@ public class GraalJavaScriptExecutor implements ScriptExecutor { @Override public Object execute(ScriptExecuteWrap wrap) throws Exception{ - try{ - if (!scriptMap.containsKey(wrap.getNodeId())){ - String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId()); - throw new ScriptLoadException(errorMsg); - } - - Context context = Context.newBuilder().allowAllAccess(true).build(); + if (!scriptMap.containsKey(wrap.getNodeId())){ + String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId()); + throw new ScriptLoadException(errorMsg); + } + try (Context context = Context.newBuilder().allowAllAccess(true).engine(this.engine).build()) { Value bindings = context.getBindings("js"); //往脚本语言绑定表里循环增加绑定上下文的key //key的规则为自定义上下文的simpleName @@ -87,7 +90,7 @@ public class GraalJavaScriptExecutor implements ScriptExecutor { }); - Value value = context.eval("js", scriptMap.get(wrap.getNodeId())); + Value value = context.eval( scriptMap.get(wrap.getNodeId())); if (value.isBoolean()) { return value.asBoolean(); } else if (value.isNumber()) { @@ -96,7 +99,7 @@ public class GraalJavaScriptExecutor implements ScriptExecutor { return value.asString(); } return value; - }catch (Exception e){ + } catch (Exception e){ log.error(e.getMessage(), e); throw e; }