AbstractScriptEngine.kt 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Decompiled with CFR 0.152.
  3. */
  4. package com.script
  5. import org.mozilla.javascript.Scriptable
  6. import java.io.Reader
  7. import java.io.StringReader
  8. abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngine {
  9. override var context: ScriptContext = SimpleScriptContext()
  10. init {
  11. bindings?.let {
  12. context.setBindings(bindings, 100)
  13. }
  14. }
  15. override fun getBindings(scope: Int): Bindings? {
  16. if (scope == 200) {
  17. return context.getBindings(200)
  18. }
  19. if (scope == 100) {
  20. return context.getBindings(100)
  21. }
  22. throw IllegalArgumentException("Invalid scope value.")
  23. }
  24. override fun setBindings(bindings: Bindings?, scope: Int) {
  25. when (scope) {
  26. 200 -> {
  27. context.setBindings(bindings, 200)
  28. }
  29. 100 -> {
  30. context.setBindings(bindings, 100)
  31. }
  32. else -> {
  33. throw IllegalArgumentException("Invalid scope value.")
  34. }
  35. }
  36. }
  37. override fun put(key: String, value: Any?) {
  38. getBindings(100)?.put(key, value)
  39. }
  40. override fun get(key: String): Any? {
  41. return getBindings(100)?.get(key)
  42. }
  43. override fun eval(script: String, scope: Scriptable): Any? {
  44. return this.eval(StringReader(script), scope)
  45. }
  46. @Throws(ScriptException::class)
  47. override fun eval(reader: Reader, context: ScriptContext): Any? {
  48. return this.eval(reader, getRuntimeScope(context))
  49. }
  50. @Throws(ScriptException::class)
  51. override fun eval(reader: Reader, bindings: Bindings): Any? {
  52. return this.eval(reader, getScriptContext(bindings))
  53. }
  54. @Throws(ScriptException::class)
  55. override fun eval(script: String, bindings: Bindings): Any? {
  56. return this.eval(script, getScriptContext(bindings))
  57. }
  58. @Throws(ScriptException::class)
  59. override fun eval(reader: Reader): Any? {
  60. return this.eval(reader, context)
  61. }
  62. @Throws(ScriptException::class)
  63. override fun eval(script: String): Any? {
  64. return this.eval(script, context)
  65. }
  66. @Throws(ScriptException::class)
  67. override fun eval(script: String, context: ScriptContext): Any? {
  68. return this.eval(StringReader(script), context)
  69. }
  70. override fun getScriptContext(bindings: Bindings): ScriptContext {
  71. val ctx = SimpleScriptContext()
  72. val gs = getBindings(200)
  73. if (gs != null) {
  74. ctx.setBindings(gs, 200)
  75. }
  76. ctx.setBindings(bindings, 100)
  77. ctx.reader = context.reader
  78. ctx.writer = context.writer
  79. ctx.errorWriter = context.errorWriter
  80. return ctx
  81. }
  82. }