Hi all,
I am experimenting with porting a small game I made using embedded Swift for esp32c6 to Wasm embedded.
I first followed the guide Getting Started with Swift SDKs for WebAssembly and got the basic functionality working:
But then I would also like to add JavaScriptKit to the mix, and found that the examples here uses earlier versions of the toolchain, so I'm not certain if the compiler and linker flags are still relevant:
I had trouble getting this to work and was uncertain about the dependency to swift-dlmalloc which I didn't see in other tutorials, as well as the experimental features which I believe are not experimental any more.
So I discarded that version of the embedded example and instead looked at the JavaScriptKit Hello World tutorial which helped me further:
https://swiftpackageindex.com/swiftwasm/JavaScriptKit/0.37.0/tutorials/javascriptkit/hello-world
I followed the tutorial, but used the embedded SDK from the Wasm Getting Started guide on swift.org.
In order to make that work I added two of the linker flags from the embedded example:
linkerSettings: [
.unsafeFlags(["-Xclang-linker", "-mexec-model=reactor",
"-Xlinker", "--export-if-defined=__main_argc_argv",
])
]
The first one I added since a helpful error message pointed me in that direction - and the second since my project contained top-level code, and I couldn't get a @main-annotated struct to work.
So far, so good. Now I can package my project using:
swift package --swift-sdk $SWIFT_SDK_ID js --use-cdn -c release
(where SWIFT_SDK_ID is swift-6.2.3-RELEASE_wasm-embedded)
After adding a JSClosure, however, I now get an error in the browser when running the project:
[Error] Unhandled Promise Rejection: TypeError: this.exports.swjs_library_features is not a function. (In 'this.exports.swjs_library_features()', 'this.exports.swjs_library_features' is undefined)
In the packaged javascript code I can see the failing code, but I don't know where the swjs_library_features() definition is supposed to come from - or if the JSClosure I am adding is even supported in embedded mode.
So my question is: Can I make the JSClosure work in embedded mode, and how / where might I define the swjs_library_features?
I apologize if I have started out in the wrong place or missed something essential in one of the guides.
Here's my full Package.swift:
// swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Hello",
dependencies: [
.package(url: "https://github.com/swiftwasm/JavaScriptKit.git", revision: "4b327dc40d3931c2236ed43c6b47325c17c88afe"),
],
targets: [
.executableTarget(
name: "Hello",
dependencies: [
.product(name: "JavaScriptKit", package: "JavaScriptKit"),
],
linkerSettings: [
.unsafeFlags(["-Xclang-linker", "-mexec-model=reactor",
"-Xlinker", "--export-if-defined=__main_argc_argv",
])
]
),
]
)