Refactor scope handling and caching, improve dynamic access and tests, bump version to 1.5.1-SNAPSHOT

This commit is contained in:
Sergey Chernov 2026-02-19 16:17:35 +03:00
parent 66d9a40bb1
commit 6bf99d354b
7 changed files with 31 additions and 12 deletions

View File

@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.5.0-SNAPSHOT"
version = "1.5.1-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below

View File

@ -1181,9 +1181,15 @@ class Compiler(
for ((name, record) in current.objects) {
if (!record.visibility.isPublic) continue
if (nameObjClass.containsKey(name)) continue
when (val value = record.value) {
is ObjClass -> nameObjClass[name] = value
is ObjInstance -> nameObjClass[name] = value.objClass
val resolved = when (val raw = record.value) {
is FrameSlotRef -> raw.read()
is RecordSlotRef -> raw.read()
else -> raw
}
when (resolved) {
is ObjClass -> nameObjClass[name] = resolved
is ObjInstance -> nameObjClass[name] = resolved.objClass
is ObjDynamic -> nameObjClass[name] = resolved.objClass
}
}
current = current.parent

View File

@ -164,8 +164,8 @@ class Script(
companion object {
/**
* Create new scope using standard safe set of modules, using [defaultImportManager]. It is
* suspended as first time calls requires compilation of standard library or other
* Create new scope using a standard safe set of modules, using [defaultImportManager]. It is
* suspended as first time invocation requires compilation of standard library or other
* asynchronous initialization.
*/
suspend fun newScope(pos: Pos = Pos.builtIn) = defaultImportManager.newStdScope(pos)

View File

@ -256,6 +256,13 @@ open class Obj {
}
}
/**
* Sugar to use it with [ScopeFacade]
*/
suspend fun toString(facade: ScopeFacade) = toString(facade.requireScope())
open suspend fun defaultToString(scope: Scope): ObjString = ObjString(this.toString())
/**

View File

@ -22,6 +22,7 @@ import net.sergeych.lyng.miniast.TypeGenericDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lyng.requireScope
class ObjRange(
val start: Obj?,

View File

@ -1,5 +1,5 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -69,10 +69,10 @@ abstract class ImportProvider(
val plan: Map<String, Int>
)
private var cachedStdScope = CachedExpression<StdScopeSeed>()
private var cachedStdScopeSeed = CachedExpression<StdScopeSeed>()
suspend fun newStdScope(pos: Pos = Pos.builtIn): Scope {
val seed = cachedStdScope.get {
suspend fun newStdScope(pos: Pos = Pos.builtIn): ModuleScope {
val seed = cachedStdScopeSeed.get {
val stdlib = prepareImport(pos, "lyng.stdlib", null)
val plan = LinkedHashMap<String, Int>()
for ((name, record) in stdlib.objects) {

View File

@ -70,9 +70,10 @@ class OOTest {
@Test
fun testDynamicGet() = runTest {
eval(
val ms = Script.newScope()
ms.eval(
"""
val accessor: Delegate = dynamic {
val accessor: String = dynamic {
get { name ->
if( name == "foo" ) "bar" else null
}
@ -84,6 +85,10 @@ class OOTest {
""".trimIndent()
)
ms.eval("""
assertEquals("bar", accessor.foo)
assertEquals(null, accessor.bad)
""".trimIndent())
}
@Test