Skip to content

Commit d0b6ce5

Browse files
authored
Add clarity on some facts. (#29)
1 parent b5d7d4e commit d0b6ce5

File tree

6 files changed

+62
-49
lines changed

6 files changed

+62
-49
lines changed

src/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ WebAssembly is described on its [home page](https://webassembly.org/) as:
1313

1414
We use LLVM as a compiler backend to produce WebAssembly binaries. Our resulting binaries also depend on [WASI](https://wasi.dev), which is a modular system interface for WebAssembly. WASI is mainly required to compile Swift Standard Library.
1515

16+
17+
### Important note
18+
In 2024, Apple introduced [Swift Embedded](https://github.com/swiftlang/swift/blob/main/docs/EmbeddedSwift/UserManual.md).
19+
While both projects benefit from each other, it is important to understand that they are different targets at the build phase, consequentially with different sets of abilities.
20+
Embedded Swift [very limited](https://github.com/swiftlang/swift/blob/main/docs/EmbeddedSwift/EmbeddedSwiftStatus.md#embedded-swift-language-features) but can produce small binaries. [Example](https://github.com/apple/swift-for-wasm-examples).
21+
22+
23+
1624
## Project Status
1725

1826
[All compiler and standard library changes have been upstreamed to the official Swift repository, and the upstream CI is now testing WebAssembly targets.](https://forums.swift.org/t/stdlib-and-runtime-tests-for-wasm-wasi-now-available-on-swift-ci/70385)

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [Introduction](README.md)
44
- [Getting Started](getting-started/index.md)
55
- [Installation](getting-started/setup.md)
6-
- [Porting code from other platforms](getting-started/porting.md)
6+
- [Porting code](getting-started/porting.md)
77
- [Creating a browser app](getting-started/browser-app.md)
88
- [JavaScript interoperation](getting-started/javascript-interop.md)
99
- [Concurrency](getting-started/concurrency.md)

src/examples/exporting-function.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Exporting function for host environment
22

3+
## Swift 6.0 or later
4+
5+
If you use Swift 6.0 or later, you can use `@_expose(wasm, "add")` and omit the `--export` linker flag.
6+
7+
```swift
8+
// File name: lib.swift
9+
@_expose(wasm, "add")
10+
@_cdecl("add") // This is still required to call the function with C ABI
11+
func add(_ lhs: Int, _ rhs: Int) -> Int {
12+
return lhs + rhs
13+
}
14+
```
15+
16+
Then you can compile the Swift code with the following command without `--export` linker flag.
17+
18+
```bash
19+
$ swiftc \
20+
-target wasm32-unknown-wasi \
21+
-parse-as-library \
22+
lib.swift -o lib.wasm \
23+
-Xclang-linker -mexec-model=reactor
24+
```
25+
326
## Swift 5.10 or earlier
427

528
You can expose a Swift function for host environment using special attribute and linker option.
@@ -62,25 +85,3 @@ console.log("2 + 3 = " + addFn(2, 3))
6285

6386
If you use SwiftPM package, you can omit linker flag using clang's `__atribute__`. Please see [swiftwasm/JavaScriptKit#91](https://github.com/swiftwasm/JavaScriptKit/pull/91/files) for more detail info
6487

65-
## Swift 6.0 or later
66-
67-
If you use Swift 6.0 or later, you can use `@_expose(wasm, "add")` and omit the `--export` linker flag.
68-
69-
```swift
70-
// File name: lib.swift
71-
@_expose(wasm, "add")
72-
@_cdecl("add") // This is still required to call the function with C ABI
73-
func add(_ lhs: Int, _ rhs: Int) -> Int {
74-
return lhs + rhs
75-
}
76-
```
77-
78-
Then you can compile the Swift code with the following command without `--export` linker flag.
79-
80-
```bash
81-
$ swiftc \
82-
-target wasm32-unknown-wasi \
83-
-parse-as-library \
84-
lib.swift -o lib.wasm \
85-
-Xclang-linker -mexec-model=reactor
86-
```

src/examples/importing-function.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# Importing a function from host environments
22

3+
## Swift 6.0 or later
4+
5+
If you are using Swift 6.0 or later, you can use experimental `@_extern(wasm)` attribute
6+
7+
Swift 6.0 introduces a new attribute `@_extern(wasm)` to import a function from the host environment.
8+
To use this experimental feature, you need to enable it in your SwiftPM manifest file:
9+
10+
```swift
11+
.executableTarget(
12+
name: "Example",
13+
swiftSettings: [
14+
.enableExperimentalFeature("Extern")
15+
]),
16+
```
17+
18+
Then, you can import a function from the host environment as follows without using C headers:
19+
20+
```swift
21+
@_extern(wasm, module: "env", name: "add")
22+
func add(_ a: Int, _ b: Int) -> Int
23+
24+
print(add(2, 2))
25+
```
26+
327
## Swift 5.10 or earlier
428

529
You can import a function from your host environment using the integration of Swift Package Manager
@@ -85,26 +109,3 @@ A more streamlined way to import host functions will be implemented in the futur
85109
SwiftWasm toolchain.
86110

87111

88-
## Swift 6.0 or later
89-
90-
If you are using Swift 6.0 or later, you can use experimental `@_extern(wasm)` attribute
91-
92-
Swift 6.0 introduces a new attribute `@_extern(wasm)` to import a function from the host environment.
93-
To use this experimental feature, you need to enable it in your SwiftPM manifest file:
94-
95-
```swift
96-
.executableTarget(
97-
name: "Example",
98-
swiftSettings: [
99-
.enableExperimentalFeature("Extern")
100-
]),
101-
```
102-
103-
Then, you can import a function from the host environment as follows without using C headers:
104-
105-
```swift
106-
@_extern(wasm, module: "env", name: "add")
107-
func add(_ a: Int, _ b: Int) -> Int
108-
109-
print(add(2, 2))
110-
```

src/getting-started/browser-app.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ already automated with `carton`.
1313

1414
### System Requirements
1515

16-
- [Swift 5.9.2 or later](https://swift.org/download/)
16+
- [Swift 5.9.2](https://swift.org/download/)
17+
18+
>Important:
19+
>Tokamak UI currently is not compatible with swift 6.0+.
1720
1821
### Installation
1922

@@ -51,7 +54,7 @@ let package = Package(
5154
)
5255
```
5356

54-
4. Add your first view to `Sources/main.swift`:
57+
4. Add your first view to `Sources/MyApp/main.swift`:
5558

5659
```swift
5760
import TokamakDOM

src/getting-started/porting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Porting code from other platforms to WebAssembly
1+
# Porting code to WebAssembly with WASI
22

33
In the form that's currently standardized and supported by browsers and other hosts, WebAssembly
44
is a 32-bit architecture. You have to take this into account when porting code from other

0 commit comments

Comments
 (0)