From c140567e0c61602997580dd1b56d84804bd8428d Mon Sep 17 00:00:00 2001 From: sergeych Date: Tue, 31 Mar 2026 23:00:38 +0300 Subject: [PATCH] made last changes KMP compliant --- .../kotlin/net/sergeych/lyng/Compiler.kt | 30 +++++++++++-------- .../lyng/bytecode/BytecodeCompiler.kt | 20 +++++++++---- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt index 23153e1..895afe3 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt @@ -436,6 +436,12 @@ class Compiler( } } + private fun rememberModuleReferencePos(name: String, pos: Pos) { + if (!moduleReferencePosByName.containsKey(name)) { + moduleReferencePosByName[name] = pos + } + } + private fun predeclareClassMembers(target: MutableSet, overrides: MutableMap) { val saved = cc.savePos() var depth = 0 @@ -948,7 +954,7 @@ class Compiler( } } captureLocalRef(name, slotLoc, pos)?.let { ref -> - moduleReferencePosByName.putIfAbsent(name, pos) + rememberModuleReferencePos(name, pos) resolutionSink?.reference(name, pos) return ref } @@ -1042,7 +1048,7 @@ class Compiler( moduleEntry.isDelegated ) captureLocalRef(name, moduleLoc, pos)?.let { ref -> - moduleReferencePosByName.putIfAbsent(name, pos) + rememberModuleReferencePos(name, pos) resolutionSink?.reference(name, pos) return ref } @@ -1069,7 +1075,7 @@ class Compiler( strictSlotRefs ) } - moduleReferencePosByName.putIfAbsent(name, pos) + rememberModuleReferencePos(name, pos) resolutionSink?.reference(name, pos) return ref } @@ -1100,14 +1106,14 @@ class Compiler( ) } } - registerImportBinding(name, resolved.binding, pos) - val slot = lookupSlotLocation(name) - if (slot != null) { - captureLocalRef(name, slot, pos)?.let { ref -> - moduleReferencePosByName.putIfAbsent(name, pos) - resolutionSink?.reference(name, pos) - return ref - } + registerImportBinding(name, resolved.binding, pos) + val slot = lookupSlotLocation(name) + if (slot != null) { + captureLocalRef(name, slot, pos)?.let { ref -> + rememberModuleReferencePos(name, pos) + resolutionSink?.reference(name, pos) + return ref + } val ref = if (!useScopeSlots && capturePlanStack.isEmpty() && slot.depth > 0) { LocalSlotRef( name, @@ -1131,7 +1137,7 @@ class Compiler( strictSlotRefs ) } - moduleReferencePosByName.putIfAbsent(name, pos) + rememberModuleReferencePos(name, pos) resolutionSink?.reference(name, pos) return ref } 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 8d79aa9..82bc1e2 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt @@ -7664,7 +7664,9 @@ class BytecodeCompiler( private fun noteScopeSlotRef(slot: Int, pos: Pos) { if (slot >= scopeSlotCount) return val key = scopeKeyByIndex.getOrNull(slot) ?: return - scopeSlotRefPosByKey.putIfAbsent(key, pos) + if (!scopeSlotRefPosByKey.containsKey(key)) { + scopeSlotRefPosByKey[key] = pos + } } private fun resolveSlot(ref: LocalSlotRef): Int? { @@ -7675,11 +7677,15 @@ class BytecodeCompiler( val localIndex = localSlotIndexByKey[key] if (localIndex != null) return scopeSlotCount + localIndex scopeSlotMap[key]?.let { - scopeSlotRefPosByKey.putIfAbsent(key, ref.pos()) + if (!scopeSlotRefPosByKey.containsKey(key)) { + scopeSlotRefPosByKey[key] = ref.pos() + } return it } scopeSlotIndexByName[ref.name]?.let { - scopeSlotRefPosByKey.putIfAbsent(key, ref.pos()) + if (!scopeSlotRefPosByKey.containsKey(key)) { + scopeSlotRefPosByKey[key] = ref.pos() + } return it } } @@ -7693,7 +7699,9 @@ class BytecodeCompiler( } val resolved = scopeSlotMap[scopeKey] if (resolved != null) { - scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos()) + if (!scopeSlotRefPosByKey.containsKey(scopeKey)) { + scopeSlotRefPosByKey[scopeKey] = ref.pos() + } } return resolved } @@ -7708,7 +7716,9 @@ class BytecodeCompiler( val scopeKey = ScopeSlotKey(refScopeId(ref), refSlot(ref)) val resolved = scopeSlotMap[scopeKey] if (resolved != null) { - scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos()) + if (!scopeSlotRefPosByKey.containsKey(scopeKey)) { + scopeSlotRefPosByKey[scopeKey] = ref.pos() + } } return resolved }