This commit is contained in:
kunfei
2023-03-25 13:06:09 +08:00
parent 2e7ce425a6
commit 614ec3fcbc
4 changed files with 22 additions and 38 deletions

View File

@@ -35,5 +35,5 @@ android {
}
dependencies {
api(fileTree(dir: 'lib', include: ['rhino-1.7.13-2.jar']))
api(fileTree(dir: 'lib', include: ['rhino-1.7.14-2.jar']))
}

View File

@@ -34,7 +34,16 @@ import org.mozilla.javascript.ClassShutter
* @author A. Sundararajan
* @since 1.6
*/
internal class RhinoClassShutter private constructor() : ClassShutter {
object RhinoClassShutter : ClassShutter {
private val protectedClasses by lazy {
val protectedClasses = HashMap<Any, Any>()
protectedClasses["java.lang.Runtime"] = java.lang.Boolean.TRUE
protectedClasses["java.io.File"] = java.lang.Boolean.TRUE
protectedClasses["java.security.AccessController"] = java.lang.Boolean.TRUE
protectedClasses
}
override fun visibleToScripts(fullClassName: String): Boolean {
val sm = System.getSecurityManager()
if (sm != null) {
@@ -50,22 +59,4 @@ internal class RhinoClassShutter private constructor() : ClassShutter {
return protectedClasses[fullClassName] == null
}
companion object {
@JvmStatic
val protectedClasses by lazy {
val protectedClasses = HashMap<Any, Any>()
protectedClasses["java.lang.Runtime"] = java.lang.Boolean.TRUE
protectedClasses["java.io.File"] = java.lang.Boolean.TRUE
protectedClasses["java.security.AccessController"] = java.lang.Boolean.TRUE
protectedClasses
}
@JvmStatic
val instance: ClassShutter by lazy {
val theInstance = RhinoClassShutter()
theInstance
}
}
}

View File

@@ -25,7 +25,6 @@
package com.script.rhino
import com.script.*
import com.script.rhino.RhinoClassShutter.Companion.instance
import org.intellij.lang.annotations.Language
import org.mozilla.javascript.*
import org.mozilla.javascript.Function
@@ -331,11 +330,18 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
val cx = super.makeContext()
cx.languageVersion = 200
cx.optimizationLevel = -1
cx.setClassShutter(instance)
cx.wrapFactory = RhinoWrapFactory.instance
cx.setClassShutter(RhinoClassShutter)
cx.wrapFactory = RhinoWrapFactory
return cx
}
override fun hasFeature(cx: Context?, featureIndex: Int): Boolean {
if (featureIndex == Context.FEATURE_ENABLE_JAVA_MAP_ACCESS) {
return true
}
return super.hasFeature(cx, featureIndex)
}
override fun doTopCall(
callable: Callable,
cx: Context,

View File

@@ -44,7 +44,7 @@ import java.lang.reflect.Modifier
* @author A. Sundararajan
* @since 1.6
*/
internal class RhinoWrapFactory private constructor() : WrapFactory() {
object RhinoWrapFactory : WrapFactory() {
override fun wrapAsJavaObject(
cx: Context,
@@ -54,7 +54,7 @@ internal class RhinoWrapFactory private constructor() : WrapFactory() {
): Scriptable? {
scope?.delete("Packages")
val sm = System.getSecurityManager()
val classShutter = RhinoClassShutter.instance
val classShutter = RhinoClassShutter
return if (javaObject is ClassLoader) {
sm?.checkPermission(RuntimePermission("getClassLoader"))
super.wrapAsJavaObject(cx, scope, javaObject, staticType)
@@ -115,17 +115,4 @@ internal class RhinoWrapFactory private constructor() : WrapFactory() {
}
}
companion object {
private var theInstance: RhinoWrapFactory? = null
@JvmStatic
@get:Synchronized
val instance: WrapFactory?
get() {
if (theInstance == null) {
theInstance = RhinoWrapFactory()
}
return theInstance
}
}
}