-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe0b4ae
getting rid of existentials, patched headers in C parts
sliemeobn 11897e1
fixed C bit field thing
sliemeobn 26b7a8e
added embedded example and conditional package flags
sliemeobn b010624
...memmove, we meet again
sliemeobn 4912646
readme
sliemeobn 025b9fc
revert to cdecl-based exports for swift 5.10 compatibility
sliemeobn 77bbc8c
added compiler gates for @_expose
sliemeobn 7f2f7c6
less embedded conditions and added comments
sliemeobn 69c58dd
added explicit returns for 5.8 compatibility
sliemeobn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.