Guest User

Untitled

a guest
Sep 22nd, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.01 KB | None | 0 0
  1. class ScriptHost {
  2.   private val jvmHost = BasicJvmScriptingHost()
  3.   private val compilationConfiguration = createCompilationConfigurationFromTemplate(
  4.     KotlinType(MyKotlinScript::class),
  5.     defaultJvmScriptingHostConfiguration,
  6.     CompilationConfiguration::class
  7.   ) {
  8.     jvm {
  9.       dependenciesFromCurrentContext(
  10.         // The classes in this JAR will not need to change during the server lifetime.
  11.         "a-static-jar",
  12.         // This jar contains a generated class whose content will change during the server lifetime.
  13.         "a-dynamic-jar"
  14.       )
  15.     }
  16.   }
  17.  
  18.   fun evaluate(code: String): CodeEvaluationResult {
  19.     val scriptSource = code.toScriptSource(name = Constants.SCRIPT_FILE_NAME)
  20.     val evaluationResult = jvmHost.eval(scriptSource, compilationConfiguration, null)
  21.     val scriptInstance = (evaluationResult as? ResultWithDiagnostics.Success)
  22.       ?.value
  23.       ?.returnValue
  24.       ?.scriptInstance
  25.     return CodeEvaluationResult(scriptInstance, evaluationResult.reports)
  26.   }
  27. }
Add Comment
Please, Sign In to add comment