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 import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" 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 // 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) { for ((name, record) in current.objects) {
if (!record.visibility.isPublic) continue if (!record.visibility.isPublic) continue
if (nameObjClass.containsKey(name)) continue if (nameObjClass.containsKey(name)) continue
when (val value = record.value) { val resolved = when (val raw = record.value) {
is ObjClass -> nameObjClass[name] = value is FrameSlotRef -> raw.read()
is ObjInstance -> nameObjClass[name] = value.objClass 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 current = current.parent

View File

@ -164,8 +164,8 @@ class Script(
companion object { companion object {
/** /**
* Create new scope using standard safe set of modules, using [defaultImportManager]. It is * Create new scope using a standard safe set of modules, using [defaultImportManager]. It is
* suspended as first time calls requires compilation of standard library or other * suspended as first time invocation requires compilation of standard library or other
* asynchronous initialization. * asynchronous initialization.
*/ */
suspend fun newScope(pos: Pos = Pos.builtIn) = defaultImportManager.newStdScope(pos) 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()) 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.addFnDoc
import net.sergeych.lyng.miniast.addPropertyDoc import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type import net.sergeych.lyng.miniast.type
import net.sergeych.lyng.requireScope
class ObjRange( class ObjRange(
val start: Obj?, 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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> val plan: Map<String, Int>
) )
private var cachedStdScope = CachedExpression<StdScopeSeed>() private var cachedStdScopeSeed = CachedExpression<StdScopeSeed>()
suspend fun newStdScope(pos: Pos = Pos.builtIn): Scope { suspend fun newStdScope(pos: Pos = Pos.builtIn): ModuleScope {
val seed = cachedStdScope.get { val seed = cachedStdScopeSeed.get {
val stdlib = prepareImport(pos, "lyng.stdlib", null) val stdlib = prepareImport(pos, "lyng.stdlib", null)
val plan = LinkedHashMap<String, Int>() val plan = LinkedHashMap<String, Int>()
for ((name, record) in stdlib.objects) { for ((name, record) in stdlib.objects) {

View File

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