Open sjorn3 opened 6 years ago
Now if you export the same function multiple times with different types the first one is given an unaltered name, and the rest are given a name with the parameter types inserted. This is somewhat arbitrary and can be changed, but before all but the last export would just get thrown away.
@wasm addTwo(Int, Int) addTwo(Float32, Float32) addTwo(Int32, Int32) addTwo(Float64, Float64)
(module
(export "addTwo_f32_f32" (func $#addTwo_Float32_Float32))
(export "addTwo" (func $#addTwo_Int64_Int64))
(export "addTwo_f64_f64" (func $#addTwo_Float64_Float64))
(export "addTwo_i32_i32" (func $#addTwo_Int32_Int32))
...
Annoyingly, in wasm you can import the same function multiple times to different names (for calling with different parameters) but can't go the other way, so they have to be given different names and it'll have to be left to the user to call the right one. It would be a really nice feature if we could do some js trickery to turn them all into a single function.
Generalised the syntax a bit further. An array of types compiles the sum of those types, and tuples of types compiles the product of them. For example:
@wasm f([Int, Int32], [Int, Int32]) ==
@wasm f(Int, Int) f(Int32, Int32)
@wasm f((Int, Int32), (Int, Int32)) ==
@wasm f(Int, Int) f(Int, Int32) f(Int32, Int) f(Int32, Int32)
Futher, you're allowed to place a tuple inside an array. (But that's it, no recursion or anything as I don't think it makes sense).
Using this syntax makes them all have names in a form like f_i32_i32
, and so the functions can be tied together on the other end. Unless only one function is produced, then it's just given the base name.
However, if you make another call that uses up the name then the tying won't happen, e.g.:
@wasm f((Int, Int32), (Int, Int32)) f(Float64, Float64)
Will mean the version that gets called f
is the Float64 one. The version you want to have the base name should be placed first in the call to @wasm
Now it's possible to compile modules with the
@wasm
macro as specified by a comment in runtests.jlSince it's implemented as a postwalk of the expression tree (and the input is slurped), you can pretty much do whatever you want.
I'd say it supersedes
@code_wasm
as it is a simple ui and won't buckle under recursion.I've also improved the recursion fix a bit, since it also needed to be applied to lower_invoke.