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"
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)
}

View File

@ -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);
}