Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74d213e

Browse files
committed
Add JSR-223 examples
1 parent 345fd4f commit 74d213e

File tree

11 files changed

+281
-1
lines changed

11 files changed

+281
-1
lines changed

‎ReadMe.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ when needed
1818
### Script definitions with scripting hosts
1919

2020
- [Simple script definition](jvm/basic/jvm-simple-script/SimpleScript.md)
21-
- [Script with Dynamic dependencies from Maven](jvm/basic/jvm-maven-deps/MavenDeps.md)
21+
- [Script with dynamic dependencies from Maven](jvm/basic/jvm-maven-deps/MavenDeps.md)
2222
- [Scripting Host with Kotlin Compiler Embeddable](jvm/basic/jvm-embeddable-host/EmbeddableCompiler.md)
2323
- [Simplified main-kts-like script implementation](jvm/simple-main-kts/SimpleMainKts.md)
2424
- [`main-kts` scripts examples](jvm/main-kts/MainKts.md)
25+
- [using scripting via JSR 223 interface](jvm/jsr223/jsr223.md)
2526

2627
## External examples
2728

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
plugins {
3+
kotlin("jvm")
4+
}
5+
6+
val kotlinVersion: String by rootProject.extra
7+
8+
dependencies {
9+
runtimeOnly("org.jetbrains.kotlin:kotlin-main-kts:$kotlinVersion")
10+
runtimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:$kotlinVersion")
11+
testRuntimeOnly("org.jetbrains.kotlin:kotlin-main-kts:$kotlinVersion")
12+
testRuntimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:$kotlinVersion")
13+
testImplementation("junit:junit:4.12")
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.jsr223.mainKts
7+
8+
import javax.script.ScriptEngineManager
9+
10+
fun main(vararg args: String) {
11+
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
12+
13+
print("> ")
14+
System.`in`.reader().forEachLine {
15+
val res = engine.eval(it)
16+
println(res)
17+
print("> ")
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.jsr223.mainKts.test
7+
8+
import org.junit.Assert.assertEquals
9+
import org.junit.Assert.assertNull
10+
import org.junit.Test
11+
import java.io.ByteArrayOutputStream
12+
import java.io.File
13+
import java.io.PrintStream
14+
import javax.script.ScriptEngineManager
15+
16+
// Adapted from kotlin-main-kts module tests in the main Kotlin repo
17+
18+
class MainKtsJsr223Test {
19+
20+
@Test
21+
fun testBasicEval() {
22+
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
23+
val res1 = engine.eval("val x = 3")
24+
assertNull(res1)
25+
val res2 = engine.eval("x + 2")
26+
assertEquals(5, res2)
27+
}
28+
29+
@Test
30+
fun testWithImport() {
31+
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
32+
val out = captureOut {
33+
val res1 = engine.eval("""
34+
@file:Import("testData/import-common.main.kts")
35+
@file:Import("testData/import-middle.main.kts")
36+
sharedVar = sharedVar + 1
37+
println(sharedVar)
38+
""".trimIndent())
39+
assertNull(res1)
40+
}.lines()
41+
assertEquals(listOf("Hi from common", "Hi from middle", "5"), out)
42+
}
43+
44+
@Test
45+
fun testKotlinxHtmlExample() {
46+
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
47+
val scriptFile = File("../../main-kts/scripts/kotlinx-html.main.kts")
48+
val out = captureOut {
49+
engine.eval(scriptFile.reader())
50+
}.lines()
51+
assertEquals(listOf("<html>", " <body>", " <h1>Hello, World!</h1>", " </body>", "</html>"), out)
52+
}
53+
}
54+
55+
private fun captureOut(body: () -> Unit): String {
56+
val outStream = ByteArrayOutputStream()
57+
val prevOut = System.out
58+
System.setOut(PrintStream(outStream))
59+
try {
60+
body()
61+
} finally {
62+
System.out.flush()
63+
System.setOut(prevOut)
64+
}
65+
return outStream.toString().trim()
66+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var sharedVar = 2
3+
4+
println("Hi from common")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
@file:Import("import-common.main.kts")
3+
4+
sharedVar *= 2
5+
6+
println("Hi from middle")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
plugins {
3+
kotlin("jvm")
4+
}
5+
6+
val kotlinVersion: String by rootProject.extra
7+
8+
dependencies {
9+
runtimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:$kotlinVersion")
10+
testRuntimeOnly("org.jetbrains.kotlin:kotlin-scripting-jsr223:$kotlinVersion")
11+
testImplementation("junit:junit:4.12")
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.jsr223.simple
7+
8+
import javax.script.ScriptEngineManager
9+
10+
fun main(vararg args: String) {
11+
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
12+
13+
print("> ")
14+
System.`in`.reader().forEachLine {
15+
val res = engine.eval(it)
16+
println(res)
17+
print("> ")
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.jsr223.simple.test
7+
8+
import org.junit.Assert.*
9+
import org.junit.Test
10+
import javax.script.Compilable
11+
import javax.script.Invocable
12+
import javax.script.ScriptEngineManager
13+
import javax.script.ScriptException
14+
15+
// Adapted from kotlin-scripting-jsr223 module tests in the main Kotlin repo
16+
17+
class SimpleJsr223Test {
18+
19+
@Test
20+
fun testBasicEval() {
21+
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
22+
val res1 = engine.eval("val x = 3")
23+
assertNull(res1)
24+
val res2 = engine.eval("x + 2")
25+
assertEquals(5, res2)
26+
}
27+
28+
@Test
29+
fun testEvalWithBindings() {
30+
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
31+
32+
engine.put("z", 33)
33+
34+
engine.eval("val x = 10 + z")
35+
36+
val result = engine.eval("x + 20")
37+
assertEquals(63, result)
38+
39+
val result2 = engine.eval("11 + boundValue", engine.createBindings().apply {
40+
put("boundValue", 100)
41+
})
42+
assertEquals(111, result2)
43+
}
44+
45+
@Test
46+
fun testEvalWithErrorsAndExceptions() {
47+
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
48+
49+
try {
50+
engine.eval("java.lang.fish")
51+
fail("Script error expected")
52+
} catch (e: ScriptException) {}
53+
54+
val res1 = engine.eval("val x = 3")
55+
assertNull(res1)
56+
57+
try {
58+
engine.eval("throw Exception(\"!!\")")
59+
fail("Expecting exception to propagate")
60+
} catch (e: ScriptException) {
61+
assertEquals("!!", e.cause?.message)
62+
}
63+
64+
try {
65+
engine.eval("y")
66+
fail("Script error expected")
67+
} catch (e: ScriptException) {}
68+
69+
val res3 = engine.eval("x + 2")
70+
assertEquals(5, res3)
71+
}
72+
73+
@Test
74+
fun testInvocable() {
75+
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
76+
val res1 = engine.eval("""
77+
fun fn(x: Int) = x + 2
78+
val obj = object {
79+
fun fn1(x: Int) = x + 3
80+
}
81+
obj""".trimIndent())
82+
assertNotNull(res1)
83+
84+
val invocator = engine as? Invocable
85+
assertNotNull(invocator)
86+
87+
try {
88+
invocator!!.invokeFunction("fn1", 3)
89+
fail("NoSuchMethodException expected")
90+
} catch (e: NoSuchMethodException) {}
91+
92+
val res2 = invocator!!.invokeFunction("fn", 3)
93+
assertEquals(5, res2)
94+
95+
val res3 = invocator.invokeMethod(res1, "fn1", 3)
96+
assertEquals(6, res3)
97+
}
98+
99+
@Test
100+
fun testCompilable() {
101+
val engine = ScriptEngineManager().getEngineByExtension("kts") as Compilable
102+
val comp1 = engine.compile("val x = 3")
103+
val comp2 = engine.compile("x + 2")
104+
val res1 = comp1.eval()
105+
assertNull(res1)
106+
val res2 = comp2.eval()
107+
assertEquals(5, res2)
108+
}
109+
110+
@Test
111+
fun testResolveSymbolsFromContext() {
112+
val scriptEngine = ScriptEngineManager().getEngineByExtension("kts")!!
113+
val result = scriptEngine.eval("${this::class.java.name}.shouldBeVisibleFromRepl * 6")
114+
assertEquals(42, result)
115+
}
116+
117+
companion object {
118+
@Suppress("unused") // accessed from the tests below
119+
@JvmStatic
120+
val shouldBeVisibleFromRepl = 7
121+
}
122+
}

‎jvm/jsr223/jsr223.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# Kotlin Scripting Examples: using Kotlin scripting via JSR 223 API
3+
4+
[JSR 223](https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/) is a common JVM scripting API specification.
5+
The implementations of this API are available for Kotlin too. These examples demonstrate usage of the default
6+
implementation (`kotlin-scripting-jsr223`) and the one based on the `kotlin-main-kts`.
7+
8+
The examples(
9+
[simple](jsr223-simple/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/simple/repl.kt),
10+
[main-kts](jsr223-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/mainKts/repl.kt))
11+
are very bare-bone REPL implementations, while tests(
12+
([simple](jsr223-simple/src/test/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/simple/test/simpleJsr223Test.kt),
13+
[main-kts](jsr223-main-kts/src/test/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/mainKts/test/mainKtsJsr223Test.kt))
14+
demonstrate various features of the implementations.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /