From a0f1ffbe4b68125141217e63b02d528cd779084f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 1 Jul 2024 13:40:17 +0900 Subject: [PATCH] Add WASI hello world program example --- Examples/hello.swift | 27 +++++++++++++++++++++++++++ Examples/hello.wasm | Bin 0 -> 286 bytes 2 files changed, 27 insertions(+) create mode 100644 Examples/hello.swift create mode 100755 Examples/hello.wasm diff --git a/Examples/hello.swift b/Examples/hello.swift new file mode 100644 index 00000000..cabb4f04 --- /dev/null +++ b/Examples/hello.swift @@ -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 + +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") +} diff --git a/Examples/hello.wasm b/Examples/hello.wasm new file mode 100755 index 0000000000000000000000000000000000000000..59794330bbe7268ca3e38881b6389b7029ca855c GIT binary patch literal 286 zcmW-bKTg9i7{$NO4IyfxHWCs7ra~*0v`Ae7tHr_zfX#KO6WlsUu^j?aU}J+LZ~aW6$P4YCDmw6p@C zLlCz*2*eMtMf(2Yh1)yCFGNx4M*==GmTL>xJx7Y99RiJ4M=*lHe?S+I_EO?GdkRvr z*&!tnMK_=;(K3dE4ZIvd7$yW97WOWLQq_2HRcW<2>;ioa!uZrb{yBTT{{H-WyZQLN zyo3F{6Vu4dP8qLlrDVBcqO@8H1LMAXl?%%