-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WASI hello world program example
- Loading branch information
1 parent
663fcfa
commit a0f1ffb
Showing
2 changed files
with
27 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Compile: swiftc ./hello.swift -o hello.wasm -target wasm32-unknown-none-wasm -enable-experimental-feature Extern -enable-experimental-feature Embedded -wmo -Xcc -fdeclspec -Xclang-linker -nostdlib -Xfrontend -disable-stack-protector -Osize | ||
// Swift version: DEVELOPMENT-SNAPSHOT-2024-06-13-a | ||
|
||
// This is a simple WASI program written in Embedded Swift. | ||
|
||
@_extern(wasm, module: "wasi_snapshot_preview1", name: "fd_write") | ||
@_extern(c) | ||
func fd_write(fd: Int32, iovs: UnsafeRawPointer, iovs_len: Int32, nwritten: UnsafeMutablePointer<Int32>) -> Int32 | ||
|
||
func _print(_ string: StaticString) { | ||
string.withUTF8Buffer { string in | ||
withUnsafeTemporaryAllocation(byteCount: 8, alignment: 4) { iov in | ||
let iov = iov.baseAddress! | ||
iov.advanced(by: 0).storeBytes(of: string.baseAddress!, as: UnsafeRawPointer.self) | ||
iov.advanced(by: 4).storeBytes(of: Int32(string.count), as: Int32.self) | ||
var nwritten: Int32 = 0 | ||
_ = fd_write(fd: 1, iovs: iov, iovs_len: 1, nwritten: &nwritten) | ||
} | ||
} | ||
} | ||
|
||
// The entry point of this WASI program. | ||
@_expose(wasm, "_start") | ||
@_cdecl("_start") | ||
func _start() { | ||
_print("Hello, World!\n") | ||
} |
Binary file not shown.