tshort / WebAssemblyCompiler.jl

Create WebAssembly with Julia
https://tshort.github.io/WebAssemblyCompiler.jl/
MIT License
76 stars 4 forks source link

Returning an array from Javascript (RuntimeError: unreachable) #8

Closed HildingElmqvist closed 11 months ago

HildingElmqvist commented 11 months ago

I want to allow entering the initial conditions in the web app. I thus want to call a Javscript function which returns a vector from Julia: `

  function getInitial() {

    return [5.0, 6.0, 7.0]  

  }

`

I declared the return type as Externref: initial = @jscall("getInitial", Externref, Tuple{}) I added some logging:

`

JS.console_log("initial")

JS.console_log(initial)

JS.console_log("initial[1]")

JS.console_log(initial[1])

JS.console_log("Done")

`

I am not able to pick up the first element of the vector. I get:

`

initial

(3) [5, 6, 7]

initial[1]

lorenz.wasm:0xd2 Uncaught RuntimeError: unreachable

at lorenz.wasm:0xd2

at run ((index):88:16)

at async (index):90:15`

Is this supposed to work or is there any work around?

tshort commented 11 months ago

The Externref isn't directly usable within Julia. You can only pass it back to JavaScript. You should be able to call JS._get(initial, 1, Float64) to extract an element from the JS array.

Wait. There's an easier way. You should be able to get that array in Julia with Vector{Float64}(initial).

Let me know if that doesn't work.

HildingElmqvist commented 11 months ago

Thanks for your quick suggestions!

No, Vector{Float64}(initial) did not work. The compilation script returns too quickly without any error message.

JS._get(initial, 2, Float64) works. Is there a way of getting the size of the array or I have to query that separately?

tshort commented 11 months ago

Try:

n = @jscall("x => x.length", Int32, Tuple{Externref}, jsa)

I need to fix and add a test case for Vector{T}.

HildingElmqvist commented 11 months ago

n = @jscall("x => x.length", Int32, Tuple{Externref}, jsa) worked fine.

Thanks a lot!