Skip to content

WIP: Support for Embedded Swift (v2) #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/Basic/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PackageDescription

let package = Package(
name: "Basic",
platforms: [
.macOS(.v14)
],
dependencies: [.package(name: "JavaScriptKit", path: "../../")],
targets: [
.executableTarget(
Expand Down
2 changes: 1 addition & 1 deletion Examples/Basic/build.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
swift build --swift-sdk DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor -Xlinker --export=__main_argc_argv
swift build --swift-sdk DEVELOPMENT-SNAPSHOT-2024-09-20-a-wasm32-unknown-wasi -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor -Xlinker --export=__main_argc_argv
6 changes: 6 additions & 0 deletions Examples/Embedded/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
Package.resolved
20 changes: 20 additions & 0 deletions Examples/Embedded/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.10

import PackageDescription

let package = Package(
name: "Embedded",
dependencies: [
.package(name: "JavaScriptKit", path: "../../"),
.package(url: "https://github.com/swifweb/EmbeddedFoundation", branch: "0.1.0")
],
targets: [
.executableTarget(
name: "EmbeddedApp",
dependencies: [
"JavaScriptKit",
.product(name: "Foundation", package: "EmbeddedFoundation")
]
)
]
)
8 changes: 8 additions & 0 deletions Examples/Embedded/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Embedded example

Requires a recent DEVELOPMENT-SNAPSHOT toolchain. (tested with swift-DEVELOPMENT-SNAPSHOT-2024-09-25-a)

```sh
$ ./build.sh
$ npx serve
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import JavaScriptKit

// NOTE: it seems the embedded tree shaker gets rid of these exports if they are not used somewhere
func _i_need_to_be_here_for_wasm_exports_to_work() {
_ = _swjs_library_features
_ = _swjs_call_host_function
_ = _swjs_free_host_function
}

// TODO: why do I need this? and surely this is not ideal... figure this out, or at least have this come from a C lib
@_cdecl("strlen")
func strlen(_ s: UnsafePointer<Int8>) -> Int {
var p = s
while p.pointee != 0 {
p += 1
}
return p - s
}

// TODO: why do I need this? and surely this is not ideal... figure this out, or at least have this come from a C lib
@_cdecl("memmove")
func memmove(_ dest: UnsafeMutableRawPointer, _ src: UnsafeRawPointer, _ n: Int) -> UnsafeMutableRawPointer {
let d = dest.assumingMemoryBound(to: UInt8.self)
let s = src.assumingMemoryBound(to: UInt8.self)
for i in 0..<n {
d[i] = s[i]
}
return dest
}
26 changes: 26 additions & 0 deletions Examples/Embedded/Sources/EmbeddedApp/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import JavaScriptKit

let alert = JSObject.global.alert.function!
let document = JSObject.global.document

print("Hello from WASM, document title: \(document.title.string ?? "")")

var count = 0

var divElement = document.createElement("div")
divElement.innerText = .string("Count \(count)")
_ = document.body.appendChild(divElement)

var buttonElement = document.createElement("button")
buttonElement.innerText = "Click me"
buttonElement.onclick = JSValue.object(JSClosure { _ in
count += 1
divElement.innerText = .string("Count \(count)")
return .undefined
})

_ = document.body.appendChild(buttonElement)

func print(_ message: String) {
_ = JSObject.global.console.log(message)
}
Empty file.
Loading
Loading