scala-native-wasm icon indicating copy to clipboard operation
scala-native-wasm copied to clipboard

Any idea how to export methods?

Open Sciss opened this issue 6 years ago • 2 comments

Thank you for this project, it works nicely! Do you happen to know if there is a trick to export methods so that they can be passed to emcc's -s EXPORTED_FUNCTIONS? What I would need is a way to create a cwrap from JavaScript in order to patch the wasm code into web audio callbacks.

Sciss avatar Jan 12 '19 17:01 Sciss

Right now, Scala Native doesn't let you export methods (the whole area of using Scala Native compiled code from elsewhere hasn't been developed yet).

I do have a theoretical hack that might make things work, but haven't tested this myself. Since Scala Native can call into C functions, you could write a small piece of glue C code that has two functions: one that receives a function pointer from Scala Native and one that is exported which internally just calls the function pointer received by the first. Scala Native does have the ability to expose Scala functions as function pointers (I used this to write a JNI compatibility layer a while ago), so this could be used to expose a Scala-defined function to JS.

shadaj avatar Jan 12 '19 17:01 shadaj

Brilliant, that works. My test here: https://github.com/Sciss/scala-native-wasm/commit/340890cfc89ecabb09d1a73ad5a855e762c42ad6

Sciss avatar Jan 12 '19 19:01 Sciss