From 6064882843680d629533941f97b2d0f46485367d Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Tue, 3 May 2022 15:47:32 +0800 Subject: [PATCH] feat: support exception thrown in applyFunction --- .../java/org/bdware/sc/boundry/JavaScriptEntry.java | 6 ++++-- src/main/java/org/bdware/sc/engine/DesktopEngine.java | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java index 77ef3d0..4125038 100644 --- a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java +++ b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java @@ -144,8 +144,8 @@ public class JavaScriptEntry { } } - public static void executeFunction(ScriptFunction callback, Object arg) { - DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg); + public static Object executeFunction(ScriptFunction callback, Object arg) { + return DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg); } public static ApiGate createAPIGate(String ip) { @@ -330,10 +330,12 @@ public class JavaScriptEntry { return bo.toString(); } } + //YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1}); public static void exceptionReturn(Object obj) throws ScriptReturnException { throw new ScriptReturnException(JSONTool.convertMirrorToJson(obj)); } + /** * publish an event with semantic AT_LEAST_ONCE * diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 7e3fda0..bd226c3 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -32,6 +32,7 @@ import org.bdware.sc.util.HashUtil; import org.bdware.sc.util.JsonUtil; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.MethodNode; +import wrp.jdk.nashorn.api.scripting.NashornException; import wrp.jdk.nashorn.api.scripting.NashornScriptEngine; import wrp.jdk.nashorn.api.scripting.NashornScriptEngineFactory; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; @@ -84,7 +85,7 @@ public class DesktopEngine extends JSEngine { startEngine(); } - public static void applyWithGlobal(ScriptFunction script, Global global, Object... obj) { + public static Object applyWithGlobal(ScriptFunction script, Global global, Object... obj) { Global oldGlobal = Context.getGlobal(); boolean globalChanged = (oldGlobal != global); try { @@ -93,8 +94,10 @@ public class DesktopEngine extends JSEngine { } // System.out.println("[DesktopEngine]" + script.getName() + " -->\n" + // script.safeToString()); - ScriptRuntime.apply(script, global, obj); - + Object ret = ScriptRuntime.apply(script, global, obj); + return ret; + } catch (NashornException e) { + throw e; } catch (Exception e) { e.printStackTrace(); } finally { @@ -102,6 +105,7 @@ public class DesktopEngine extends JSEngine { Context.setGlobal(oldGlobal); } } + return null; } public void setRecovering(boolean b) {