-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from sliemeobn/swift-embedded
Support for Embedded Swift (v2)
- Loading branch information
Showing
29 changed files
with
1,675 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
Package.resolved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
] | ||
) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
29 changes: 29 additions & 0 deletions
29
Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.