Newbie question here: when using the Wasm targets,...
# webassembly
f
Newbie question here: when using the Wasm targets, can one just build applications or can also build libraries that I could load elsewhere? I would expect to be able to produce a wasm file from my Kotlin code, and then to somehow use that wasm file, for example from python with some wasm loading library. Would that make sense? If so, what do I run to compile my library to wasm (supposing I have setup the project as a kotlin multi-platform project and added the wasm-wasi target)? Where do I get my wasm file?
s
Libraries can have WASM targets.
s
You can build libraries today, but most non-JS runtimes wouldn’t be able to run them today. Having said that, you can run the task
compileProductionExecutableKotlinWasmWasi
and see the output in
build
(Gradle plugin support for WASI is a bit rough, and there could be a better way that I don’t know of)
Copy code
build
└── compileSync
   └── wasmWasi
       └── main
          └── productionExecutable
              └── kotlin
                 ├── kotlin-wasm-wasi-example-wasm-wasi.map
                 ├── kotlin-wasm-wasi-example-wasm-wasi.mjs
                 └── kotlin-wasm-wasi-example-wasm-wasi.wasm
The
kotlin-wasm-wasi-example-wasm-wasi.wasm
is the file you are looking for. You can, in theory, use it independently with any wasm runtime that supports recent additions like WasmGC. In practice, these runtimes are V8 and SpiderMonkey, and your python embedding probably wouldn’t support it yet.
👍 2
In addition, as a limitation of the raw
.wasm
format, you can only
@WasmExport
functions with numbers, making your library API very low-level. Upcoming Component Model spec promises to enable rich types in many languages, like python.