[Wasm] Add initialization flag
diff --git a/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArgumentsCopyGenerated.kt b/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArgumentsCopyGenerated.kt index af02ea8..7409d736 100644 --- a/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArgumentsCopyGenerated.kt +++ b/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArgumentsCopyGenerated.kt
@@ -68,6 +68,7 @@ to.useEsGenerators = from.useEsGenerators to.wasm = from.wasm to.wasmDebug = from.wasmDebug + to.wasmDisableInitialization = from.wasmDisableInitialization to.wasmEnableArrayRangeChecks = from.wasmEnableArrayRangeChecks to.wasmEnableAsserts = from.wasmEnableAsserts to.wasmGenerateWat = from.wasmGenerateWat
diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt index 71ca8da..ef65057 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt
@@ -634,6 +634,16 @@ } @Argument( + value = "-Xwasm-disable-initialization", + description = "Disables the automatic initialization of the WebAssembly module. When this option is active, the exported function '_initialize' must be called manually prior to calling any other exported functions." + ) + var wasmDisableInitialization = false + set(value) { + checkFrozen() + field = value + } + + @Argument( value = "-Xoptimize-generated-js", description = "Perform additional optimizations on the generated JS code." )
diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt index 4f7d18d..3ee89ed 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt
@@ -379,6 +379,7 @@ baseFileName = outputName, emitNameSection = arguments.wasmDebug, allowIncompleteImplementations = arguments.irDce, + initialization = !arguments.wasmDisableInitialization, generateWat = configuration.get(JSConfigurationKeys.WASM_GENERATE_WAT, false), generateSourceMaps = generateSourceMaps, )
diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt index 50c0dcc..41281f1 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt
@@ -47,7 +47,7 @@ class WasmCompilerResult( val wat: String?, val jsUninstantiatedWrapper: String?, - val jsWrapper: String, + val jsWrapper: String?, val wasm: ByteArray, val debugInformation: DebugInformation?, val dts: String? @@ -130,6 +130,7 @@ baseFileName: String, emitNameSection: Boolean = false, allowIncompleteImplementations: Boolean = false, + initialization: Boolean, generateWat: Boolean = false, generateSourceMaps: Boolean = false, ): WasmCompilerResult { @@ -172,19 +173,23 @@ val byteArray = os.toByteArray() val jsUninstantiatedWrapper: String? - val jsWrapper: String + val jsWrapper: String? if (backendContext.isWasmJsTarget) { jsUninstantiatedWrapper = compiledWasmModule.generateAsyncJsWrapper( "./$baseFileName.wasm", backendContext.jsModuleAndQualifierReferences ) - jsWrapper = compiledWasmModule.generateEsmExportsWrapper( - "./$baseFileName.uninstantiated.mjs", - backendContext.jsModuleAndQualifierReferences - ) + jsWrapper = if (initialization) { + compiledWasmModule.generateEsmExportsWrapper( + "./$baseFileName.uninstantiated.mjs", + backendContext.jsModuleAndQualifierReferences + ) + } else null } else { jsUninstantiatedWrapper = null - jsWrapper = compiledWasmModule.generateAsyncWasiWrapper("./$baseFileName.wasm") + jsWrapper = if (initialization) { + compiledWasmModule.generateAsyncWasiWrapper("./$baseFileName.wasm") + } else null } @@ -423,10 +428,16 @@ } File(dir, "$fileNameBase.wasm").writeBytes(result.wasm) - if (result.jsUninstantiatedWrapper != null) { - File(dir, "$fileNameBase.uninstantiated.mjs").writeText(result.jsUninstantiatedWrapper) + val uninstantiatedFileName = if (result.jsWrapper != null) { + File(dir, "$fileNameBase.mjs").writeText(result.jsWrapper) + "$fileNameBase.uninstantiated.mjs" + } else { + "$fileNameBase.mjs" } - File(dir, "$fileNameBase.mjs").writeText(result.jsWrapper) + + if (result.jsUninstantiatedWrapper != null) { + File(dir, uninstantiatedFileName).writeText(result.jsUninstantiatedWrapper) + } result.debugInformation?.sourceMapForBinary?.let { File(dir, "$fileNameBase.wasm.map").writeText(it)
diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index 5812b78..c2c274d 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out
@@ -54,6 +54,7 @@ -Xes-generators Enable ES2015 generator functions usage inside the compiled code -Xwasm Use the experimental WebAssembly compiler backend. -Xwasm-debug-info Add debug info to the compiled WebAssembly module. + -Xwasm-disable-initialization Disables the automatic initialization of the WebAssembly module. When this option is active, the exported function '_initialize' must be called manually prior to calling any other exported functions. -Xwasm-enable-array-range-checks Turn on range checks for array access functions. -Xwasm-enable-asserts Turn on asserts.
diff --git a/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/converters/WasmBackendFacade.kt b/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/converters/WasmBackendFacade.kt index bf08e7e..5841b52 100644 --- a/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/converters/WasmBackendFacade.kt +++ b/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/converters/WasmBackendFacade.kt
@@ -112,6 +112,7 @@ baseFileName = baseFileName, emitNameSection = true, allowIncompleteImplementations = false, + initialization = true, generateWat = generateWat, generateSourceMaps = generateSourceMaps, ) @@ -128,6 +129,7 @@ baseFileName = baseFileName, emitNameSection = true, allowIncompleteImplementations = true, + initialization = true, generateWat = generateWat, generateSourceMaps = generateSourceMaps, )