Skip to content

Commit

Permalink
[Wasm] Load wasm file for browser via Url.href, not with just relativ…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
ilgonmic authored and qodana-bot committed Sep 12, 2024
1 parent 2a0ac60 commit 4f013a4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -298,7 +299,7 @@ $jsCodeBodyIndented
throw "Supported JS engine not detected";
}
const wasmFilePath = ${wasmFilePath.toJsStringLiteral()};
const wasmFilePath = $pathJsStringLiteral;
const importObject = {
js_code,
intrinsics: {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 4f013a4

Please sign in to comment.