1+ /*
2+ * #%L
3+ * JSR-223-compliant Kotlin scripting language plugin.
4+ * %%
5+ * Copyright (C) 2016 - 2019 Board of Regents of the University of
6+ * Wisconsin-Madison.
7+ * %%
8+ * Redistribution and use in source and binary forms, with or without
9+ * modification, are permitted provided that the following conditions are met:
10+ *
11+ * 1. Redistributions of source code must retain the above copyright notice,
12+ * this list of conditions and the following disclaimer.
13+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14+ * this list of conditions and the following disclaimer in the documentation
15+ * and/or other materials provided with the distribution.
16+ *
17+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+ * POSSIBILITY OF SUCH DAMAGE.
28+ * #L%
29+ */
30+ package org.scijava.plugins.scripting.kotlin
31+
32+ import org.scijava.plugins.scripting.kotlin.KotlinScriptLanguage.MyScriptEngineFactory
33+ import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase
34+ import org.scijava.plugin.Plugin
35+ import org.scijava.plugins.scripting.kotlin.KotlinScriptLanguage.SynchronizedScriptEngine
36+ import org.scijava.script.AdaptedScriptLanguage
37+ import org.scijava.script.ScriptLanguage
38+ import java.io.Reader
39+ import java.util.*
40+ import javax.script.*
41+
42+ /* *
43+ * A SciJava [ScriptLanguage] for Kotlin.
44+ *
45+ * @author Curtis Rueden
46+ * @see ScriptEngine
47+ */
48+ @Plugin(type = ScriptLanguage ::class , name = " Kotlin" )
49+ class KotlinScriptLanguage : AdaptedScriptLanguage (MyScriptEngineFactory ()) {
50+ // NB: The wrapped ScriptEngineFactory does not include Kotlin in its list.
51+ override fun getNames () = listOf (" kotlin" , " Kotlin" )
52+
53+ // NB: The wrapped ScriptEngineFactory does not include .kt in its list.
54+ override fun getExtensions () = listOf (" kt" , " kts" )
55+
56+ class MyScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase () {
57+ override fun getScriptEngine (): ScriptEngine {
58+ return SynchronizedScriptEngine (ScriptEngineManager ().getEngineByExtension(" kts" ))
59+ }
60+ }
61+
62+ class SynchronizedScriptEngine (private val delegate : ScriptEngine ) : ScriptEngine {
63+ @Synchronized
64+ @Throws(ScriptException ::class )
65+ override fun eval (s : String , scriptContext : ScriptContext ): Any? = delegate.eval(s, scriptContext)
66+
67+ @Synchronized
68+ @Throws(ScriptException ::class )
69+ override fun eval (reader : Reader , scriptContext : ScriptContext ): Any? = delegate.eval(reader, scriptContext)
70+
71+ @Synchronized
72+ @Throws(ScriptException ::class )
73+ override fun eval (s : String ): Any? = delegate.eval(s)
74+
75+ @Synchronized
76+ @Throws(ScriptException ::class )
77+ override fun eval (reader : Reader ): Any? = delegate.eval(reader)
78+
79+ @Synchronized
80+ @Throws(ScriptException ::class )
81+ override fun eval (s : String , bindings : Bindings ): Any? = delegate.eval(s, bindings)
82+
83+ @Synchronized
84+ @Throws(ScriptException ::class )
85+ override fun eval (reader : Reader , bindings : Bindings ): Any? = delegate.eval(reader, bindings)
86+
87+ @Synchronized
88+ override fun put (s : String , o : Any ) = delegate.put(s, o)
89+
90+ @Synchronized
91+ override fun get (s : String ): Any? = delegate[s]
92+
93+ @Synchronized
94+ override fun getBindings (i : Int ): Bindings ? = delegate.getBindings(i)
95+
96+ @Synchronized
97+ override fun setBindings (bindings : Bindings , i : Int ) = delegate.setBindings(bindings, i)
98+
99+ @Synchronized
100+ override fun createBindings (): Bindings = delegate.createBindings()
101+
102+ @Synchronized
103+ override fun getContext (): ScriptContext = delegate.context
104+
105+ @Synchronized
106+ override fun setContext (scriptContext : ScriptContext ) {
107+ delegate.context = scriptContext
108+ }
109+
110+ @Synchronized
111+ override fun getFactory (): ScriptEngineFactory = delegate.factory
112+ }
113+ }
0 commit comments