[K/JS, K/Wasm] Optimize simple objects declaration and usage ^Fixed KT-58797
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.inline.clean
|
||||
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsClass
|
||||
|
||||
class ClassPostProcessor(val root: JsClass) {
|
||||
val optimizations = listOf(
|
||||
{ EmptyConstructorRemoval(root).apply() }
|
||||
)
|
||||
|
||||
fun apply() {
|
||||
do {
|
||||
var hasChanges = false
|
||||
for (opt in optimizations) {
|
||||
hasChanges = hasChanges or opt()
|
||||
}
|
||||
} while (hasChanges)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.inline.clean
|
||||
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsClass
|
||||
|
||||
class EmptyConstructorRemoval(private val klass: JsClass) {
|
||||
fun apply(): Boolean {
|
||||
if (klass.constructor?.body?.statements?.isEmpty() != true) return false
|
||||
klass.constructor = null
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -328,6 +328,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
|
||||
runTest("js/js.translator/testData/box/classObject/objectInCompanionObject.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("objectLazyInitialized.kt")
|
||||
public void testObjectLazyInitialized() throws Exception {
|
||||
runTest("js/js.translator/testData/box/classObject/objectLazyInitialized.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("setVar.kt")
|
||||
public void testSetVar() throws Exception {
|
||||
|
||||
+6
@@ -328,6 +328,12 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest {
|
||||
runTest("js/js.translator/testData/box/classObject/objectInCompanionObject.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("objectLazyInitialized.kt")
|
||||
public void testObjectLazyInitialized() throws Exception {
|
||||
runTest("js/js.translator/testData/box/classObject/objectLazyInitialized.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("setVar.kt")
|
||||
public void testSetVar() throws Exception {
|
||||
|
||||
+6
@@ -328,6 +328,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
|
||||
runTest("js/js.translator/testData/box/classObject/objectInCompanionObject.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("objectLazyInitialized.kt")
|
||||
public void testObjectLazyInitialized() throws Exception {
|
||||
runTest("js/js.translator/testData/box/classObject/objectLazyInitialized.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("setVar.kt")
|
||||
public void testSetVar() throws Exception {
|
||||
|
||||
+6
@@ -328,6 +328,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
|
||||
runTest("js/js.translator/testData/box/classObject/objectInCompanionObject.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("objectLazyInitialized.kt")
|
||||
public void testObjectLazyInitialized() throws Exception {
|
||||
runTest("js/js.translator/testData/box/classObject/objectLazyInitialized.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("setVar.kt")
|
||||
public void testSetVar() throws Exception {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// EXPECTED_REACHABLE_NODES: 1291
|
||||
// See KT-6201
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
var log = ""
|
||||
|
||||
open class Mixin {
|
||||
init {
|
||||
log = "mixin"
|
||||
}
|
||||
}
|
||||
|
||||
fun initCall(): Number {
|
||||
log = "initCall"
|
||||
return 2
|
||||
}
|
||||
|
||||
object O1 {
|
||||
init {
|
||||
log = "O1 init"
|
||||
}
|
||||
}
|
||||
|
||||
object O2 {
|
||||
init {
|
||||
initCall()
|
||||
}
|
||||
}
|
||||
|
||||
object O3 : Mixin()
|
||||
|
||||
object O4 {
|
||||
val someValue = initCall()
|
||||
}
|
||||
|
||||
object O5 {
|
||||
val someValue = O3.also { log = "O5 also" }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (log != "") return "Fail: something was initialized before any object was used"
|
||||
O1
|
||||
if (log != "O1 init") return "Fail: O1 didn't initialized lazy"
|
||||
O2
|
||||
if (log != "initCall") return "Fail: O2 didn't initialized lazy"
|
||||
O3
|
||||
if (log != "mixin") return "Fail: O3 didn't initialized lazy"
|
||||
O4
|
||||
if (log != "initCall") return "Fail: O4 didn't initialized lazy"
|
||||
O5
|
||||
if (log != "O5 also") return "Fail: O5 didn't initialized lazy"
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user