title: How to execute Kotlin WebAssembly function from JavaScript?
body: <p>My goal is to write a Kotlin library, compile it to WebAssembly and call its functions from JS. Since a few hours I try to get a simple hello world to work. The documentation on this topic is either non existent or well hidden.</p>
  
  <p>This is my kotlin file:</p>
  
  <pre><code>@Used
  public fun hello() {
      println("Hello world!")
  }
  
  fun main(args: Array&lt;String&gt;) {
      println("main() function executed!")
  }
  </code></pre>
  
  <p>When I compile it to WebAssembly, I get a <em>hello.wasm</em> and <em>hello.wasm.js</em> file.</p>
  
  <p>First I tried to use something like this to execute the function:</p>
  
  <pre><code>WebAssembly.instantiateStreaming(fetch('hello.wasm'), importObject)
      .then(obj =&gt; obj.instance.exports.hello());
  </code></pre>
  
  <p>Then I understood that I need to pass the imports from my <em>hello.wasm.js</em> file in the <strong>importObject</strong> parameter. So I guess that I need to use the <em>hello.wasm.js</em> file to correctly initialize my wasm program.</p>
  
  <p>When I load my wasm like the following, I don't get any errors and the <strong>main()</strong> function is executed.</p>
  
  <pre><code>&lt;script wasm="hello.wasm" src="hello.wasm.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>But how can I execute the <strong>hello()</strong> function from JavaScript? The only kotlin wasm examples I found are not calling specific functions but rendering something from the <strong>main()</strong> function.</p>
  
  <p>Also any links to relevant documentation are very much appreciated.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>
  I managed to execute the function, but I don't believe this is the correct way:</p>
  
  <pre><code>&lt;script wasm="hello.wasm" src="hello.wasm.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  WebAssembly.instantiateStreaming(fetch('hello.wasm'), konan_dependencies)
          .then(obj =&gt; obj.instance.exports['kfun:hello$$ValueType']());
  &lt;/script&gt;
  </code></pre>
  
  <p>The problem is, that my wasm file is fetched two times if I do it like that. </p>
  
  <p>Loading only the hello.wasm.js file without the wasm attribute gets me the following error: </p>
  
  <pre><code>Uncaught Error: Could not find the wasm attribute pointing to the WebAssembly binary.
      at Object.konan.moduleEntry (stats.wasm.js:433)
      at stats.wasm.js:532
  </code></pre>