feat: support exception thrown in applyFunction

This commit is contained in:
CaiHQ 2022-05-03 15:47:32 +08:00
parent cf097ce7fd
commit 6064882843
2 changed files with 11 additions and 5 deletions

View File

@ -144,8 +144,8 @@ public class JavaScriptEntry {
} }
} }
public static void executeFunction(ScriptFunction callback, Object arg) { public static Object executeFunction(ScriptFunction callback, Object arg) {
DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg); return DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg);
} }
public static ApiGate createAPIGate(String ip) { public static ApiGate createAPIGate(String ip) {
@ -330,10 +330,12 @@ public class JavaScriptEntry {
return bo.toString(); return bo.toString();
} }
} }
//YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1}); //YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1});
public static void exceptionReturn(Object obj) throws ScriptReturnException { public static void exceptionReturn(Object obj) throws ScriptReturnException {
throw new ScriptReturnException(JSONTool.convertMirrorToJson(obj)); throw new ScriptReturnException(JSONTool.convertMirrorToJson(obj));
} }
/** /**
* publish an event with semantic AT_LEAST_ONCE * publish an event with semantic AT_LEAST_ONCE
* *

View File

@ -32,6 +32,7 @@ import org.bdware.sc.util.HashUtil;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.MethodNode; 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.NashornScriptEngine;
import wrp.jdk.nashorn.api.scripting.NashornScriptEngineFactory; import wrp.jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
@ -84,7 +85,7 @@ public class DesktopEngine extends JSEngine {
startEngine(); 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(); Global oldGlobal = Context.getGlobal();
boolean globalChanged = (oldGlobal != global); boolean globalChanged = (oldGlobal != global);
try { try {
@ -93,8 +94,10 @@ public class DesktopEngine extends JSEngine {
} }
// System.out.println("[DesktopEngine]" + script.getName() + " -->\n" + // System.out.println("[DesktopEngine]" + script.getName() + " -->\n" +
// script.safeToString()); // script.safeToString());
ScriptRuntime.apply(script, global, obj); Object ret = ScriptRuntime.apply(script, global, obj);
return ret;
} catch (NashornException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -102,6 +105,7 @@ public class DesktopEngine extends JSEngine {
Context.setGlobal(oldGlobal); Context.setGlobal(oldGlobal);
} }
} }
return null;
} }
public void setRecovering(boolean b) { public void setRecovering(boolean b) {