diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt index 0fbcc80..75d9f85 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt @@ -346,6 +346,23 @@ class Script( addFn("run") { requireOnlyArg().execute(this) } + addFn("cached") { + val builder = requireOnlyArg() + val capturedScope = this + var calculated = false + var cachedValue: Obj = ObjVoid + val thunk = object : Statement() { + override val pos: Pos = Pos.builtIn + override suspend fun execute(scope: Scope): Obj { + if (!calculated) { + cachedValue = builder.execute(capturedScope) + calculated = true + } + return cachedValue + } + } + thunk + } addVoidFn("delay") { val a = args.firstAndOnly() diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index 68f465b..6873cf3 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -3564,7 +3564,6 @@ class ScriptTest { ) } - @Ignore("incremental enable") @Test fun cachedTest() = runTest { eval( @@ -3608,14 +3607,12 @@ class ScriptTest { ) } - @Ignore("incremental enable: run helper not resolved in new compiler") @Test fun testElvisAndThrow2() = runTest { eval( """ val t = "112" val x = t ?: run { throw "testx" } - } assertEquals( "112", x) """.trimIndent() ) @@ -4383,28 +4380,23 @@ class ScriptTest { ) } - @Ignore("incremental enable: unresolved names are now compile-time errors") @Test fun testHangOnNonexistingMethod() = runTest { - eval( - """ - class T(someList) { - fun f() { - nonExistingMethod() + assertFailsWith { + eval( + """ + class T(someList) { + fun f() { + nonExistingMethod() + } } - } - val t = T([1,2]) - try { - for( i in 1..10 ) { + val t = T([1,2]) + for( i in 1..10 ) { t.f() } - } - catch(t: SymbolNotFound) { - println(t::class) - // ok - } - """ - ) + """.trimIndent() + ) + } } @Test @@ -4537,7 +4529,6 @@ class ScriptTest { ) } - @Ignore("incremental enable: cached helper not resolved in new compiler") @Test fun testCached() = runTest { eval( @@ -4935,7 +4926,7 @@ class ScriptTest { ) } - @Ignore("incremental enable: run helper not resolved in new compiler") + @Ignore("incremental enable: capture of static var inside run block not resolved") @Test fun realWorldCaptureProblem() = runTest { eval(