diff --git a/examples/tcpserver.lyng b/examples/tcpserver.lyng index 8c11c97..2e14213 100644 --- a/examples/tcpserver.lyng +++ b/examples/tcpserver.lyng @@ -2,70 +2,55 @@ import lyng.io.net val host = "127.0.0.1" val clientCount = 1000 -val server: TcpServer = Net.tcpListen(0, host, clientCount, true) as TcpServer -val port: Int = server.localAddress().port +val server = Net.tcpListen(0, host, clientCount, true) as TcpServer +val port = server.localAddress().port fun payloadFor(index: Int): String { "$index:${Random.nextInt()}:${Random.nextInt()}" } -fun handleClient(client: TcpSocket): String { +fun handleClient(client: TcpSocket) { try { val source = client.readLine() - if( source == null ) { - return "server-eof" + if( source != null ) { + client.writeUtf8("pong: $source\n") + client.flush() } - val reply = "pong: $source" - client.writeUtf8(reply + "\n") - client.flush() - reply } finally { client.close() } } -val serverJob: Deferred = launch { - var handlers: List = List() +launch { try { - for( i in 0..<1000 ) { - val client: TcpSocket = server.accept() as TcpSocket - handlers += launch { + for( i in 0.. = (0.. +val replies = (0.. val payload = payloadFor(index) launch { - val socket: TcpSocket = Net.tcpConnect(host, port) as TcpSocket + val socket = Net.tcpConnect(host, port) as TcpSocket try { socket.writeUtf8(payload + "\n") socket.flush() val reply = socket.readLine() - if( reply == null ) { - "client-eof:$payload" - } - else { - assertEquals("pong: $payload", reply) - reply - } + assertEquals("pong: $payload", reply) + reply } finally { socket.close() } } -} - -val replies = clientJobs.joinAll() -val serverReplies = serverJob.await() as List +}.joinAll() assertEquals(clientCount, replies.size) -assertEquals(clientCount, serverReplies.size) -assertEquals(replies.toSet, serverReplies.toSet) val summary = "OK: $clientCount concurrent tcp clients" println(summary) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt index d6d0d78..af93b44 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt @@ -7820,7 +7820,9 @@ class BytecodeCompiler( scopeSlotRefPosByKey[scopeKey] = ref.pos() } } - return resolved + if (resolved != null) return resolved + localSlotIndexByName[ref.name]?.let { return scopeSlotCount + it } + return null } private fun resolveLocalSlotByRefOrName(ref: LocalSlotRef): Int? { @@ -8672,6 +8674,11 @@ class BytecodeCompiler( collectLoopVarNamesRef(ref.targetRef) collectLoopVarNamesRef(ref.indexRef) } + is RangeRef -> { + ref.left?.let { collectLoopVarNamesRef(it) } + ref.right?.let { collectLoopVarNamesRef(it) } + ref.step?.let { collectLoopVarNamesRef(it) } + } else -> {} } } @@ -8798,6 +8805,11 @@ class BytecodeCompiler( collectScopeSlotsRef(ref.targetRef) collectScopeSlotsRef(ref.indexRef) } + is RangeRef -> { + ref.left?.let { collectScopeSlotsRef(it) } + ref.right?.let { collectScopeSlotsRef(it) } + ref.step?.let { collectScopeSlotsRef(it) } + } is ClassOperatorRef -> { collectScopeSlotsRef(ref.target) } diff --git a/lynglib/src/commonTest/kotlin/CoroutinesTest.kt b/lynglib/src/commonTest/kotlin/CoroutinesTest.kt index 5dd3de1..31b3f6c 100644 --- a/lynglib/src/commonTest/kotlin/CoroutinesTest.kt +++ b/lynglib/src/commonTest/kotlin/CoroutinesTest.kt @@ -199,6 +199,25 @@ class TestCoroutines { ) } + @Test + fun testLaunchCanUseCapturedRangeBoundInForLoop() = runTest { + eval( + """ + val count = 8 + val outer = launch { + val jobs = [] + for( i in 0..