I have an external Wasm file named “rust_library.w...
# webassembly
m
I have an external Wasm file named “rust_library.wasm” which exports a function “fibonacci”. Where do I have to place this file and what else has to be done to be able to directly import this function into Kotlin/wasmJs and thus avoid going through JS interop? I have placed it into “.../src/wasmJsMain/resources” and imported the function like this:
Copy code
@kotlin.wasm.WasmImport("rust_library", "fibonacci")
external fun calculateFibonacci(n: Int): Int
But at build time I only get
Copy code
Module not found: Error: Can't resolve 'rust_library' in '/Users/mpaus/Projects/tmp/EmmentalerDemo/build/js/packages/composeApp/kotlin'
although this folder actually contains the Wasm file. The Wasm file itself is correct because it works nicely in Chasm, Chickory and GraalVMWasm.
I got it working myself. The correct way to import the module is:
Copy code
@kotlin.wasm.WasmImport("./rust_library.wasm", "fibonacci")
external fun calculateFibonacci(n: Int): Int
So, the module here is actually a relative file path and not only the module name. Calling this function like this
Copy code
val fib = calculateFibonacci(10)
println("fib(10) = $fib (should be 55)")
actually produces
Copy code
fib(10) = 55 (should be 55)
in the browser console 🎉. Now let’s see whether I can also get less trivial code to work.