优化脚本功能
This commit is contained in:
parent
ec010f0aef
commit
07fa602ede
|
|
@ -15,6 +15,8 @@ import java.util.stream.Collectors;
|
|||
|
||||
public abstract class AbstractScriptFactory implements ScriptFactory {
|
||||
|
||||
private final Utils utils = new Utils();
|
||||
|
||||
static Class<?>[] DEFAULT_DENIES = {
|
||||
System.class,
|
||||
File.class,
|
||||
|
|
@ -97,4 +99,20 @@ public abstract class AbstractScriptFactory implements ScriptFactory {
|
|||
return denies.contains("*") || denies.contains(typeName);
|
||||
}
|
||||
|
||||
|
||||
public Utils getUtils(){
|
||||
return utils;
|
||||
}
|
||||
|
||||
|
||||
public class Utils {
|
||||
|
||||
private Utils(){}
|
||||
|
||||
public Object toJavaType(Object obj) {
|
||||
return AbstractScriptFactory.this.convertToJavaType(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
|
|||
"this.eval = function(e){};" +
|
||||
"function readFully(){};" +
|
||||
"function readLine(){};" +
|
||||
"const print = console.log;" +
|
||||
"const echo = console.log;");
|
||||
"var print = console.log;" +
|
||||
"var echo = console.log;");
|
||||
|
||||
wrap.add("/* script start */");
|
||||
|
||||
|
|
@ -57,6 +57,7 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
|
|||
Class<? super T> expose) {
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
Set<String> distinct = new HashSet<>();
|
||||
joiner.add("var _$this = $this;");
|
||||
joiner.add(
|
||||
Arrays.stream(expose.getMethods())
|
||||
.filter(method -> !ignoreMethod.contains(method))
|
||||
|
|
@ -70,10 +71,14 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
|
|||
.append(method.getName())
|
||||
.append("(){");
|
||||
if (method.getParameterCount() == 0) {
|
||||
call.append("return $$__that.")
|
||||
call.append("return _$this.")
|
||||
.append(method.getName())
|
||||
.append("();");
|
||||
} else {
|
||||
} else if (method.getParameterCount() == 1 && method.getParameterTypes()[0].isArray()) {
|
||||
call.append("return _$this.")
|
||||
.append(method.getName())
|
||||
.append("(utils.toJavaType(arguments));");
|
||||
}else {
|
||||
|
||||
for (int i = 0; i <= method.getParameterCount(); i++) {
|
||||
String[] args = new String[i];
|
||||
|
|
@ -82,7 +87,7 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
|
|||
}
|
||||
String arg = String.join(",", args);
|
||||
call.append("if(arguments.length==").append(i).append("){")
|
||||
.append("return $$__that.")
|
||||
.append("return _$this.")
|
||||
.append(method.getName())
|
||||
.append("(").append(arg).append(");")
|
||||
.append("}");
|
||||
|
|
@ -101,7 +106,7 @@ public abstract class JavaScriptFactory extends Jsr223ScriptFactory {
|
|||
CompiledScript compiledScript = compile(script.content(joiner.toString()));
|
||||
|
||||
return (instance, ctx) -> {
|
||||
ctx.setAttribute("$$__that", instance, ScriptContext.ENGINE_SCOPE);
|
||||
ctx.setAttribute("$this", instance, ScriptContext.ENGINE_SCOPE);
|
||||
return compiledScript.call(ctx);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ public abstract class Jsr223ScriptFactory extends AbstractScriptFactory {
|
|||
ctx.setAttribute("console", new Jsr223ScriptFactory.Console(
|
||||
LoggerFactory.getLogger("org.jetlinks.community.script." + script.getName())),
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
ctx.setAttribute("utils", getUtils(), ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
ctx.setAttribute("engine", null, ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue