优化
This commit is contained in:
@@ -39,4 +39,8 @@ class ScriptBindings : NativeObject() {
|
||||
}
|
||||
}
|
||||
|
||||
fun put(key: String, value: Any?) {
|
||||
set(key, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.script
|
||||
|
||||
import org.mozilla.javascript.Context
|
||||
|
||||
inline fun buildScriptBindings(block: (bindings: ScriptBindings) -> Unit): ScriptBindings {
|
||||
val bindings = ScriptBindings()
|
||||
Context.enter()
|
||||
try {
|
||||
block(bindings)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
return bindings
|
||||
}
|
||||
@@ -55,7 +55,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
|
||||
fun eval(js: String, bindingsConfig: ScriptBindings.() -> Unit = {}): Any? {
|
||||
val bindings = ScriptBindings()
|
||||
bindings.apply(bindingsConfig)
|
||||
Context.enter()
|
||||
try {
|
||||
bindings.apply(bindingsConfig)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
return eval(js, bindings)
|
||||
}
|
||||
|
||||
@@ -249,7 +254,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
return newScope
|
||||
}
|
||||
|
||||
override fun getRuntimeScope(bindings: ScriptBindings): ScriptBindings {
|
||||
override fun getRuntimeScope(bindings: ScriptBindings): Scriptable {
|
||||
val cx = Context.enter()
|
||||
try {
|
||||
bindings.prototype = cx.initStandardObjects()
|
||||
|
||||
Reference in New Issue
Block a user