This commit is contained in:
Horis
2023-06-10 15:23:25 +08:00
parent 45c88716f3
commit d6ff9d06ad
9 changed files with 128 additions and 37 deletions

View File

@@ -32,9 +32,11 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
200 -> {
context.setBindings(bindings, 200)
}
100 -> {
context.setBindings(bindings, 100)
}
else -> {
throw IllegalArgumentException("Invalid scope value.")
}
@@ -84,15 +86,11 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
}
override fun getScriptContext(bindings: Bindings): ScriptContext {
val ctx = SimpleScriptContext()
val ctx = SimpleScriptContext(bindings, context.errorWriter, context.reader, context.writer)
val gs = getBindings(200)
if (gs != null) {
ctx.setBindings(gs, 200)
}
ctx.setBindings(bindings, 100)
ctx.reader = context.reader
ctx.writer = context.writer
ctx.errorWriter = context.errorWriter
return ctx
}
}
}

View File

@@ -9,12 +9,14 @@ import java.io.Reader
import java.io.Writer
import java.util.*
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`)
open class SimpleScriptContext(
private var engineScope: Bindings = SimpleBindings(),
override var errorWriter: Writer = PrintWriter(System.err, true),
override var reader: Reader = InputStreamReader(System.`in`),
override var writer: Writer = PrintWriter(System.out, true)
) : ScriptContext {
private var globalScope: Bindings? = null
override fun setBindings(bindings: Bindings?, scope: Int) {
when (scope) {
100 -> {
@@ -24,6 +26,7 @@ open class SimpleScriptContext : ScriptContext {
engineScope = bindings
return
}
200 -> {
globalScope = bindings
return
@@ -46,6 +49,7 @@ open class SimpleScriptContext : ScriptContext {
100 -> {
return engineScope[name]
}
200 -> {
return globalScope?.get(name)
}
@@ -58,6 +62,7 @@ open class SimpleScriptContext : ScriptContext {
100 -> {
return getBindings(100)?.remove(name)
}
200 -> {
return getBindings(200)?.remove(name)
}
@@ -71,6 +76,7 @@ open class SimpleScriptContext : ScriptContext {
engineScope[name] = value
return
}
200 -> {
globalScope?.put(name, value)
return