From 9b814b7665d89d21f093e96ae69f92a47898c581 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 25 Mar 2023 16:09:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/script/AbstractScriptEngine.kt | 125 +++++------ rhino/src/main/java/com/script/Bindings.kt | 24 +-- rhino/src/main/java/com/script/Compilable.kt | 16 +- .../main/java/com/script/CompiledScript.kt | 38 ++-- rhino/src/main/java/com/script/Invocable.kt | 17 +- .../src/main/java/com/script/ScriptContext.kt | 46 ++-- .../src/main/java/com/script/ScriptEngine.kt | 45 ++-- .../main/java/com/script/ScriptException.kt | 93 ++++---- .../main/java/com/script/SimpleBindings.kt | 121 ++++------- .../java/com/script/SimpleScriptContext.kt | 203 +++++++----------- .../com/script/rhino/InterfaceImplementor.kt | 30 ++- .../com/script/rhino/RhinoCompiledScript.kt | 7 +- .../com/script/rhino/RhinoScriptEngine.kt | 65 +++--- 13 files changed, 350 insertions(+), 480 deletions(-) diff --git a/rhino/src/main/java/com/script/AbstractScriptEngine.kt b/rhino/src/main/java/com/script/AbstractScriptEngine.kt index c810e5a0a..aa21f0209 100644 --- a/rhino/src/main/java/com/script/AbstractScriptEngine.kt +++ b/rhino/src/main/java/com/script/AbstractScriptEngine.kt @@ -1,111 +1,86 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.io.Reader; +import java.io.Reader -@SuppressWarnings("unused") -public abstract class AbstractScriptEngine implements ScriptEngine { +abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngine { - protected ScriptContext context = new SimpleScriptContext(); + override var context: ScriptContext = SimpleScriptContext() - public AbstractScriptEngine() { - } - - public AbstractScriptEngine(Bindings n) { - this(); - if (n == null) { - throw new NullPointerException("n is null"); + init { + bindings?.let { + context.setBindings(bindings, 100) } - this.context.setBindings(n, 100); } - @Override - 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) { + override fun getBindings(scope: Int): Bindings? { if (scope == 200) { - return this.context.getBindings(200); + return context.getBindings(200) } if (scope == 100) { - return this.context.getBindings(100); + return context.getBindings(100) } - throw new IllegalArgumentException("Invalid scope value."); + throw IllegalArgumentException("Invalid scope value.") } - @Override - public void setBindings(Bindings bindings, int scope) { - if (scope == 200) { - this.context.setBindings(bindings, 200); - } else if (scope == 100) { - this.context.setBindings(bindings, 100); - } else { - throw new IllegalArgumentException("Invalid scope value."); + override fun setBindings(bindings: Bindings?, scope: Int) { + when (scope) { + 200 -> { + context.setBindings(bindings, 200) + } + 100 -> { + context.setBindings(bindings, 100) + } + else -> { + throw IllegalArgumentException("Invalid scope value.") + } } } - @Override - public void put(String key, Object value) { - Bindings nn = this.getBindings(100); + override fun put(key: String, value: Any?) { + val nn = getBindings(100) if (nn != null) { - nn.put(key, value); + nn[key] = value } } - @Override - public Object get(String key) { - Bindings nn = this.getBindings(100); - if (nn != null) { - return nn.get(key); - } - return null; + override fun get(key: String): Any? { + val nn = getBindings(100) + return nn?.get(key) } - @Override - public Object eval(Reader reader, Bindings bindings) throws ScriptException { - return this.eval(reader, this.getScriptContext(bindings)); + @Throws(ScriptException::class) + override fun eval(reader: Reader, bindings: Bindings): Any? { + return this.eval(reader, getScriptContext(bindings)) } - @Override - public Object eval(String script, Bindings bindings) throws ScriptException { - return this.eval(script, this.getScriptContext(bindings)); + @Throws(ScriptException::class) + override fun eval(script: String, bindings: Bindings): Any? { + return this.eval(script, getScriptContext(bindings)) } - @Override - public Object eval(Reader reader) throws ScriptException { - return this.eval(reader, this.context); + @Throws(ScriptException::class) + override fun eval(reader: Reader): Any? { + return this.eval(reader, context) } - @Override - public Object eval(String script) throws ScriptException { - return this.eval(script, this.context); + @Throws(ScriptException::class) + override fun eval(script: String): Any? { + return this.eval(script, context) } - protected ScriptContext getScriptContext(Bindings nn) { - SimpleScriptContext ctxt = new SimpleScriptContext(); - Bindings gs = this.getBindings(200); + protected fun getScriptContext(nn: Bindings): ScriptContext { + val ctx = SimpleScriptContext() + val gs = getBindings(200) if (gs != null) { - ctxt.setBindings(gs, 200); + ctx.setBindings(gs, 200) } - if (nn != null) { - ctxt.setBindings(nn, 100); - ctxt.setReader(this.context.getReader()); - ctxt.setWriter(this.context.getWriter()); - ctxt.setErrorWriter(this.context.getErrorWriter()); - return ctxt; - } - throw new NullPointerException("Engine scope Bindings may not be null."); + ctx.setBindings(nn, 100) + ctx.reader = context.reader + ctx.writer = context.writer + ctx.errorWriter = context.errorWriter + return ctx } -} +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/Bindings.kt b/rhino/src/main/java/com/script/Bindings.kt index 983547a1a..bb035ba8b 100644 --- a/rhino/src/main/java/com/script/Bindings.kt +++ b/rhino/src/main/java/com/script/Bindings.kt @@ -1,24 +1,18 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.util.Map; +interface Bindings : MutableMap { -@SuppressWarnings("UnnecessaryModifier") -public interface Bindings extends Map { - @Override - public boolean containsKey(Object var1); + override fun containsKey(key: String): Boolean - @Override - public Object get(Object var1); + override operator fun get(key: String): Any? - @Override - public Object put(String var1, Object var2); + override fun put(key: String, value: Any?): Any? - @Override - public void putAll(Map var1); + override fun putAll(from: Map) - @Override - public Object remove(Object var1); -} + override fun remove(key: String): Any? + +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/Compilable.kt b/rhino/src/main/java/com/script/Compilable.kt index 485ee0842..ed2b45e12 100644 --- a/rhino/src/main/java/com/script/Compilable.kt +++ b/rhino/src/main/java/com/script/Compilable.kt @@ -1,13 +1,15 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.io.Reader; +import java.io.Reader -@SuppressWarnings("UnnecessaryModifier") -public interface Compilable { - public CompiledScript compile(Reader var1) throws ScriptException; +interface Compilable { - public CompiledScript compile(String var1) throws ScriptException; -} + @Throws(ScriptException::class) + fun compile(script: Reader): CompiledScript + + @Throws(ScriptException::class) + fun compile(script: String): CompiledScript +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/CompiledScript.kt b/rhino/src/main/java/com/script/CompiledScript.kt index 1cf0eff0c..29a056d83 100644 --- a/rhino/src/main/java/com/script/CompiledScript.kt +++ b/rhino/src/main/java/com/script/CompiledScript.kt @@ -1,28 +1,32 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -public abstract class CompiledScript { - public abstract Object eval(ScriptContext var1) throws ScriptException; +abstract class CompiledScript { - public abstract ScriptEngine getEngine(); + abstract fun getEngine(): ScriptEngine - public Object eval(Bindings bindings) throws ScriptException { - ScriptContext ctxt = this.getEngine().getContext(); + @Throws(ScriptException::class) + abstract fun eval(context: ScriptContext): Any? + + @Throws(ScriptException::class) + fun eval(bindings: Bindings?): Any? { + var ctxt = getEngine().context if (bindings != null) { - SimpleScriptContext tempctxt = new SimpleScriptContext(); - tempctxt.setBindings(bindings, 100); - tempctxt.setBindings(ctxt.getBindings(200), 200); - tempctxt.setWriter(ctxt.getWriter()); - tempctxt.setReader(ctxt.getReader()); - tempctxt.setErrorWriter(ctxt.getErrorWriter()); - ctxt = tempctxt; + val tempContext = SimpleScriptContext() + tempContext.setBindings(bindings, 100) + tempContext.setBindings(ctxt.getBindings(200), 200) + tempContext.writer = ctxt.writer + tempContext.reader = ctxt.reader + tempContext.errorWriter = ctxt.errorWriter + ctxt = tempContext } - return this.eval(ctxt); + return this.eval(ctxt) } - public Object eval() throws ScriptException { - return this.eval(this.getEngine().getContext()); + @Throws(ScriptException::class) + fun eval(): Any? { + return this.eval(getEngine().context) } -} +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/Invocable.kt b/rhino/src/main/java/com/script/Invocable.kt index 63113ac5c..7131c7725 100644 --- a/rhino/src/main/java/com/script/Invocable.kt +++ b/rhino/src/main/java/com/script/Invocable.kt @@ -1,15 +1,16 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -@SuppressWarnings({"unused", "UnnecessaryModifier"}) -public interface Invocable { - public T getInterface(Class var1); +interface Invocable { + fun getInterface(clazz: Class): T? - public T getInterface(Object var1, Class var2); + fun getInterface(obj: Any?, paramClass: Class): 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? +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/ScriptContext.kt b/rhino/src/main/java/com/script/ScriptContext.kt index 2cba3cad5..eacc651ff 100644 --- a/rhino/src/main/java/com/script/ScriptContext.kt +++ b/rhino/src/main/java/com/script/ScriptContext.kt @@ -1,43 +1,37 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.io.Reader; -import java.io.Writer; -import java.util.List; +import java.io.Reader +import java.io.Writer -@SuppressWarnings({"unused", "UnnecessaryModifier"}) -public interface ScriptContext { +interface ScriptContext { - public static final int ENGINE_SCOPE = 100; - public static final int GLOBAL_SCOPE = 200; + var errorWriter: Writer - public Object getAttribute(String var1); + var reader: Reader - public Object getAttribute(String var1, int var2); + val scopes: List - 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 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); - - public void setErrorWriter(Writer var1); - - public void setReader(Reader var1); - - public void setWriter(Writer var1); -} + companion object { + const val ENGINE_SCOPE = 100 + const val GLOBAL_SCOPE = 200 + } +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/ScriptEngine.kt b/rhino/src/main/java/com/script/ScriptEngine.kt index 809dc3ad6..2a895bc20 100644 --- a/rhino/src/main/java/com/script/ScriptEngine.kt +++ b/rhino/src/main/java/com/script/ScriptEngine.kt @@ -1,37 +1,42 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.io.Reader; +import java.io.Reader -@SuppressWarnings({"unused", "UnnecessaryModifier"}) -public interface ScriptEngine { - public static final String FILENAME = "javax.script.filename"; +interface ScriptEngine { + var context: ScriptContext - 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); - - public void setContext(ScriptContext var1); -} + companion object { + const val FILENAME = "javax.script.filename" + } +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/ScriptException.kt b/rhino/src/main/java/com/script/ScriptException.kt index f8a558ed4..4737c2222 100644 --- a/rhino/src/main/java/com/script/ScriptException.kt +++ b/rhino/src/main/java/com/script/ScriptException.kt @@ -1,67 +1,54 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -@SuppressWarnings("unused") -public class ScriptException extends Exception { - private int columnNumber; - private String fileName; - private int lineNumber; +class ScriptException : Exception { + var columnNumber: Int + private set + var fileName: String? + private set + var lineNumber: Int + private set - public ScriptException(String s) { - super(s); - this.fileName = null; - this.lineNumber = -1; - this.columnNumber = -1; + constructor(s: String?) : super(s) { + fileName = null + lineNumber = -1 + columnNumber = -1 } - public ScriptException(Exception e) { - super(e); - this.fileName = null; - this.lineNumber = -1; - this.columnNumber = -1; + constructor(e: Exception?) : super(e) { + fileName = null + lineNumber = -1 + columnNumber = -1 } - public ScriptException(String message, String fileName2, int lineNumber2) { - super(message); - this.fileName = fileName2; - this.lineNumber = lineNumber2; - this.columnNumber = -1; + constructor(message: String?, fileName2: String?, lineNumber2: Int) : super(message) { + fileName = fileName2 + lineNumber = lineNumber2 + columnNumber = -1 } - public ScriptException(String message, String fileName2, int lineNumber2, int columnNumber2) { - super(message); - this.fileName = fileName2; - this.lineNumber = lineNumber2; - this.columnNumber = columnNumber2; + constructor(message: String?, fileName2: String?, lineNumber2: Int, columnNumber2: Int) : super( + message + ) { + fileName = fileName2 + lineNumber = lineNumber2 + columnNumber = columnNumber2 } - @Override - public String getMessage() { - String ret = super.getMessage(); - if (this.fileName == null) { - return ret; + override val message: String + get() { + val ret = super.message + if (fileName == null) { + 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; - } -} +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/SimpleBindings.kt b/rhino/src/main/java/com/script/SimpleBindings.kt index 1236b98c0..7fdf28853 100644 --- a/rhino/src/main/java/com/script/SimpleBindings.kt +++ b/rhino/src/main/java/com/script/SimpleBindings.kt @@ -1,107 +1,70 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +class SimpleBindings @JvmOverloads constructor( + private val map: MutableMap = HashMap() +) : Bindings { -public class SimpleBindings implements Bindings { - private Map map; - - public SimpleBindings(Map m) { - if (m == null) { - throw new NullPointerException(); - } - this.map = m; + override fun put(key: String, value: Any?): Any? { + checkKey(key) + return map.put(key, value) } - public SimpleBindings() { - this(new HashMap()); - } - - @Override - public Object put(String name, Object value) { - this.checkKey(name); - return this.map.put(name, value); - } - - @Override - public void putAll(Map toMerge) { - if (toMerge == null) { - throw new NullPointerException("toMerge map is null"); - } - for (Map.Entry entry : toMerge.entrySet()) { - String key = entry.getKey(); - this.checkKey(key); - this.put(key, entry.getValue()); + override fun putAll(from: Map) { + for ((key, value) in from) { + checkKey(key) + this[key] = value } } - @Override - public void clear() { - this.map.clear(); + override fun clear() { + map.clear() } - @Override - public boolean containsKey(Object key) { - this.checkKey(key); - return this.map.containsKey(key); + override fun containsKey(key: String): Boolean { + checkKey(key) + return map.containsKey(key) } - @Override - public boolean containsValue(Object value) { - return this.map.containsValue(value); + override fun containsValue(value: Any?): Boolean { + return map.containsValue(value) } - @Override - public Set> entrySet() { - return this.map.entrySet(); + override val entries: MutableSet> + get() = map.entries + + override operator fun get(key: String): Any? { + checkKey(key) + return map[key] } - @Override - public Object get(Object key) { - this.checkKey(key); - return this.map.get(key); + override fun isEmpty(): Boolean { + return map.isEmpty() } - @Override - public boolean isEmpty() { - return this.map.isEmpty(); + override val keys: MutableSet + get() = map.keys + + override fun remove(key: String): Any? { + checkKey(key) + return map.remove(key) } - @Override - public Set keySet() { - return this.map.keySet(); - } + override val size: Int + get() = map.size - @Override - public Object remove(Object key) { - this.checkKey(key); - return this.map.remove(key); - } + override val values: MutableCollection + get() = map.values - @Override - public int size() { - return this.map.size(); - } - - @Override - public Collection values() { - return this.map.values(); - } - - private void checkKey(Object key) { + private fun checkKey(key: Any?) { if (key == null) { - throw new NullPointerException("key can not be null"); + throw NullPointerException("key can not be null") } - if (!(key instanceof String)) { - throw new ClassCastException("key should be a String"); - } - if (key.equals("")) { - throw new IllegalArgumentException("key can not be empty"); + if (key !is String) { + throw ClassCastException("key should be a String") } + require(key != "") { "key can not be empty" } } -} +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/SimpleScriptContext.kt b/rhino/src/main/java/com/script/SimpleScriptContext.kt index 49343edc7..41dca6077 100644 --- a/rhino/src/main/java/com/script/SimpleScriptContext.kt +++ b/rhino/src/main/java/com/script/SimpleScriptContext.kt @@ -1,166 +1,119 @@ /* * Decompiled with CFR 0.152. */ -package com.script; +package com.script -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.io.Reader; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.io.InputStreamReader +import java.io.PrintWriter +import java.io.Reader +import java.io.Writer +import java.util.* -public class SimpleScriptContext implements ScriptContext { - private static List scopes = new ArrayList(2); - protected Bindings engineScope = new SimpleBindings(); - protected Writer errorWriter = new PrintWriter(System.err, true); - protected Bindings globalScope = null; - protected Reader reader = new InputStreamReader(System.in); - protected Writer writer = new PrintWriter(System.out, true); - - @Override - public void setBindings(Bindings bindings, int scope) { - switch (scope) { - case 100: { +open class SimpleScriptContext : ScriptContext { + private var engineScope: Bindings = SimpleBindings() + override var errorWriter: Writer = PrintWriter(System.err, true) + private var globalScope: Bindings? = null + override var reader: Reader = InputStreamReader(System.`in`) + override var writer: Writer = PrintWriter(System.out, true) + override fun setBindings(bindings: Bindings?, scope: Int) { + when (scope) { + 100 -> { if (bindings == null) { - throw new NullPointerException("Engine scope cannot be null."); + throw NullPointerException("Engine scope cannot be null.") } - this.engineScope = bindings; - return; + engineScope = bindings + return } - case 200: { - this.globalScope = bindings; - return; + 200 -> { + globalScope = bindings + return } } - throw new IllegalArgumentException("Invalid scope value."); + throw IllegalArgumentException("Invalid scope value.") } - @Override - public Object getAttribute(String name) { - if (this.engineScope.containsKey(name)) { - return this.getAttribute(name, 100); + override fun getAttribute(name: String): Any? { + if (engineScope.containsKey(name)) { + return this.getAttribute(name, 100) } - if (this.globalScope == null || !this.globalScope.containsKey(name)) { - return null; - } - return this.getAttribute(name, 200); + return if (globalScope == null || !globalScope!!.containsKey(name)) { + null + } else this.getAttribute(name, 200) } - @Override - public Object getAttribute(String name, int scope) { - switch (scope) { - case 100: { - return this.engineScope.get(name); + override fun getAttribute(name: String, scope: Int): Any? { + when (scope) { + 100 -> { + return engineScope[name] } - case 200: { - if (this.globalScope != null) { - return this.globalScope.get(name); - } - return null; + 200 -> { + return if (globalScope != null) { + globalScope!![name] + } else null } } - throw new IllegalArgumentException("Illegal scope value."); + throw IllegalArgumentException("Illegal scope value.") } - @Override - public Object removeAttribute(String name, int scope) { - switch (scope) { - case 100: { - if (this.getBindings(100) != null) { - return this.getBindings(100).remove(name); - } - return null; + override fun removeAttribute(name: String, scope: Int): Any? { + when (scope) { + 100 -> { + return if (getBindings(100) != null) { + getBindings(100)!!.remove(name) + } else null } - case 200: { - if (this.getBindings(200) != null) { - return this.getBindings(200).remove(name); - } - return null; + 200 -> { + return if (getBindings(200) != null) { + getBindings(200)!!.remove(name) + } else null } } - throw new IllegalArgumentException("Illegal scope value."); + throw IllegalArgumentException("Illegal scope value.") } - @Override - public void setAttribute(String name, Object value, int scope) { - switch (scope) { - case 100: { - this.engineScope.put(name, value); - return; + override fun setAttribute(name: String, value: Any?, scope: Int) { + when (scope) { + 100 -> { + engineScope[name] = value + return } - case 200: { - if (this.globalScope != null) { - this.globalScope.put(name, value); - return; - } - return; + 200 -> { + globalScope?.put(name, value) + return } } - throw new IllegalArgumentException("Illegal scope value."); + throw IllegalArgumentException("Illegal scope value.") } - @Override - public Writer getWriter() { - return this.writer; - } - - @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; + override fun getAttributesScope(name: String): Int { + if (engineScope.containsKey(name)) { + return 100 } - if (this.globalScope == null || !this.globalScope.containsKey(name)) { - return -1; - } - return 200; + return if (globalScope == null || !globalScope!!.containsKey(name)) { + -1 + } else 200 } - @Override - public Bindings getBindings(int scope) { + override fun getBindings(scope: Int): Bindings? { if (scope == 100) { - return this.engineScope; + return engineScope } if (scope == 200) { - return this.globalScope; + return globalScope } - throw new IllegalArgumentException("Illegal scope value."); + throw IllegalArgumentException("Illegal scope value.") } - @Override - public List getScopes() { - return scopes; - } + override val scopes: List + get() = Companion.scopes - static { - scopes.add(100); - scopes.add(200); - scopes = Collections.unmodifiableList(scopes); + companion object { + private var scopes: MutableList = ArrayList(2) + + init { + scopes.add(100) + scopes.add(200) + scopes = Collections.unmodifiableList(scopes) + } } -} +} \ No newline at end of file diff --git a/rhino/src/main/java/com/script/rhino/InterfaceImplementor.kt b/rhino/src/main/java/com/script/rhino/InterfaceImplementor.kt index ef27dc6d9..e8627e45d 100644 --- a/rhino/src/main/java/com/script/rhino/InterfaceImplementor.kt +++ b/rhino/src/main/java/com/script/rhino/InterfaceImplementor.kt @@ -43,17 +43,17 @@ import java.security.PrivilegedExceptionAction @Suppress("UNUSED_PARAMETER") open class InterfaceImplementor(private val engine: Invocable) { @Throws(ScriptException::class) - fun getInterface(thiz: Any?, iface: Class?): T? { - return if (iface != null && iface.isInterface) { - if (!isImplemented(thiz, iface)) { + fun getInterface(obj: Any?, clazz: Class?): T? { + return if (clazz != null && clazz.isInterface) { + if (!isImplemented(obj, clazz)) { null } else { val accContext = AccessController.getContext() - iface.cast( + clazz.cast( Proxy.newProxyInstance( - iface.classLoader, - arrayOf>(iface), - InterfaceImplementorInvocationHandler(thiz, accContext) + clazz.classLoader, + arrayOf>(clazz), + 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 } @Throws(ScriptException::class) - protected open fun convertResult(method: Method?, res: Any): Any? { + protected open fun convertResult(method: Method?, res: Any?): Any? { return res } @@ -77,7 +77,7 @@ open class InterfaceImplementor(private val engine: Invocable) { } private inner class InterfaceImplementorInvocationHandler( - private val thiz: Any?, + private val obj: Any?, private val accContext: AccessControlContext ) : InvocationHandler { @@ -85,13 +85,9 @@ open class InterfaceImplementor(private val engine: Invocable) { override fun invoke(proxy: Any, method: Method, args: Array): Any? { val finalArgs = convertArguments(method, args) val result = AccessController.doPrivileged(PrivilegedExceptionAction { - if (thiz == null) engine.invokeFunction( - method.name, - *finalArgs - ) else engine.invokeMethod( - thiz, method.name, *finalArgs - ) - } as PrivilegedExceptionAction, accContext) + if (obj == null) engine.invokeFunction(method.name, *finalArgs) + else engine.invokeMethod(obj, method.name, *finalArgs) + }, accContext) return convertResult(method, result) } diff --git a/rhino/src/main/java/com/script/rhino/RhinoCompiledScript.kt b/rhino/src/main/java/com/script/rhino/RhinoCompiledScript.kt index 8d7d87e2c..386f91c5c 100644 --- a/rhino/src/main/java/com/script/rhino/RhinoCompiledScript.kt +++ b/rhino/src/main/java/com/script/rhino/RhinoCompiledScript.kt @@ -41,6 +41,10 @@ internal class RhinoCompiledScript( private val script: Script ) : CompiledScript() { + override fun getEngine(): ScriptEngine { + return engine + } + @Throws(ScriptException::class) override fun eval(context: ScriptContext): Any? { val cx = Context.enter() @@ -65,7 +69,4 @@ internal class RhinoCompiledScript( return result } - override fun getEngine(): ScriptEngine { - return engine - } } \ No newline at end of file diff --git a/rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt b/rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt index 5d50150f1..a5ccff77d 100644 --- a/rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt +++ b/rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt @@ -49,11 +49,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { private val implementor: InterfaceImplementor @Throws(ScriptException::class) - override fun eval(reader: Reader, ctxt: ScriptContext): Any? { + override fun eval(reader: Reader, context: ScriptContext): Any? { val cx = Context.enter() val ret: Any? try { - val scope = getRuntimeScope(ctxt) + val scope = getRuntimeScope(context) var filename = this["javax.script.filename"] as? String filename = filename ?: "" ret = cx.evaluateReader(scope, reader, filename, 1, null) @@ -76,12 +76,8 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } @Throws(ScriptException::class) - override fun eval(script: String?, ctxt: ScriptContext): Any? { - return if (script == null) { - throw NullPointerException("null script") - } else { - this.eval(StringReader(script) as Reader, ctxt) - } + override fun eval(script: String, context: ScriptContext): Any? { + return this.eval(StringReader(script) as Reader, context) } override fun createBindings(): Bindings { @@ -94,11 +90,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } @Throws(ScriptException::class, NoSuchMethodException::class) - override fun invokeMethod(thiz: Any?, name: String, vararg args: Any): Any? { - return if (thiz == null) { + override fun invokeMethod(obj: Any?, name: String, vararg args: Any): Any? { + return if (obj == null) { throw IllegalArgumentException("脚本对象不能为空") } else { - this.invoke(thiz, name, *args) + this.invoke(obj, name, *args) } } @@ -136,20 +132,20 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { return var11 } - override fun getInterface(clasz: Class): T? { + override fun getInterface(clazz: Class): T? { return try { - implementor.getInterface(null, clasz) + implementor.getInterface(null, clazz) } catch (var3: ScriptException) { null } } - override fun getInterface(thiz: Any?, clasz: Class): T? { - return if (thiz == null) { + override fun getInterface(obj: Any?, paramClass: Class): T? { + return if (obj == null) { throw IllegalArgumentException("脚本对象不能为空") } else { try { - implementor.getInterface(thiz, clasz) + implementor.getInterface(obj, paramClass) } catch (var4: ScriptException) { null } @@ -224,27 +220,26 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { indexedProps = HashMap() implementor = object : InterfaceImplementor(this) { - override fun isImplemented(thiz: Any?, iface: Class<*>): Boolean { - var thiz1 = thiz + override fun isImplemented(obj: Any?, clazz: Class<*>): Boolean { + var obj1 = obj return try { - if (thiz1 != null && thiz1 !is Scriptable) { - thiz1 = Context.toObject( - thiz1, - topLevel - ) + if (obj1 != null && obj1 !is Scriptable) { + obj1 = Context.toObject(obj1, topLevel) } - val engineScope = - getRuntimeScope(context) - val localScope = - if (thiz1 != null) thiz1 as Scriptable else engineScope - val var5 = iface.methods - val var6 = var5.size - for (var7 in 0 until var6) { - val method = var5[var7] + val engineScope = getRuntimeScope(context) + val localScope = if (obj1 != null) obj1 as Scriptable else engineScope + val methods = clazz.methods + val methodsSize = methods.size + for (index in 0 until methodsSize) { + val method = methods[index] if (method.declaringClass != Any::class.java) { - val obj = - ScriptableObject.getProperty(localScope, method.name) as? Function - obj ?: return false + if (ScriptableObject.getProperty( + localScope, + method.name + ) !is Function + ) { + return false + } } } 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 val desiredType = method.returnType if (desiredType == Void.TYPE) return null