This commit is contained in:
kunfei
2023-03-25 16:09:40 +08:00
parent 29b843aab3
commit 9b814b7665
13 changed files with 350 additions and 480 deletions
@@ -1,111 +1,86 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.io.Reader; import java.io.Reader
@SuppressWarnings("unused") abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngine {
public abstract class AbstractScriptEngine implements ScriptEngine {
protected ScriptContext context = new SimpleScriptContext(); override var context: ScriptContext = SimpleScriptContext()
public AbstractScriptEngine() { init {
} bindings?.let {
context.setBindings(bindings, 100)
public AbstractScriptEngine(Bindings n) {
this();
if (n == null) {
throw new NullPointerException("n is null");
} }
this.context.setBindings(n, 100);
} }
@Override override fun getBindings(scope: Int): Bindings? {
public void setContext(ScriptContext ctxt) {
if (ctxt == null) {
throw new NullPointerException("null context");
}
this.context = ctxt;
}
@Override
public ScriptContext getContext() {
return this.context;
}
@Override
public Bindings getBindings(int scope) {
if (scope == 200) { if (scope == 200) {
return this.context.getBindings(200); return context.getBindings(200)
} }
if (scope == 100) { if (scope == 100) {
return this.context.getBindings(100); return context.getBindings(100)
} }
throw new IllegalArgumentException("Invalid scope value."); throw IllegalArgumentException("Invalid scope value.")
} }
@Override override fun setBindings(bindings: Bindings?, scope: Int) {
public void setBindings(Bindings bindings, int scope) { when (scope) {
if (scope == 200) { 200 -> {
this.context.setBindings(bindings, 200); context.setBindings(bindings, 200)
} else if (scope == 100) { }
this.context.setBindings(bindings, 100); 100 -> {
} else { context.setBindings(bindings, 100)
throw new IllegalArgumentException("Invalid scope value."); }
else -> {
throw IllegalArgumentException("Invalid scope value.")
}
} }
} }
@Override override fun put(key: String, value: Any?) {
public void put(String key, Object value) { val nn = getBindings(100)
Bindings nn = this.getBindings(100);
if (nn != null) { if (nn != null) {
nn.put(key, value); nn[key] = value
} }
} }
@Override override fun get(key: String): Any? {
public Object get(String key) { val nn = getBindings(100)
Bindings nn = this.getBindings(100); return nn?.get(key)
if (nn != null) {
return nn.get(key);
}
return null;
} }
@Override @Throws(ScriptException::class)
public Object eval(Reader reader, Bindings bindings) throws ScriptException { override fun eval(reader: Reader, bindings: Bindings): Any? {
return this.eval(reader, this.getScriptContext(bindings)); return this.eval(reader, getScriptContext(bindings))
} }
@Override @Throws(ScriptException::class)
public Object eval(String script, Bindings bindings) throws ScriptException { override fun eval(script: String, bindings: Bindings): Any? {
return this.eval(script, this.getScriptContext(bindings)); return this.eval(script, getScriptContext(bindings))
} }
@Override @Throws(ScriptException::class)
public Object eval(Reader reader) throws ScriptException { override fun eval(reader: Reader): Any? {
return this.eval(reader, this.context); return this.eval(reader, context)
} }
@Override @Throws(ScriptException::class)
public Object eval(String script) throws ScriptException { override fun eval(script: String): Any? {
return this.eval(script, this.context); return this.eval(script, context)
} }
protected ScriptContext getScriptContext(Bindings nn) { protected fun getScriptContext(nn: Bindings): ScriptContext {
SimpleScriptContext ctxt = new SimpleScriptContext(); val ctx = SimpleScriptContext()
Bindings gs = this.getBindings(200); val gs = getBindings(200)
if (gs != null) { if (gs != null) {
ctxt.setBindings(gs, 200); ctx.setBindings(gs, 200)
} }
if (nn != null) { ctx.setBindings(nn, 100)
ctxt.setBindings(nn, 100); ctx.reader = context.reader
ctxt.setReader(this.context.getReader()); ctx.writer = context.writer
ctxt.setWriter(this.context.getWriter()); ctx.errorWriter = context.errorWriter
ctxt.setErrorWriter(this.context.getErrorWriter()); return ctx
return ctxt;
}
throw new NullPointerException("Engine scope Bindings may not be null.");
} }
} }
+8 -14
View File
@@ -1,24 +1,18 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.util.Map; interface Bindings : MutableMap<String, Any?> {
@SuppressWarnings("UnnecessaryModifier") override fun containsKey(key: String): Boolean
public interface Bindings extends Map<String, Object> {
@Override
public boolean containsKey(Object var1);
@Override override operator fun get(key: String): Any?
public Object get(Object var1);
@Override override fun put(key: String, value: Any?): Any?
public Object put(String var1, Object var2);
@Override override fun putAll(from: Map<out String, *>)
public void putAll(Map<? extends String, ?> var1);
override fun remove(key: String): Any?
@Override
public Object remove(Object var1);
} }
+8 -6
View File
@@ -1,13 +1,15 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.io.Reader; import java.io.Reader
@SuppressWarnings("UnnecessaryModifier") interface Compilable {
public interface Compilable {
public CompiledScript compile(Reader var1) throws ScriptException;
public CompiledScript compile(String var1) throws ScriptException; @Throws(ScriptException::class)
fun compile(script: Reader): CompiledScript
@Throws(ScriptException::class)
fun compile(script: String): CompiledScript
} }
@@ -1,28 +1,32 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
public abstract class CompiledScript { abstract class CompiledScript {
public abstract Object eval(ScriptContext var1) throws ScriptException;
public abstract ScriptEngine getEngine(); abstract fun getEngine(): ScriptEngine
public Object eval(Bindings bindings) throws ScriptException { @Throws(ScriptException::class)
ScriptContext ctxt = this.getEngine().getContext(); abstract fun eval(context: ScriptContext): Any?
@Throws(ScriptException::class)
fun eval(bindings: Bindings?): Any? {
var ctxt = getEngine().context
if (bindings != null) { if (bindings != null) {
SimpleScriptContext tempctxt = new SimpleScriptContext(); val tempContext = SimpleScriptContext()
tempctxt.setBindings(bindings, 100); tempContext.setBindings(bindings, 100)
tempctxt.setBindings(ctxt.getBindings(200), 200); tempContext.setBindings(ctxt.getBindings(200), 200)
tempctxt.setWriter(ctxt.getWriter()); tempContext.writer = ctxt.writer
tempctxt.setReader(ctxt.getReader()); tempContext.reader = ctxt.reader
tempctxt.setErrorWriter(ctxt.getErrorWriter()); tempContext.errorWriter = ctxt.errorWriter
ctxt = tempctxt; ctxt = tempContext
} }
return this.eval(ctxt); return this.eval(ctxt)
} }
public Object eval() throws ScriptException { @Throws(ScriptException::class)
return this.eval(this.getEngine().getContext()); fun eval(): Any? {
return this.eval(getEngine().context)
} }
} }
+8 -7
View File
@@ -1,15 +1,16 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
@SuppressWarnings({"unused", "UnnecessaryModifier"}) interface Invocable {
public interface Invocable { fun <T> getInterface(clazz: Class<T>): T?
public <T> T getInterface(Class<T> var1);
public <T> T getInterface(Object var1, Class<T> var2); fun <T> getInterface(obj: Any?, paramClass: Class<T>): T?
public Object invokeFunction(String var1, Object ... var2) throws ScriptException, NoSuchMethodException; @Throws(ScriptException::class, NoSuchMethodException::class)
fun invokeFunction(name: String, vararg args: Any): Any?
public Object invokeMethod(Object var1, String var2, Object ... var3) throws ScriptException, NoSuchMethodException; @Throws(ScriptException::class, NoSuchMethodException::class)
fun invokeMethod(obj: Any?, name: String, vararg args: Any): Any?
} }
+19 -25
View File
@@ -1,43 +1,37 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.io.Reader; import java.io.Reader
import java.io.Writer; import java.io.Writer
import java.util.List;
@SuppressWarnings({"unused", "UnnecessaryModifier"}) interface ScriptContext {
public interface ScriptContext {
public static final int ENGINE_SCOPE = 100; var errorWriter: Writer
public static final int GLOBAL_SCOPE = 200;
public Object getAttribute(String var1); var reader: Reader
public Object getAttribute(String var1, int var2); val scopes: List<Int>
public int getAttributesScope(String var1); var writer: Writer
public Bindings getBindings(int var1); fun getAttribute(name: String): Any?
public Writer getErrorWriter(); fun getAttribute(name: String, scope: Int): Any?
public Reader getReader(); fun getAttributesScope(name: String): Int
public List<Integer> getScopes(); fun getBindings(scope: Int): Bindings?
public Writer getWriter(); fun removeAttribute(name: String, scope: Int): Any?
public Object removeAttribute(String var1, int var2); fun setAttribute(name: String, value: Any?, scope: Int)
public void setAttribute(String var1, Object var2, int var3); fun setBindings(bindings: Bindings?, scope: Int)
public void setBindings(Bindings var1, int var2); companion object {
const val ENGINE_SCOPE = 100
public void setErrorWriter(Writer var1); const val GLOBAL_SCOPE = 200
}
public void setReader(Reader var1);
public void setWriter(Writer var1);
} }
+24 -19
View File
@@ -1,37 +1,42 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.io.Reader; import java.io.Reader
@SuppressWarnings({"unused", "UnnecessaryModifier"}) interface ScriptEngine {
public interface ScriptEngine { var context: ScriptContext
public static final String FILENAME = "javax.script.filename";
public Bindings createBindings(); fun createBindings(): Bindings?
public Object eval(Reader var1) throws ScriptException; @Throws(ScriptException::class)
fun eval(reader: Reader): Any?
public Object eval(Reader var1, Bindings var2) throws ScriptException; @Throws(ScriptException::class)
fun eval(reader: Reader, bindings: Bindings): Any?
public Object eval(Reader var1, ScriptContext var2) throws ScriptException; @Throws(ScriptException::class)
fun eval(reader: Reader, context: ScriptContext): Any?
public Object eval(String var1) throws ScriptException; @Throws(ScriptException::class)
fun eval(script: String): Any?
public Object eval(String var1, Bindings var2) throws ScriptException; @Throws(ScriptException::class)
fun eval(script: String, bindings: Bindings): Any?
public Object eval(String var1, ScriptContext var2) throws ScriptException; @Throws(ScriptException::class)
fun eval(script: String, context: ScriptContext): Any?
public Object get(String var1); operator fun get(key: String): Any?
public Bindings getBindings(int var1); fun getBindings(scope: Int): Bindings?
public ScriptContext getContext(); fun put(key: String, value: Any?)
public void put(String var1, Object var2); fun setBindings(bindings: Bindings?, scope: Int)
public void setBindings(Bindings var1, int var2); companion object {
const val FILENAME = "javax.script.filename"
public void setContext(ScriptContext var1); }
} }
@@ -1,67 +1,54 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
@SuppressWarnings("unused") class ScriptException : Exception {
public class ScriptException extends Exception { var columnNumber: Int
private int columnNumber; private set
private String fileName; var fileName: String?
private int lineNumber; private set
var lineNumber: Int
private set
public ScriptException(String s) { constructor(s: String?) : super(s) {
super(s); fileName = null
this.fileName = null; lineNumber = -1
this.lineNumber = -1; columnNumber = -1
this.columnNumber = -1;
} }
public ScriptException(Exception e) { constructor(e: Exception?) : super(e) {
super(e); fileName = null
this.fileName = null; lineNumber = -1
this.lineNumber = -1; columnNumber = -1
this.columnNumber = -1;
} }
public ScriptException(String message, String fileName2, int lineNumber2) { constructor(message: String?, fileName2: String?, lineNumber2: Int) : super(message) {
super(message); fileName = fileName2
this.fileName = fileName2; lineNumber = lineNumber2
this.lineNumber = lineNumber2; columnNumber = -1
this.columnNumber = -1;
} }
public ScriptException(String message, String fileName2, int lineNumber2, int columnNumber2) { constructor(message: String?, fileName2: String?, lineNumber2: Int, columnNumber2: Int) : super(
super(message); message
this.fileName = fileName2; ) {
this.lineNumber = lineNumber2; fileName = fileName2
this.columnNumber = columnNumber2; lineNumber = lineNumber2
columnNumber = columnNumber2
} }
@Override override val message: String
public String getMessage() { get() {
String ret = super.getMessage(); val ret = super.message
if (this.fileName == null) { if (fileName == null) {
return ret; return ret!!
}
var ret2 = ret + " in " + fileName
if (lineNumber != -1) {
ret2 = ret2 + " at line number " + lineNumber
}
return if (columnNumber != -1) {
ret2 + " at column number " + columnNumber
} else ret2
} }
String ret2 = ret + " in " + this.fileName;
if (this.lineNumber != -1) {
ret2 = ret2 + " at line number " + this.lineNumber;
}
if (this.columnNumber != -1) {
return ret2 + " at column number " + this.columnNumber;
}
return ret2;
}
public int getLineNumber() {
return this.lineNumber;
}
public int getColumnNumber() {
return this.columnNumber;
}
public String getFileName() {
return this.fileName;
}
} }
@@ -1,107 +1,70 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.util.Collection; class SimpleBindings @JvmOverloads constructor(
import java.util.HashMap; private val map: MutableMap<String, Any?> = HashMap()
import java.util.Map; ) : Bindings {
import java.util.Set;
public class SimpleBindings implements Bindings { override fun put(key: String, value: Any?): Any? {
private Map<String, Object> map; checkKey(key)
return map.put(key, value)
public SimpleBindings(Map<String, Object> m) {
if (m == null) {
throw new NullPointerException();
}
this.map = m;
} }
public SimpleBindings() { override fun putAll(from: Map<out String, Any?>) {
this(new HashMap<String, Object>()); for ((key, value) in from) {
} checkKey(key)
this[key] = value
@Override
public Object put(String name, Object value) {
this.checkKey(name);
return this.map.put(name, value);
}
@Override
public void putAll(Map<? extends String, ? extends Object> toMerge) {
if (toMerge == null) {
throw new NullPointerException("toMerge map is null");
}
for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) {
String key = entry.getKey();
this.checkKey(key);
this.put(key, entry.getValue());
} }
} }
@Override override fun clear() {
public void clear() { map.clear()
this.map.clear();
} }
@Override override fun containsKey(key: String): Boolean {
public boolean containsKey(Object key) { checkKey(key)
this.checkKey(key); return map.containsKey(key)
return this.map.containsKey(key);
} }
@Override override fun containsValue(value: Any?): Boolean {
public boolean containsValue(Object value) { return map.containsValue(value)
return this.map.containsValue(value);
} }
@Override override val entries: MutableSet<MutableMap.MutableEntry<String, Any?>>
public Set<Map.Entry<String, Object>> entrySet() { get() = map.entries
return this.map.entrySet();
override operator fun get(key: String): Any? {
checkKey(key)
return map[key]
} }
@Override override fun isEmpty(): Boolean {
public Object get(Object key) { return map.isEmpty()
this.checkKey(key);
return this.map.get(key);
} }
@Override override val keys: MutableSet<String>
public boolean isEmpty() { get() = map.keys
return this.map.isEmpty();
override fun remove(key: String): Any? {
checkKey(key)
return map.remove(key)
} }
@Override override val size: Int
public Set<String> keySet() { get() = map.size
return this.map.keySet();
}
@Override override val values: MutableCollection<Any?>
public Object remove(Object key) { get() = map.values
this.checkKey(key);
return this.map.remove(key);
}
@Override private fun checkKey(key: Any?) {
public int size() {
return this.map.size();
}
@Override
public Collection<Object> values() {
return this.map.values();
}
private void checkKey(Object key) {
if (key == null) { if (key == null) {
throw new NullPointerException("key can not be null"); throw NullPointerException("key can not be null")
} }
if (!(key instanceof String)) { if (key !is String) {
throw new ClassCastException("key should be a String"); throw ClassCastException("key should be a String")
}
if (key.equals("")) {
throw new IllegalArgumentException("key can not be empty");
} }
require(key != "") { "key can not be empty" }
} }
} }
@@ -1,166 +1,119 @@
/* /*
* Decompiled with CFR 0.152. * Decompiled with CFR 0.152.
*/ */
package com.script; package com.script
import java.io.InputStreamReader; import java.io.InputStreamReader
import java.io.PrintWriter; import java.io.PrintWriter
import java.io.Reader; import java.io.Reader
import java.io.Writer; import java.io.Writer
import java.util.ArrayList; import java.util.*
import java.util.Collections;
import java.util.List;
public class SimpleScriptContext implements ScriptContext { open class SimpleScriptContext : ScriptContext {
private static List<Integer> scopes = new ArrayList<Integer>(2); private var engineScope: Bindings = SimpleBindings()
protected Bindings engineScope = new SimpleBindings(); override var errorWriter: Writer = PrintWriter(System.err, true)
protected Writer errorWriter = new PrintWriter(System.err, true); private var globalScope: Bindings? = null
protected Bindings globalScope = null; override var reader: Reader = InputStreamReader(System.`in`)
protected Reader reader = new InputStreamReader(System.in); override var writer: Writer = PrintWriter(System.out, true)
protected Writer writer = new PrintWriter(System.out, true); override fun setBindings(bindings: Bindings?, scope: Int) {
when (scope) {
@Override 100 -> {
public void setBindings(Bindings bindings, int scope) {
switch (scope) {
case 100: {
if (bindings == null) { if (bindings == null) {
throw new NullPointerException("Engine scope cannot be null."); throw NullPointerException("Engine scope cannot be null.")
} }
this.engineScope = bindings; engineScope = bindings
return; return
} }
case 200: { 200 -> {
this.globalScope = bindings; globalScope = bindings
return; return
} }
} }
throw new IllegalArgumentException("Invalid scope value."); throw IllegalArgumentException("Invalid scope value.")
} }
@Override override fun getAttribute(name: String): Any? {
public Object getAttribute(String name) { if (engineScope.containsKey(name)) {
if (this.engineScope.containsKey(name)) { return this.getAttribute(name, 100)
return this.getAttribute(name, 100);
} }
if (this.globalScope == null || !this.globalScope.containsKey(name)) { return if (globalScope == null || !globalScope!!.containsKey(name)) {
return null; null
} } else this.getAttribute(name, 200)
return this.getAttribute(name, 200);
} }
@Override override fun getAttribute(name: String, scope: Int): Any? {
public Object getAttribute(String name, int scope) { when (scope) {
switch (scope) { 100 -> {
case 100: { return engineScope[name]
return this.engineScope.get(name);
} }
case 200: { 200 -> {
if (this.globalScope != null) { return if (globalScope != null) {
return this.globalScope.get(name); globalScope!![name]
} } else null
return null;
} }
} }
throw new IllegalArgumentException("Illegal scope value."); throw IllegalArgumentException("Illegal scope value.")
} }
@Override override fun removeAttribute(name: String, scope: Int): Any? {
public Object removeAttribute(String name, int scope) { when (scope) {
switch (scope) { 100 -> {
case 100: { return if (getBindings(100) != null) {
if (this.getBindings(100) != null) { getBindings(100)!!.remove(name)
return this.getBindings(100).remove(name); } else null
}
return null;
} }
case 200: { 200 -> {
if (this.getBindings(200) != null) { return if (getBindings(200) != null) {
return this.getBindings(200).remove(name); getBindings(200)!!.remove(name)
} } else null
return null;
} }
} }
throw new IllegalArgumentException("Illegal scope value."); throw IllegalArgumentException("Illegal scope value.")
} }
@Override override fun setAttribute(name: String, value: Any?, scope: Int) {
public void setAttribute(String name, Object value, int scope) { when (scope) {
switch (scope) { 100 -> {
case 100: { engineScope[name] = value
this.engineScope.put(name, value); return
return;
} }
case 200: { 200 -> {
if (this.globalScope != null) { globalScope?.put(name, value)
this.globalScope.put(name, value); return
return;
}
return;
} }
} }
throw new IllegalArgumentException("Illegal scope value."); throw IllegalArgumentException("Illegal scope value.")
} }
@Override override fun getAttributesScope(name: String): Int {
public Writer getWriter() { if (engineScope.containsKey(name)) {
return this.writer; return 100
}
@Override
public Reader getReader() {
return this.reader;
}
@Override
public void setReader(Reader reader2) {
this.reader = reader2;
}
@Override
public void setWriter(Writer writer2) {
this.writer = writer2;
}
@Override
public Writer getErrorWriter() {
return this.errorWriter;
}
@Override
public void setErrorWriter(Writer writer2) {
this.errorWriter = writer2;
}
@Override
public int getAttributesScope(String name) {
if (this.engineScope.containsKey(name)) {
return 100;
} }
if (this.globalScope == null || !this.globalScope.containsKey(name)) { return if (globalScope == null || !globalScope!!.containsKey(name)) {
return -1; -1
} } else 200
return 200;
} }
@Override override fun getBindings(scope: Int): Bindings? {
public Bindings getBindings(int scope) {
if (scope == 100) { if (scope == 100) {
return this.engineScope; return engineScope
} }
if (scope == 200) { if (scope == 200) {
return this.globalScope; return globalScope
} }
throw new IllegalArgumentException("Illegal scope value."); throw IllegalArgumentException("Illegal scope value.")
} }
@Override override val scopes: List<Int>
public List<Integer> getScopes() { get() = Companion.scopes
return scopes;
}
static { companion object {
scopes.add(100); private var scopes: MutableList<Int> = ArrayList(2)
scopes.add(200);
scopes = Collections.unmodifiableList(scopes); init {
scopes.add(100)
scopes.add(200)
scopes = Collections.unmodifiableList(scopes)
}
} }
} }
@@ -43,17 +43,17 @@ import java.security.PrivilegedExceptionAction
@Suppress("UNUSED_PARAMETER") @Suppress("UNUSED_PARAMETER")
open class InterfaceImplementor(private val engine: Invocable) { open class InterfaceImplementor(private val engine: Invocable) {
@Throws(ScriptException::class) @Throws(ScriptException::class)
fun <T> getInterface(thiz: Any?, iface: Class<T>?): T? { fun <T> getInterface(obj: Any?, clazz: Class<T>?): T? {
return if (iface != null && iface.isInterface) { return if (clazz != null && clazz.isInterface) {
if (!isImplemented(thiz, iface)) { if (!isImplemented(obj, clazz)) {
null null
} else { } else {
val accContext = AccessController.getContext() val accContext = AccessController.getContext()
iface.cast( clazz.cast(
Proxy.newProxyInstance( Proxy.newProxyInstance(
iface.classLoader, clazz.classLoader,
arrayOf<Class<*>>(iface), arrayOf<Class<*>>(clazz),
InterfaceImplementorInvocationHandler(thiz, accContext) InterfaceImplementorInvocationHandler(obj, accContext)
) )
) )
} }
@@ -62,12 +62,12 @@ open class InterfaceImplementor(private val engine: Invocable) {
} }
} }
protected open fun isImplemented(thiz: Any?, iface: Class<*>): Boolean { protected open fun isImplemented(obj: Any?, clazz: Class<*>): Boolean {
return true return true
} }
@Throws(ScriptException::class) @Throws(ScriptException::class)
protected open fun convertResult(method: Method?, res: Any): Any? { protected open fun convertResult(method: Method?, res: Any?): Any? {
return res return res
} }
@@ -77,7 +77,7 @@ open class InterfaceImplementor(private val engine: Invocable) {
} }
private inner class InterfaceImplementorInvocationHandler( private inner class InterfaceImplementorInvocationHandler(
private val thiz: Any?, private val obj: Any?,
private val accContext: AccessControlContext private val accContext: AccessControlContext
) : InvocationHandler { ) : InvocationHandler {
@@ -85,13 +85,9 @@ open class InterfaceImplementor(private val engine: Invocable) {
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? { override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
val finalArgs = convertArguments(method, args) val finalArgs = convertArguments(method, args)
val result = AccessController.doPrivileged(PrivilegedExceptionAction { val result = AccessController.doPrivileged(PrivilegedExceptionAction {
if (thiz == null) engine.invokeFunction( if (obj == null) engine.invokeFunction(method.name, *finalArgs)
method.name, else engine.invokeMethod(obj, method.name, *finalArgs)
*finalArgs }, accContext)
) else engine.invokeMethod(
thiz, method.name, *finalArgs
)
} as PrivilegedExceptionAction<Any>, accContext)
return convertResult(method, result) return convertResult(method, result)
} }
@@ -41,6 +41,10 @@ internal class RhinoCompiledScript(
private val script: Script private val script: Script
) : CompiledScript() { ) : CompiledScript() {
override fun getEngine(): ScriptEngine {
return engine
}
@Throws(ScriptException::class) @Throws(ScriptException::class)
override fun eval(context: ScriptContext): Any? { override fun eval(context: ScriptContext): Any? {
val cx = Context.enter() val cx = Context.enter()
@@ -65,7 +69,4 @@ internal class RhinoCompiledScript(
return result return result
} }
override fun getEngine(): ScriptEngine {
return engine
}
} }
@@ -49,11 +49,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
private val implementor: InterfaceImplementor private val implementor: InterfaceImplementor
@Throws(ScriptException::class) @Throws(ScriptException::class)
override fun eval(reader: Reader, ctxt: ScriptContext): Any? { override fun eval(reader: Reader, context: ScriptContext): Any? {
val cx = Context.enter() val cx = Context.enter()
val ret: Any? val ret: Any?
try { try {
val scope = getRuntimeScope(ctxt) val scope = getRuntimeScope(context)
var filename = this["javax.script.filename"] as? String var filename = this["javax.script.filename"] as? String
filename = filename ?: "<Unknown source>" filename = filename ?: "<Unknown source>"
ret = cx.evaluateReader(scope, reader, filename, 1, null) ret = cx.evaluateReader(scope, reader, filename, 1, null)
@@ -76,12 +76,8 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
} }
@Throws(ScriptException::class) @Throws(ScriptException::class)
override fun eval(script: String?, ctxt: ScriptContext): Any? { override fun eval(script: String, context: ScriptContext): Any? {
return if (script == null) { return this.eval(StringReader(script) as Reader, context)
throw NullPointerException("null script")
} else {
this.eval(StringReader(script) as Reader, ctxt)
}
} }
override fun createBindings(): Bindings { override fun createBindings(): Bindings {
@@ -94,11 +90,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
} }
@Throws(ScriptException::class, NoSuchMethodException::class) @Throws(ScriptException::class, NoSuchMethodException::class)
override fun invokeMethod(thiz: Any?, name: String, vararg args: Any): Any? { override fun invokeMethod(obj: Any?, name: String, vararg args: Any): Any? {
return if (thiz == null) { return if (obj == null) {
throw IllegalArgumentException("脚本对象不能为空") throw IllegalArgumentException("脚本对象不能为空")
} else { } else {
this.invoke(thiz, name, *args) this.invoke(obj, name, *args)
} }
} }
@@ -136,20 +132,20 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
return var11 return var11
} }
override fun <T> getInterface(clasz: Class<T>): T? { override fun <T> getInterface(clazz: Class<T>): T? {
return try { return try {
implementor.getInterface(null, clasz) implementor.getInterface(null, clazz)
} catch (var3: ScriptException) { } catch (var3: ScriptException) {
null null
} }
} }
override fun <T> getInterface(thiz: Any?, clasz: Class<T>): T? { override fun <T> getInterface(obj: Any?, paramClass: Class<T>): T? {
return if (thiz == null) { return if (obj == null) {
throw IllegalArgumentException("脚本对象不能为空") throw IllegalArgumentException("脚本对象不能为空")
} else { } else {
try { try {
implementor.getInterface(thiz, clasz) implementor.getInterface(obj, paramClass)
} catch (var4: ScriptException) { } catch (var4: ScriptException) {
null null
} }
@@ -224,27 +220,26 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
indexedProps = HashMap() indexedProps = HashMap()
implementor = object : InterfaceImplementor(this) { implementor = object : InterfaceImplementor(this) {
override fun isImplemented(thiz: Any?, iface: Class<*>): Boolean { override fun isImplemented(obj: Any?, clazz: Class<*>): Boolean {
var thiz1 = thiz var obj1 = obj
return try { return try {
if (thiz1 != null && thiz1 !is Scriptable) { if (obj1 != null && obj1 !is Scriptable) {
thiz1 = Context.toObject( obj1 = Context.toObject(obj1, topLevel)
thiz1,
topLevel
)
} }
val engineScope = val engineScope = getRuntimeScope(context)
getRuntimeScope(context) val localScope = if (obj1 != null) obj1 as Scriptable else engineScope
val localScope = val methods = clazz.methods
if (thiz1 != null) thiz1 as Scriptable else engineScope val methodsSize = methods.size
val var5 = iface.methods for (index in 0 until methodsSize) {
val var6 = var5.size val method = methods[index]
for (var7 in 0 until var6) {
val method = var5[var7]
if (method.declaringClass != Any::class.java) { if (method.declaringClass != Any::class.java) {
val obj = if (ScriptableObject.getProperty(
ScriptableObject.getProperty(localScope, method.name) as? Function localScope,
obj ?: return false method.name
) !is Function
) {
return false
}
} }
} }
true true
@@ -253,7 +248,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
} }
} }
override fun convertResult(method: Method?, res: Any): Any? { override fun convertResult(method: Method?, res: Any?): Any? {
method ?: return null method ?: return null
val desiredType = method.returnType val desiredType = method.returnType
if (desiredType == Void.TYPE) return null if (desiredType == Void.TYPE) return null