From 645e4af8b19ad5cb1c569fb5ee3032f0f5c94298 Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Wed, 8 Nov 2023 20:09:22 +0200 Subject: [PATCH] [IDE] Move KotlinJavaScriptMetaFileDecompiler to kotlin.git KTIJ-27752 --- analysis/build.gradle.kts | 1 + .../decompiled/decompiler-js/build.gradle.kts | 27 +++++++++++++ .../js/KotlinJavaScriptMetaFileDecompiler.kt | 40 +++++++++++++++++++ .../js/KotlinJavaScriptMetaFileType.kt | 23 +++++++++++ .../psi/KotlinLabelProviderService.kt | 1 + build.gradle.kts | 1 + settings.gradle | 1 + 7 files changed, 94 insertions(+) create mode 100644 analysis/decompiled/decompiler-js/build.gradle.kts create mode 100644 analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileDecompiler.kt create mode 100644 analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileType.kt diff --git a/analysis/build.gradle.kts b/analysis/build.gradle.kts index 95e342fb4c6..c5ce0db935c 100644 --- a/analysis/build.gradle.kts +++ b/analysis/build.gradle.kts @@ -6,6 +6,7 @@ tasks.register("analysisAllTests") { dependsOn( ":analysis:decompiled:decompiler-to-file-stubs:test", ":analysis:decompiled:decompiler-to-psi:test", + ":analysis:decompiled:decompiler-js:test", ":analysis:decompiled:decompiler-native:test", ":analysis:analysis-api:test", ":analysis:analysis-api-fir:test", diff --git a/analysis/decompiled/decompiler-js/build.gradle.kts b/analysis/decompiled/decompiler-js/build.gradle.kts new file mode 100644 index 00000000000..7a2e5a03c60 --- /dev/null +++ b/analysis/decompiled/decompiler-js/build.gradle.kts @@ -0,0 +1,27 @@ +plugins { + kotlin("jvm") + id("jps-compatible") +} + +sourceSets { + "main" { projectDefault() } + "test" { projectDefault() } +} + +projectTest { + workingDir = rootDir +} + +dependencies { + api(project(":core:deserialization")) + api(project(":compiler:psi")) + api(project(":analysis:decompiled:decompiler-to-file-stubs")) + api(project(":analysis:decompiled:decompiler-to-psi")) + api(project(":js:js.serializer")) + compileOnly(intellijCore()) + + testImplementation(projectTests(":compiler:tests-common")) + testImplementation(projectTests(":compiler:tests-common-new")) +} + +testsJar() diff --git a/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileDecompiler.kt b/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileDecompiler.kt new file mode 100644 index 00000000000..71306f1590a --- /dev/null +++ b/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileDecompiler.kt @@ -0,0 +1,40 @@ +/* + * 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.analysis.decompiler.js + +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.kotlin.analysis.decompiler.psi.KotlinMetadataDecompiler +import org.jetbrains.kotlin.analysis.decompiler.stub.file.KotlinMetadataStubBuilder +import org.jetbrains.kotlin.metadata.ProtoBuf +import org.jetbrains.kotlin.metadata.js.JsProtoBuf +import org.jetbrains.kotlin.psi.stubs.KotlinStubVersions +import org.jetbrains.kotlin.serialization.js.DynamicTypeDeserializer +import org.jetbrains.kotlin.serialization.js.JsSerializerProtocol +import org.jetbrains.kotlin.utils.JsMetadataVersion +import java.io.ByteArrayInputStream + +class KotlinJavaScriptMetaFileDecompiler : KotlinMetadataDecompiler( + fileType = KotlinJavaScriptMetaFileType, + serializerProtocol = { JsSerializerProtocol }, + flexibleTypeDeserializer = DynamicTypeDeserializer, + expectedBinaryVersion = { JsMetadataVersion.INSTANCE }, + invalidBinaryVersion = { JsMetadataVersion.INVALID_VERSION }, + stubVersion = KotlinStubVersions.JS_STUB_VERSION +) { + override fun readFile(bytes: ByteArray, file: VirtualFile): KotlinMetadataStubBuilder.FileWithMetadata { + val stream = ByteArrayInputStream(bytes) + + val version = JsMetadataVersion.readFrom(stream) + if (!version.isCompatible()) { + return KotlinMetadataStubBuilder.FileWithMetadata.Incompatible(version) + } + + JsProtoBuf.Header.parseDelimitedFrom(stream) + + val proto = ProtoBuf.PackageFragment.parseFrom(stream, JsSerializerProtocol.extensionRegistry) + return KotlinMetadataStubBuilder.FileWithMetadata.Compatible(proto, version, JsSerializerProtocol) + } +} diff --git a/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileType.kt b/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileType.kt new file mode 100644 index 00000000000..72fb8044755 --- /dev/null +++ b/analysis/decompiled/decompiler-js/src/org/jetbrains/kotlin/analysis/decompiler/js/KotlinJavaScriptMetaFileType.kt @@ -0,0 +1,23 @@ +/* + * 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.analysis.decompiler.js + +import com.intellij.openapi.fileTypes.FileType +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.kotlin.analysis.decompiler.psi.KotlinLabelProviderService +import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializationUtil + +object KotlinJavaScriptMetaFileType : FileType { + override fun getName() = "KJSM" + override fun getDescription() = KotlinLabelProviderService.getService()?.getLabelForKotlinJavaScriptMetaFileType() + ?: "Kotlin JavaScript meta file" + + override fun getDefaultExtension() = KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION + override fun getIcon() = null + override fun isBinary() = true + override fun isReadOnly() = true + override fun getCharset(file: VirtualFile, content: ByteArray) = null +} diff --git a/analysis/decompiled/decompiler-to-psi/src/org/jetbrains/kotlin/analysis/decompiler/psi/KotlinLabelProviderService.kt b/analysis/decompiled/decompiler-to-psi/src/org/jetbrains/kotlin/analysis/decompiler/psi/KotlinLabelProviderService.kt index 6ea1ba89b18..ea01c08014f 100644 --- a/analysis/decompiled/decompiler-to-psi/src/org/jetbrains/kotlin/analysis/decompiler/psi/KotlinLabelProviderService.kt +++ b/analysis/decompiled/decompiler-to-psi/src/org/jetbrains/kotlin/analysis/decompiler/psi/KotlinLabelProviderService.kt @@ -10,6 +10,7 @@ import com.intellij.openapi.application.ApplicationManager abstract class KotlinLabelProviderService { abstract fun getLabelForBuiltInFileType(): String abstract fun getLabelForKlibMetaFileType(): String + abstract fun getLabelForKotlinJavaScriptMetaFileType(): String companion object { fun getService(): KotlinLabelProviderService? = diff --git a/build.gradle.kts b/build.gradle.kts index 25860df9c07..ae204f0deea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -157,6 +157,7 @@ val commonCompilerModules = arrayOf( ":compiler:frontend.java", // TODO this is fe10 module but some utils used in fir ide now ":analysis:decompiled:decompiler-to-stubs", ":analysis:decompiled:decompiler-to-file-stubs", + ":analysis:decompiled:decompiler-js", ":analysis:decompiled:decompiler-native", ":analysis:decompiled:decompiler-to-psi", ":analysis:decompiled:light-classes-for-decompiled", diff --git a/settings.gradle b/settings.gradle index 8ea82230661..d813b9f5091 100644 --- a/settings.gradle +++ b/settings.gradle @@ -571,6 +571,7 @@ include ":generators:analysis-api-generator", ":analysis:decompiled:decompiler-to-psi", ":analysis:decompiled:decompiler-to-stubs", ":analysis:decompiled:decompiler-to-file-stubs", + ":analysis:decompiled:decompiler-js", ":analysis:decompiled:decompiler-native", ":analysis:decompiled:light-classes-for-decompiled", ":analysis:decompiled:light-classes-for-decompiled-fe10",