Issue using Swift macros in SwiftPM package when targeting Wasm

Thanks for the help! After resolving a few other issues that I ran into with JavaScriptKit and stuff, I finally managed to get an online playground working :tada:


I faced a few different issues while trying to get this working, if any of these aren't already known I'd be happy to split them out into separate topics or GitHub issues.

Hiccups

My first working wasm example was a simple print("Hello, world!") which I got working one I built SwiftPM from the main branch. When I then tried to add the JavaScriptKit dependency the runtime let me know that JavaScriptKit requires the reactor ABI, which I promptly built my example with.

Whenever I loaded this latest build (with the reactor ABI) in the browser nothing would happen, even if I included (Int?.none)! to force a fatal error nothing happened. After quite a bit of digging I eventually discovered that JavaScriptKit looks for an exported main function to call which my built wasm didn't have. I ended up solving the issue by adding ["-Xlinker", "--export=main"] to the target's linkerSettings in my package manifest.

Is that meant to be required or might it be something strange about my Swift toolchain? If it's meant to be required as a workaround, it'd be great if that could be added to the docs.

Up-to-date SwiftWASM examples

When trying to resolve my issues I was trying to find example projects to see if I was doing anything differently, but all of the projects that I found hadn't been touched in a few years and either relied on old versions of carton and JavaScriptKit or wouldn't compile. Are there any up-to-date SwiftWASM examples that I missed? If not, I might try updating an existing one for future reference (happy to update whichever example is decided to be the most 'official').

2 Likes