Skip to content

Commit faab359

Browse files
Merge pull request #114 from heckj/swift6compile-examples
global variable initialization isn't allow with throw in swift6 compiler - adds boilerplate main() around the examples showing use of WasmKit
2 parents 7238410 + 3883aa5 commit faab359

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed

Examples/Sources/Factorial/Factorial.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import WasmKit
44
import WAT
55
import Foundation
66

7-
// Convert a WAT file to a Wasm binary, then parse it.
8-
let module = try parseWasm(
9-
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat"))
10-
)
7+
@main
8+
struct Example {
9+
static func main() throws {
10+
// Convert a WAT file to a Wasm binary, then parse it.
11+
let module = try parseWasm(
12+
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat"))
13+
)
1114

12-
// Create a module instance from the parsed module.
13-
let runtime = Runtime()
14-
let instance = try runtime.instantiate(module: module)
15-
let input: UInt64 = 5
16-
// Invoke the exported function "fac" with a single argument.
17-
let result = try runtime.invoke(instance, function: "fac", with: [.i64(input)])
18-
print("fac(\(input)) = \(result[0].i64)")
15+
// Create a module instance from the parsed module.
16+
let runtime = Runtime()
17+
let instance = try runtime.instantiate(module: module)
18+
let input: UInt64 = 5
19+
// Invoke the exported function "fac" with a single argument.
20+
let result = try runtime.invoke(instance, function: "fac", with: [.i64(input)])
21+
print("fac(\(input)) = \(result[0].i64)")
22+
}
23+
}

Examples/Sources/PrintAdd/PrintAdd.swift

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@
33
import WasmKit
44
import WAT
55

6-
// Convert a WAT file to a Wasm binary, then parse it.
7-
let module = try parseWasm(
8-
bytes: try wat2wasm(
9-
"""
10-
(module
11-
(import "printer" "print_i32" (func $print_i32 (param i32)))
12-
(func (export "print_add") (param $x i32) (param $y i32)
13-
(call $print_i32 (i32.add (local.get $x) (local.get $y)))
14-
)
6+
@main
7+
struct Example {
8+
static func main() throws {
9+
// Convert a WAT file to a Wasm binary, then parse it.
10+
let module = try parseWasm(
11+
bytes: try wat2wasm(
12+
"""
13+
(module
14+
(import "printer" "print_i32" (func $print_i32 (param i32)))
15+
(func (export "print_add") (param $x i32) (param $y i32)
16+
(call $print_i32 (i32.add (local.get $x) (local.get $y)))
17+
)
18+
)
19+
"""
20+
)
1521
)
16-
"""
17-
)
18-
)
1922

20-
// Define a host function that prints an i32 value.
21-
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
22-
// This function is called from "print_add" in the WebAssembly module.
23-
print(args[0])
24-
return []
23+
// Define a host function that prints an i32 value.
24+
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
25+
// This function is called from "print_add" in the WebAssembly module.
26+
print(args[0])
27+
return []
28+
}
29+
// Create a runtime importing the host function.
30+
let runtime = Runtime(hostModules: [
31+
"printer": HostModule(functions: ["print_i32": hostPrint])
32+
])
33+
let instance = try runtime.instantiate(module: module)
34+
// Invoke the exported function "print_add"
35+
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])
36+
}
2537
}
26-
// Create a runtime importing the host function.
27-
let runtime = Runtime(hostModules: [
28-
"printer": HostModule(functions: ["print_i32": hostPrint])
29-
])
30-
let instance = try runtime.instantiate(module: module)
31-
// Invoke the exported function "print_add"
32-
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])

Examples/Sources/WASI-Hello/Hello.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ import WasmKitWASI
55
import WAT
66
import Foundation
77

8-
// Parse a WASI-compliant WebAssembly module from a file.
9-
let module = try parseWasm(filePath: "wasm/hello.wasm")
8+
@main
9+
struct Example {
10+
static func main() throws {
11+
// Parse a WASI-compliant WebAssembly module from a file.
12+
let module = try parseWasm(filePath: "wasm/hello.wasm")
1013

11-
// Create a WASI instance forwarding to the host environment.
12-
let wasi = try WASIBridgeToHost()
13-
// Create a runtime with WASI host modules.
14-
let runtime = Runtime(hostModules: wasi.hostModules)
15-
let instance = try runtime.instantiate(module: module)
14+
// Create a WASI instance forwarding to the host environment.
15+
let wasi = try WASIBridgeToHost()
16+
// Create a runtime with WASI host modules.
17+
let runtime = Runtime(hostModules: wasi.hostModules)
18+
let instance = try runtime.instantiate(module: module)
1619

17-
// Start the WASI command-line application.
18-
let exitCode = try wasi.start(instance, runtime: runtime)
19-
// Exit the Swift program with the WASI exit code.
20-
exit(Int32(exitCode))
20+
// Start the WASI command-line application.
21+
let exitCode = try wasi.start(instance, runtime: runtime)
22+
// Exit the Swift program with the WASI exit code.
23+
exit(Int32(exitCode))
24+
}
25+
}

0 commit comments

Comments
 (0)