diff options
Diffstat (limited to 'src')
-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 |
9 files changed, 129 insertions, 47 deletions
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 |