connect function node with compiled function

This commit is contained in:
CaiHQ 2022-09-06 12:27:27 +08:00
parent 363a20049a
commit 56d140a2c7
2 changed files with 29 additions and 11 deletions

View File

@ -6,7 +6,7 @@ plugins {
} }
group = "org.bdware.sc" group = "org.bdware.sc"
version = "1.6.4" version = "1.6.5"
repositories { repositories {
mavenCentral() mavenCentral()
mavenLocal() mavenLocal()
@ -58,11 +58,11 @@ jar {
libs = libs + " libs/" + it.name libs = libs + " libs/" + it.name
} }
from { from {
// uncomment this when publish, // uncomment this when publish,
// while develop at local use "false" //while develop at local use "false"
configurations.runtimeClasspath.filter { configurations.runtimeClasspath.filter {
// it.getAbsolutePath().contains("/lib/") it.getAbsolutePath().contains("/lib/")
false // false
}.collect { }.collect {
it.isDirectory() ? it : zipTree(it) it.isDirectory() ? it : zipTree(it)
} }

View File

@ -41,6 +41,7 @@ import wrp.jdk.nashorn.internal.runtime.*;
import javax.script.*; import javax.script.*;
import java.io.*; import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
@ -271,7 +272,7 @@ public class DesktopEngine extends JSEngine {
fun.getFileName(), fun.getFileName(),
ScriptContext.ENGINE_SCOPE); ScriptContext.ENGINE_SCOPE);
LOGGER.info("loadFun:" + str); LOGGER.info("loadFun:" + str);
compileFunction(str, isInsnLimit); compileFunction(fun, str, isInsnLimit);
} catch (ScriptException e) { } catch (ScriptException e) {
return wrapperException(e, fun); return wrapperException(e, fun);
} catch (Exception e) { } catch (Exception e) {
@ -321,13 +322,13 @@ public class DesktopEngine extends JSEngine {
"function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }", "function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }",
name, topic, semantics.name()); name, topic, semantics.name());
} }
compileFunction(str, false); compileFunction(null, str, false);
LOGGER.debug("compile function " + name + " success!"); LOGGER.debug("compile function " + name + " success!");
str = str =
String.format( String.format(
"function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }", "function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }",
name, topic); name, topic);
compileFunction(str, false); compileFunction(null, str, false);
LOGGER.debug("compile function " + name + "s success!"); LOGGER.debug("compile function " + name + "s success!");
} catch (ScriptException e) { } catch (ScriptException e) {
e.printStackTrace(); e.printStackTrace();
@ -406,7 +407,7 @@ public class DesktopEngine extends JSEngine {
// //
// return new ContractResult(Status.Success, new JsonPrimitive("")); // 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(); Global oldGlobal = Context.getGlobal();
boolean globalChanged = (oldGlobal != global); boolean globalChanged = (oldGlobal != global);
try { try {
@ -418,6 +419,23 @@ public class DesktopEngine extends JSEngine {
Context.TRACEMETHOD = true; Context.TRACEMETHOD = true;
} }
sf.compileScriptFunction(); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -426,8 +444,8 @@ public class DesktopEngine extends JSEngine {
} }
} }
public void compileFunction(String snippet, boolean instrumentBranch) throws ScriptException { public void compileFunction(FunctionNode functionNode, String snippet, boolean instrumentBranch) throws ScriptException {
compileFunction((ScriptObjectMirror) engine.eval(snippet), instrumentBranch); compileFunction(functionNode, (ScriptObjectMirror) engine.eval(snippet), instrumentBranch);
} }