diff options
author | nea <romangraef@gmail.com> | 2021-08-20 15:33:32 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2021-08-20 15:33:58 +0200 |
commit | 5042498c8fe12751c1b6560ae1fde07e7cd78259 (patch) | |
tree | 688aa93978ffffd0b15c639768fdad6f17609bdd | |
parent | 65a6ffeb6761994d4d69011c87f23fe074f7a1f7 (diff) | |
download | webos-5042498c8fe12751c1b6560ae1fde07e7cd78259.tar.gz webos-5042498c8fe12751c1b6560ae1fde07e7cd78259.tar.bz2 webos-5042498c8fe12751c1b6560ae1fde07e7cd78259.zip |
-rw-r--r-- | build.gradle.kts | 31 | ||||
-rw-r--r-- | gradle.properties | 2 | ||||
-rw-r--r-- | src/main/kotlin/WebOS.kt (renamed from src/jsMain/kotlin/WebOS.kt) | 115 | ||||
-rw-r--r-- | src/main/kotlin/asyncio.kt | 61 | ||||
-rw-r--r-- | src/main/kotlin/io/Path.kt (renamed from src/jsMain/kotlin/io/Path.kt) | 0 | ||||
-rw-r--r-- | src/main/kotlin/io/files.kt (renamed from src/jsMain/kotlin/io/files.kt) | 0 | ||||
-rw-r--r-- | src/main/kotlin/util/sequence.kt (renamed from src/jsMain/kotlin/util/sequence.kt) | 0 | ||||
-rw-r--r-- | src/main/resources/index.html (renamed from src/jsMain/resources/index.html) | 0 | ||||
-rw-r--r-- | src/test/kotlin/ProjectConfig.kt (renamed from src/jsTest/kotlin/ProjectConfig.kt) | 0 | ||||
-rw-r--r-- | src/test/kotlin/io/FileServiceTest.kt (renamed from src/jsTest/kotlin/io/FileServiceTest.kt) | 0 | ||||
-rw-r--r-- | src/test/kotlin/io/PathTest.kt (renamed from src/jsTest/kotlin/io/PathTest.kt) | 0 |
11 files changed, 149 insertions, 60 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index 99c6139..c657335 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("multiplatform") version "1.5.21" + kotlin("js") version "1.5.21" id("io.kotest.multiplatform") version "5.0.0.3" id("com.bnorm.power.kotlin-power-assert") version "0.10.0" } @@ -14,24 +14,19 @@ val kotestVersion: String by project configure<com.bnorm.power.PowerAssertGradleExtension> { functions = listOf("kotlin.assert", "kotlin.test.assertTrue", "kotlin.test.assertFalse", "kotlin.test.assertEquals") } - kotlin { - targets { - js(IR) { - nodejs { } - browser { - webpackTask { - output.libraryTarget = "umd" - } - testTask { useMocha() } - } + js(IR) { + binaries.executable() + useCommonJs() + browser { + testTask { useMocha() } } } sourceSets { - val jsMain by getting { + val main by getting { } - val jsTest by getting { + val test by getting { dependencies { implementation("io.kotest:kotest-assertions-core:5.0.0.376-SNAPSHOT") implementation("io.kotest:kotest-framework-api:5.0.0.376-SNAPSHOT") @@ -40,3 +35,13 @@ kotlin { } } } + +tasks.withType<org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile>() { + kotlinOptions { + moduleKind = "commonjs" + sourceMap = true + sourceMapEmbedSources = "always" + freeCompilerArgs += "-Xopt-in=kotlin.contracts.ExperimentalContracts" + + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..6890d45 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +kotlin.js.webpack.major.version=4 + diff --git a/src/jsMain/kotlin/WebOS.kt b/src/main/kotlin/WebOS.kt index 4ad2f86..5bb788c 100644 --- a/src/jsMain/kotlin/WebOS.kt +++ b/src/main/kotlin/WebOS.kt @@ -13,6 +13,27 @@ import kotlin.properties.Delegates fun main() { console.log("Hello from Kotlin") + + val runner = IORunner.runOnIO { + println("ON START") + wait() + println("WAIT CALLED ONCE") + val x = getOneKey() + println("INPUT GET: $x") + println("EXITING NOW") + } + println("runOnIO called -> CALLING WHATEVER") + runner._CALLED_BY_JS_onWhatever() + println("CALLED WHATEVER -> CALLING INPUT") + runner._CALLED_BY_JS_onInput(window.prompt("INPUT") ?: "NO INPUT") + println("CALLED INPUT") + return + + + + + + val webos = WebOS() document.body?.addEventListener("load", { document.body?.querySelectorAll(".webosconsole")?.asList()?.forEach { @@ -84,10 +105,10 @@ class ShellRunner(console: Console) : Activity(console) { is Key.Printable -> { inputBuffer += key.char } - is Key.Enter -> { + is Key.Enter -> { inputBuffer += "\n" } - else -> Unit + else -> Unit } fun <T : ShellProgram> openShellProgram(program: (ShellRunner) -> T): ShellRunner = openShellProgram(program(this)) @@ -158,52 +179,52 @@ sealed class Key { companion object { fun from(string: String) = when (string) { - "Alt" -> Alt - "AltGraph" -> AltGraph - "CapsLock" -> CapsLock - "Control" -> Control - "Fn" -> Function - "FnLock" -> FunctionLock - "Hyper" -> Hyper - "Meta" -> Meta - "NumLock" -> NumLock - "Shift" -> Shift - "Super" -> Super - "Symbol" -> Symbol - "SymbolLock" -> SymbolLock - "Enter" -> Enter - "Tab" -> Tab - "Down" -> Arrow.Down - "Left" -> Arrow.Left - "Right" -> Arrow.Right - "Up" -> Arrow.Up - "End" -> End - "Home" -> Home - "PageUp" -> PageUp - "PageDown" -> PageDown - "Backspace" -> Backspace - "Clear" -> Clear - "Copy" -> Copy - "CrSel" -> CrSel - "Cut" -> Cut - "Delete" -> Delete - "EraseEof" -> EraseEof - "ExSel" -> ExSel - "Insert" -> Insert - "Paste" -> Paste - "Redo" -> Redo - "Undo" -> Undo - "Accept" -> Accept - "Again" -> Again - "Attn" -> Attn - "Cancel" -> Cancel + "Alt" -> Alt + "AltGraph" -> AltGraph + "CapsLock" -> CapsLock + "Control" -> Control + "Fn" -> Function + "FnLock" -> FunctionLock + "Hyper" -> Hyper + "Meta" -> Meta + "NumLock" -> NumLock + "Shift" -> Shift + "Super" -> Super + "Symbol" -> Symbol + "SymbolLock" -> SymbolLock + "Enter" -> Enter + "Tab" -> Tab + "Down" -> Arrow.Down + "Left" -> Arrow.Left + "Right" -> Arrow.Right + "Up" -> Arrow.Up + "End" -> End + "Home" -> Home + "PageUp" -> PageUp + "PageDown" -> PageDown + "Backspace" -> Backspace + "Clear" -> Clear + "Copy" -> Copy + "CrSel" -> CrSel + "Cut" -> Cut + "Delete" -> Delete + "EraseEof" -> EraseEof + "ExSel" -> ExSel + "Insert" -> Insert + "Paste" -> Paste + "Redo" -> Redo + "Undo" -> Undo + "Accept" -> Accept + "Again" -> Again + "Attn" -> Attn + "Cancel" -> Cancel "ContextMenu" -> ContextMenu - "Escape" -> Escape - "Execute" -> Execute - "Find" -> Find - "Finish" -> Finish - "Help" -> Help - else -> if (string.length == 1) + "Escape" -> Escape + "Execute" -> Execute + "Find" -> Find + "Finish" -> Finish + "Help" -> Help + else -> if (string.length == 1) Printable(string.first()) else if (string.first() == 'F') FunctionN(string.substring(1).toInt()) diff --git a/src/main/kotlin/asyncio.kt b/src/main/kotlin/asyncio.kt new file mode 100644 index 0000000..e14fea9 --- /dev/null +++ b/src/main/kotlin/asyncio.kt @@ -0,0 +1,61 @@ +import kotlin.coroutines.* + +class IORunner : Continuation<Unit> { + var nextStep: Continuation<Unit>? = null + val eventQueue = ArrayDeque<String>() + fun _CALLED_BY_JS_onInput(str: String) { + eventQueue.addLast(str) + awake() + } + + fun _CALLED_BY_JS_onWhatever() { + awake() + } + + private fun awake() { + val step = nextStep + if (step != null) { + nextStep = null + step.resume(Unit) + } + } + + suspend fun wait() { + return suspendCoroutine { c -> + console.log("SUSPENDING COROUTINE") + nextStep = c + } + } + + override val context: CoroutineContext + get() = EmptyCoroutineContext + + override fun resumeWith(result: Result<Unit>) { + if (result.isFailure) { + result.exceptionOrNull()?.printStackTrace() + } else { + println("IORunner exited successfully") + } + } + + companion object { + fun runOnIO(block: suspend IORunner.() -> Unit): IORunner { + val r = IORunner() + block.startCoroutine(r, r) + return r + } + } +} + + +suspend fun IORunner.getOneKey(): String { + while (true) { + val x = eventQueue.removeFirstOrNull() + if (x == null) + wait() + else { + return x + } + } +} + diff --git a/src/jsMain/kotlin/io/Path.kt b/src/main/kotlin/io/Path.kt index 241c559..241c559 100644 --- a/src/jsMain/kotlin/io/Path.kt +++ b/src/main/kotlin/io/Path.kt diff --git a/src/jsMain/kotlin/io/files.kt b/src/main/kotlin/io/files.kt index 4c53c4e..4c53c4e 100644 --- a/src/jsMain/kotlin/io/files.kt +++ b/src/main/kotlin/io/files.kt diff --git a/src/jsMain/kotlin/util/sequence.kt b/src/main/kotlin/util/sequence.kt index 72b07dc..72b07dc 100644 --- a/src/jsMain/kotlin/util/sequence.kt +++ b/src/main/kotlin/util/sequence.kt diff --git a/src/jsMain/resources/index.html b/src/main/resources/index.html index 562ee45..562ee45 100644 --- a/src/jsMain/resources/index.html +++ b/src/main/resources/index.html diff --git a/src/jsTest/kotlin/ProjectConfig.kt b/src/test/kotlin/ProjectConfig.kt index faa4d69..faa4d69 100644 --- a/src/jsTest/kotlin/ProjectConfig.kt +++ b/src/test/kotlin/ProjectConfig.kt diff --git a/src/jsTest/kotlin/io/FileServiceTest.kt b/src/test/kotlin/io/FileServiceTest.kt index 9c9c8b9..9c9c8b9 100644 --- a/src/jsTest/kotlin/io/FileServiceTest.kt +++ b/src/test/kotlin/io/FileServiceTest.kt diff --git a/src/jsTest/kotlin/io/PathTest.kt b/src/test/kotlin/io/PathTest.kt index 9ba8a9e..9ba8a9e 100644 --- a/src/jsTest/kotlin/io/PathTest.kt +++ b/src/test/kotlin/io/PathTest.kt |