From 4f013a493d8af2b9cc2e162a1ebd2213ed59535a Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Thu, 12 Sep 2024 10:27:15 +0000 Subject: [PATCH] [Wasm] Load wasm file for browser via Url.href, not with just relative path If relative path is used, it is loaded relative to current document location, not JS file location. So for example if we are on /foo/bar/index.html and load /project.mjs, it tries to load /foo/bar/project.wasm instead of /project.wasm Webpack could process new URL('project.wasm', import.meta.url).href correctly for such case ^KT-71294 fixed --- .../org/jetbrains/kotlin/backend/wasm/wasmCompiler.kt | 5 +++-- .../kotlin/gradle/KotlinWasmGradlePluginIT.kt | 7 ++++++- .../kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt | 11 ----------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/wasmCompiler.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/wasmCompiler.kt index 60d7147956716..b340f8a9e2535 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/wasmCompiler.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/wasmCompiler.kt @@ -261,6 +261,7 @@ fun WasmCompiledModuleFragment.generateAsyncJsWrapper( }.sorted() .joinToString("\n") //language=js + val pathJsStringLiteral = wasmFilePath.toJsStringLiteral() return """ export async function instantiate(imports={}, runInitializer=true) { const cachedJsObjects = new WeakMap(); @@ -298,7 +299,7 @@ $jsCodeBodyIndented throw "Supported JS engine not detected"; } - const wasmFilePath = ${wasmFilePath.toJsStringLiteral()}; + const wasmFilePath = $pathJsStringLiteral; const importObject = { js_code, intrinsics: { @@ -334,7 +335,7 @@ $imports } if (isBrowser) { - wasmInstance = (await WebAssembly.instantiateStreaming(fetch(wasmFilePath), importObject)).instance; + wasmInstance = (await WebAssembly.instantiateStreaming(fetch(new URL($pathJsStringLiteral,import.meta.url).href), importObject)).instance; } } catch (e) { if (e instanceof WebAssembly.CompileError) { diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinWasmGradlePluginIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinWasmGradlePluginIT.kt index fb981e2fc9bef..28b99ccf85094 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinWasmGradlePluginIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinWasmGradlePluginIT.kt @@ -196,9 +196,14 @@ class KotlinWasmGradlePluginIT : KGPBaseTest() { assertTasksExecuted(":compileProductionExecutableKotlinWasmJsOptimize") assertTasksExecuted(":wasmJsBrowserDistribution") - assertFileInProjectExists("build/${Distribution.DIST}/wasmJs/productionExecutable/redefined-wasm-module-name.wasm") assertFileInProjectExists("build/${Distribution.DIST}/wasmJs/productionExecutable/new-mpp-wasm-js.js") assertFileInProjectExists("build/${Distribution.DIST}/wasmJs/productionExecutable/new-mpp-wasm-js.js.map") + + assertTrue("Expected one wasm file") { + projectPath.resolve("build/${Distribution.DIST}/wasmJs/productionExecutable").toFile().listFiles()!! + .filter { it.extension == "wasm" } + .size == 1 + } } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt index 5ef564e81d5eb..4af93e32cb6fa 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt @@ -237,17 +237,6 @@ abstract class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) : copy.from(processResourcesTask) copy.from(webpackTask.flatMap { it.outputDirectory }) - if (binary.compilation.platformType == KotlinPlatformType.wasm) { - copy.from( - binary.linkSyncTask.zip(binary.linkTask) { linkSyncTask, linkTask -> - val moduleNameProvider = linkTask.compilerOptions.moduleName - linkSyncTask.destinationDirectory.zip(moduleNameProvider) { destDir, moduleName -> - destDir.resolve("$moduleName.wasm") - } - } - ) - } - copy.into(binary.distribution.outputDirectory) }