代码优化

This commit is contained in:
HapeLee
2026-05-14 15:37:26 +08:00
parent 4d3ae1e537
commit 7fda394d74
12 changed files with 301 additions and 147 deletions
@@ -2,9 +2,10 @@ package com.script.rhino
import org.mozilla.javascript.NativeJavaObject
import org.mozilla.javascript.Scriptable
import org.mozilla.javascript.lc.type.TypeInfoFactory
class ReadOnlyJavaObject(scope: Scriptable?, javaObject: Any, staticType: Class<*>?) :
NativeJavaObject(scope, javaObject, staticType) {
NativeJavaObject(scope, javaObject, staticType?.let { TypeInfoFactory.GLOBAL.create(it) }) {
override fun has(name: String, start: Scriptable): Boolean {
if (name.length > 3 && name.startsWith("set")) {
@@ -1,20 +1,31 @@
package com.script.rhino
import org.mozilla.javascript.VMBridge
import org.mozilla.javascript.Context
import org.mozilla.javascript.ContextFactory
object VMBridgeReflect {
val instance: VMBridge by lazy {
VMBridge::class.java.getDeclaredField("instance").apply {
isAccessible = true
}.get(null) as VMBridge
}
val contextLocal: ThreadLocal<Any> by lazy {
@Suppress("UNCHECKED_CAST")
instance::class.java.getDeclaredField("contextLocal").apply {
isAccessible = true
}.get(null) as ThreadLocal<Any>
try {
// Rhino 1.9.1+ uses threadContextLocal directly in Context class
val field = Context::class.java.getDeclaredField("threadContextLocal")
field.isAccessible = true
field.get(null) as ThreadLocal<Any>
} catch (e: Exception) {
try {
// Fallback for some versions or custom builds
val factory = ContextFactory.getGlobal()
val field = ContextFactory::class.java.getDeclaredField("localContext")
field.isAccessible = true
field.get(factory) as ThreadLocal<Any>
} catch (e2: Exception) {
// Older versions used threadContextHelper (Object) which held a ThreadLocal
val field = Context::class.java.getDeclaredField("threadContextHelper")
field.isAccessible = true
field.get(null) as ThreadLocal<Any>
}
}
}
}