From 56d140a2c746d5f4195faf59729be8510e7d4e62 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Tue, 6 Sep 2022 12:27:27 +0800 Subject: [PATCH] connect function node with compiled function --- build.gradle | 10 +++---- .../org/bdware/sc/engine/DesktopEngine.java | 30 +++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 6177d6a..863222a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "org.bdware.sc" -version = "1.6.4" +version = "1.6.5" repositories { mavenCentral() mavenLocal() @@ -58,11 +58,11 @@ jar { libs = libs + " libs/" + it.name } from { - // uncomment this when publish, - // while develop at local use "false" + // uncomment this when publish, + //while develop at local use "false" configurations.runtimeClasspath.filter { - // it.getAbsolutePath().contains("/lib/") - false + it.getAbsolutePath().contains("/lib/") + // false }.collect { it.isDirectory() ? it : zipTree(it) } diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 107a88d..5a8ff55 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -41,6 +41,7 @@ import wrp.jdk.nashorn.internal.runtime.*; import javax.script.*; import java.io.*; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; @@ -271,7 +272,7 @@ public class DesktopEngine extends JSEngine { fun.getFileName(), ScriptContext.ENGINE_SCOPE); LOGGER.info("loadFun:" + str); - compileFunction(str, isInsnLimit); + compileFunction(fun, str, isInsnLimit); } catch (ScriptException e) { return wrapperException(e, fun); } catch (Exception e) { @@ -321,13 +322,13 @@ public class DesktopEngine extends JSEngine { "function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }", name, topic, semantics.name()); } - compileFunction(str, false); + compileFunction(null, str, false); LOGGER.debug("compile function " + name + " success!"); str = String.format( "function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }", name, topic); - compileFunction(str, false); + compileFunction(null, str, false); LOGGER.debug("compile function " + name + "s success!"); } catch (ScriptException e) { e.printStackTrace(); @@ -406,7 +407,7 @@ public class DesktopEngine extends JSEngine { // // return new ContractResult(Status.Success, new JsonPrimitive("")); // } - private void compileFunction(ScriptObjectMirror sf, boolean instrumentBranch) { + private void compileFunction(FunctionNode functionNode, ScriptObjectMirror sf, boolean instrumentBranch) { Global oldGlobal = Context.getGlobal(); boolean globalChanged = (oldGlobal != global); try { @@ -418,6 +419,23 @@ public class DesktopEngine extends JSEngine { Context.TRACEMETHOD = true; } sf.compileScriptFunction(); + ScriptFunction scriptFunction = (ScriptFunction) sf.getScriptObject(); + Field f = ScriptFunction.class.getDeclaredField("data"); + f.setAccessible(true); + ScriptFunctionData scriptFunctioNData = (ScriptFunctionData) f.get(scriptFunction); + Object scope = scriptFunction.getScope(); + Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker", ScriptObject.class); + getGeneric.setAccessible(true); + MethodHandle methodHandle = (MethodHandle) getGeneric.invoke(scriptFunctioNData, scope); + Field memberNameField = methodHandle.getClass().getDeclaredField("member"); + memberNameField.setAccessible(true); + Object memberName = memberNameField.get(methodHandle); + Field clazz = memberName.getClass().getDeclaredField("clazz"); + clazz.setAccessible(true); + Class clazz2 = (Class) clazz.get(memberName); + if (functionNode != null) + functionNode.compiledClazz = clazz2; + //functionNode==null --> event functions } catch (Exception e) { e.printStackTrace(); } finally { @@ -426,8 +444,8 @@ public class DesktopEngine extends JSEngine { } } - public void compileFunction(String snippet, boolean instrumentBranch) throws ScriptException { - compileFunction((ScriptObjectMirror) engine.eval(snippet), instrumentBranch); + public void compileFunction(FunctionNode functionNode, String snippet, boolean instrumentBranch) throws ScriptException { + compileFunction(functionNode, (ScriptObjectMirror) engine.eval(snippet), instrumentBranch); }