[K/JS] Support eager initialization for per-file granularity
This commit is contained in:
@@ -1404,7 +1404,10 @@ public class JsToStringGenerationVisitor extends JsVisitor {
|
||||
p.print("}");
|
||||
}
|
||||
|
||||
p.print(" from ");
|
||||
if (!(target == JsImport.Target.Effect.INSTANCE)) {
|
||||
p.print(" from ");
|
||||
}
|
||||
|
||||
p.print(javaScriptString(jsImport.getModule()));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class JsImport(
|
||||
get() = (target as Target.Elements).elements
|
||||
|
||||
sealed class Target {
|
||||
object Effect : Target()
|
||||
class Elements(val elements: MutableList<Element>) : Target()
|
||||
class Default(val name: JsNameRef) : Target() {
|
||||
constructor(name: String) : this(JsNameRef(name))
|
||||
@@ -36,6 +37,7 @@ class JsImport(
|
||||
|
||||
override fun acceptChildren(visitor: JsVisitor) {
|
||||
when (target) {
|
||||
is Target.Effect -> {}
|
||||
is Target.All -> visitor.accept(target.alias)
|
||||
is Target.Default -> visitor.accept(target.name)
|
||||
is Target.Elements -> target.elements.forEach {
|
||||
|
||||
+18
@@ -2041,6 +2041,24 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/constructor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal1.kt")
|
||||
public void testEagerInitializationGlobal1() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal1.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal2.kt")
|
||||
public void testEagerInitializationGlobal2() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal3.kt")
|
||||
public void testEagerInitializationGlobal3() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal3.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inheritance.kt")
|
||||
public void testInheritance() throws Exception {
|
||||
|
||||
+18
@@ -2147,6 +2147,24 @@ public class FirJsES6BoxTestGenerated extends AbstractFirJsES6BoxTest {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/constructor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal1.kt")
|
||||
public void testEagerInitializationGlobal1() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal1.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal2.kt")
|
||||
public void testEagerInitializationGlobal2() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal3.kt")
|
||||
public void testEagerInitializationGlobal3() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal3.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inheritance.kt")
|
||||
public void testInheritance() throws Exception {
|
||||
|
||||
+18
@@ -2147,6 +2147,24 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/constructor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal1.kt")
|
||||
public void testEagerInitializationGlobal1() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal1.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal2.kt")
|
||||
public void testEagerInitializationGlobal2() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal3.kt")
|
||||
public void testEagerInitializationGlobal3() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal3.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inheritance.kt")
|
||||
public void testInheritance() throws Exception {
|
||||
|
||||
+18
@@ -2041,6 +2041,24 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/constructor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal1.kt")
|
||||
public void testEagerInitializationGlobal1() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal1.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal2.kt")
|
||||
public void testEagerInitializationGlobal2() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("eagerInitializationGlobal3.kt")
|
||||
public void testEagerInitializationGlobal3() throws Exception {
|
||||
runTest("js/js.translator/testData/box/esModules/crossModuleRefPerFile/eagerInitializationGlobal3.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inheritance.kt")
|
||||
public void testInheritance() throws Exception {
|
||||
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
// TARGET_BACKEND: JS_IR
|
||||
// IGNORE_BACKEND: WASM
|
||||
// ES_MODULES
|
||||
// PROPERTY_LAZY_INITIALIZATION
|
||||
|
||||
// FILE: lib.kt
|
||||
var z1 = false
|
||||
var z2 = false
|
||||
|
||||
// FILE: lib2.kt
|
||||
|
||||
@OptIn(kotlin.ExperimentalStdlibApi::class)
|
||||
@EagerInitialization
|
||||
val x = foo()
|
||||
|
||||
private fun foo(): Int {
|
||||
z1 = true
|
||||
return 42
|
||||
}
|
||||
|
||||
// Will be initialized since [x]'s initializer calls a function from the file.
|
||||
val y = run { z2 = true; 117 }
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
fun box(): String {
|
||||
return return if (z1 && z2) "OK" else "fail"
|
||||
}
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// TARGET_BACKEND: JS_IR
|
||||
// IGNORE_BACKEND: WASM
|
||||
// ES_MODULES
|
||||
// PROPERTY_LAZY_INITIALIZATION
|
||||
|
||||
// FILE: lib.kt
|
||||
var z1 = false
|
||||
var z2 = false
|
||||
|
||||
// FILE: lib2.kt
|
||||
|
||||
@OptIn(kotlin.ExperimentalStdlibApi::class)
|
||||
@EagerInitialization
|
||||
val x = run { z1 = true; 42 }
|
||||
|
||||
// Won't be initialized (cause no function from the file will be called during [x] initialization).
|
||||
val y = run { z2 = true; 117 }
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
fun box(): String {
|
||||
return if (z1 && !z2) "OK" else "fail"
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// TARGET_BACKEND: JS_IR
|
||||
// IGNORE_BACKEND: WASM
|
||||
// ES_MODULES
|
||||
// PROPERTY_LAZY_INITIALIZATION
|
||||
|
||||
// FILE: lib.kt
|
||||
var z1 = false
|
||||
|
||||
// FILE: lib2.kt
|
||||
|
||||
@OptIn(kotlin.ExperimentalStdlibApi::class)
|
||||
@EagerInitialization
|
||||
val x = run { z1 = !z1; 42 }
|
||||
|
||||
val y = run { 73 }
|
||||
|
||||
fun box(): String {
|
||||
return if (z1) "OK" else "fail"
|
||||
}
|
||||
+11
-3
@@ -1,10 +1,18 @@
|
||||
IGNORE_PER_FILE: true
|
||||
|
||||
MODULES: lib1, main
|
||||
|
||||
STEP 0:
|
||||
libs: lib1, main
|
||||
dirty js modules: lib1, main
|
||||
STEP 1..3:
|
||||
dirty js files: lib1/l1, lib1/l2, main/m, main/m.export, main
|
||||
STEP 1:
|
||||
libs: lib1, main
|
||||
dirty js modules: lib1
|
||||
dirty js files: lib1/l2, lib1, main
|
||||
STEP 2:
|
||||
libs: lib1, main
|
||||
dirty js modules: lib1
|
||||
dirty js files: lib1/l2
|
||||
STEP 3:
|
||||
libs: lib1, main
|
||||
dirty js modules: lib1
|
||||
dirty js files: lib1/l2, main
|
||||
|
||||
Reference in New Issue
Block a user