优化
This commit is contained in:
@@ -48,12 +48,12 @@ open class InterfaceImplementor(private val engine: Invocable) {
|
||||
if (!isImplemented(thiz, iface)) {
|
||||
null
|
||||
} else {
|
||||
val accCtxt = AccessController.getContext()
|
||||
val accContext = AccessController.getContext()
|
||||
iface.cast(
|
||||
Proxy.newProxyInstance(
|
||||
iface.classLoader,
|
||||
arrayOf<Class<*>>(iface),
|
||||
InterfaceImplementorInvocationHandler(thiz, accCtxt)
|
||||
InterfaceImplementorInvocationHandler(thiz, accContext)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -67,7 +67,7 @@ open class InterfaceImplementor(private val engine: Invocable) {
|
||||
}
|
||||
|
||||
@Throws(ScriptException::class)
|
||||
protected open fun convertResult(method: Method?, res: Any): Any {
|
||||
protected open fun convertResult(method: Method?, res: Any): Any? {
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -78,10 +78,11 @@ open class InterfaceImplementor(private val engine: Invocable) {
|
||||
|
||||
private inner class InterfaceImplementorInvocationHandler(
|
||||
private val thiz: Any?,
|
||||
private val accCtxt: AccessControlContext
|
||||
private val accContext: AccessControlContext
|
||||
) : InvocationHandler {
|
||||
|
||||
@Throws(Throwable::class)
|
||||
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 result = AccessController.doPrivileged(PrivilegedExceptionAction {
|
||||
if (thiz == null) engine.invokeFunction(
|
||||
@@ -90,8 +91,9 @@ open class InterfaceImplementor(private val engine: Invocable) {
|
||||
) else engine.invokeMethod(
|
||||
thiz, method.name, *finalArgs
|
||||
)
|
||||
} as PrivilegedExceptionAction<Any>, accCtxt)
|
||||
} as PrivilegedExceptionAction<Any>, accContext)
|
||||
return convertResult(method, result)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -156,48 +156,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
fun getRuntimeScope(ctxt: ScriptContext?): Scriptable {
|
||||
return if (ctxt == null) {
|
||||
throw NullPointerException("脚本context为空")
|
||||
} else {
|
||||
val newScope: Scriptable = ExternalScriptable(ctxt, indexedProps)
|
||||
newScope.prototype = topLevel
|
||||
newScope.put("context", newScope, ctxt)
|
||||
/*
|
||||
val cx = Context.enter()
|
||||
try {
|
||||
@Language("js")
|
||||
val js = """
|
||||
function print(str, newline) {
|
||||
if (typeof(str) == 'undefined') {
|
||||
str = 'undefined';
|
||||
} else if (str == null) {
|
||||
str = 'null';
|
||||
}
|
||||
var out = context.getWriter();
|
||||
if (!(out instanceof java.io.PrintWriter))
|
||||
out = new java.io.PrintWriter(out);
|
||||
out.print(String(str));
|
||||
if (newline) out.print('\\n');
|
||||
out.flush();
|
||||
}
|
||||
function println(str) {
|
||||
print(str, true);
|
||||
}
|
||||
""".trimIndent()
|
||||
cx.evaluateString(
|
||||
newScope,
|
||||
js,
|
||||
"print",
|
||||
1,
|
||||
null
|
||||
)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
*/
|
||||
newScope
|
||||
}
|
||||
fun getRuntimeScope(context: ScriptContext): Scriptable {
|
||||
val newScope: Scriptable = ExternalScriptable(context, indexedProps)
|
||||
newScope.prototype = topLevel
|
||||
newScope.put("context", newScope, context)
|
||||
return newScope
|
||||
}
|
||||
|
||||
@Throws(ScriptException::class)
|
||||
@@ -260,6 +223,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
}
|
||||
indexedProps = HashMap()
|
||||
implementor = object : InterfaceImplementor(this) {
|
||||
|
||||
override fun isImplemented(thiz: Any?, iface: Class<*>): Boolean {
|
||||
var thiz1 = thiz
|
||||
return try {
|
||||
@@ -289,12 +253,11 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertResult(method: Method?, res: Any): Any {
|
||||
val desiredType = method!!.returnType
|
||||
return (if (desiredType == Void.TYPE) null else Context.jsToJava(
|
||||
res,
|
||||
desiredType
|
||||
))!!
|
||||
override fun convertResult(method: Method?, res: Any): Any? {
|
||||
method ?: return null
|
||||
val desiredType = method.returnType
|
||||
if (desiredType == Void.TYPE) return null
|
||||
return Context.jsToJava(res, desiredType)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,30 +265,6 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
@Suppress("unused")
|
||||
companion object {
|
||||
|
||||
private const val DEBUG = false
|
||||
|
||||
/*
|
||||
@Language("js")
|
||||
private val printSource = """
|
||||
function print(str, newline) {
|
||||
if (typeof str == "undefined") {
|
||||
str = "undefined";
|
||||
} else if (str == null) {
|
||||
str = "null";
|
||||
}
|
||||
var out = context.getWriter();
|
||||
if (!(out instanceof java.io.PrintWriter))
|
||||
out = new java.io.PrintWriter(out);
|
||||
out.print(String(str));
|
||||
if (newline) out.print("\\n");
|
||||
out.flush();
|
||||
}
|
||||
function println(str) {
|
||||
print(str, true);
|
||||
}
|
||||
""".trimIndent()
|
||||
*/
|
||||
|
||||
init {
|
||||
ContextFactory.initGlobal(object : ContextFactory() {
|
||||
|
||||
@@ -352,16 +291,16 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
thisObj: Scriptable,
|
||||
args: Array<Any>
|
||||
): Any? {
|
||||
var accCtxt: AccessControlContext? = null
|
||||
var accContext: AccessControlContext? = null
|
||||
val global = ScriptableObject.getTopLevelScope(scope)
|
||||
val globalProto = global.prototype
|
||||
if (globalProto is RhinoTopLevel) {
|
||||
accCtxt = globalProto.accessContext
|
||||
accContext = globalProto.accessContext
|
||||
}
|
||||
return if (accCtxt != null) AccessController.doPrivileged(
|
||||
return if (accContext != null) AccessController.doPrivileged(
|
||||
PrivilegedAction {
|
||||
superDoTopCall(callable, cx, scope, thisObj, args)
|
||||
} as PrivilegedAction<*>, accCtxt) else superDoTopCall(
|
||||
} as PrivilegedAction<*>, accContext) else superDoTopCall(
|
||||
callable,
|
||||
cx,
|
||||
scope,
|
||||
|
||||
Reference in New Issue
Block a user