Skip to content

Commit

Permalink
Merge pull request #263 from sliemeobn/swift-embedded
Browse files Browse the repository at this point in the history
Support for Embedded Swift (v2)
  • Loading branch information
kateinoigakukun authored Oct 14, 2024
2 parents 6d4a114 + 69c58dd commit 459d1e9
Show file tree
Hide file tree
Showing 29 changed files with 1,675 additions and 80 deletions.
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

0 comments on commit 459d1e9

Please sign in to comment.