Skip to content

Commit 4729c8d

Browse files
Update examples to use WAT module
1 parent aa50d6b commit 4729c8d

File tree

11 files changed

+133
-61
lines changed

11 files changed

+133
-61
lines changed

Examples/Package.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swift-tools-version:5.8
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Examples",
7+
platforms: [.macOS(.v10_13), .iOS(.v12)],
8+
dependencies: [
9+
.package(path: "../")
10+
],
11+
targets: [
12+
.executableTarget(name: "Factorial", dependencies: [
13+
"WasmKit",
14+
.product(name: "WAT", package: "WasmKit")
15+
]),
16+
.executableTarget(name: "PrintAdd", dependencies: [
17+
"WasmKit",
18+
.product(name: "WAT", package: "WasmKit")
19+
]),
20+
.executableTarget(name: "WASI-Hello", dependencies: [
21+
"WasmKit",
22+
.product(name: "WasmKitWASI", package: "WasmKit"),
23+
.product(name: "WAT", package: "WasmKit"),
24+
]),
25+
]
26+
)

Examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
This directory contains a number of examples that demonstrate how to use the `WasmKit` library.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// This example demonstrates how to call a function exported by a WebAssembly module.
2+
3+
import WasmKit
4+
import WAT
5+
import Foundation
6+
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+
)
11+
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)")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// This example demonstrates how to import a function from the host environment and call it from a WebAssembly module.
2+
3+
import WasmKit
4+
import WAT
5+
import Foundation
6+
7+
// Convert a WAT file to a Wasm binary, then parse it.
8+
let module = try parseWasm(
9+
bytes: try wat2wasm(
10+
"""
11+
(module
12+
(import "printer" "print_i32" (func $print_i32 (param i32)))
13+
(func (export "print_add") (param $x i32) (param $y i32)
14+
(call $print_i32 (i32.add (local.get $x) (local.get $y)))
15+
)
16+
)
17+
"""
18+
)
19+
)
20+
21+
// Define a host function that prints an i32 value.
22+
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
23+
// This function is called from "print_add" in the WebAssembly module.
24+
print(args[0])
25+
return []
26+
}
27+
// Create a runtime importing the host function.
28+
let runtime = Runtime(hostModules: [
29+
"printer": HostModule(functions: ["print_i32": hostPrint])
30+
])
31+
let instance = try runtime.instantiate(module: module)
32+
// Invoke the exported function "print_add"
33+
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This example demonstrates WASI support in WasmKit.
2+
3+
import WasmKit
4+
import WasmKitWASI
5+
import WAT
6+
import Foundation
7+
8+
// Parse a WASI-compliant WebAssembly module from a file.
9+
let module = try parseWasm(filePath: "wasm/hello.wasm")
10+
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)
16+
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))

Examples/wasm/factorial.wat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
(module
12
(func $fac (export "fac") (param i64) (result i64)
23
(if (result i64) (i64.eqz (local.get 0))
34
(then (i64.const 1))
@@ -9,3 +10,4 @@
910
)
1011
)
1112
)
13+
)

Examples/wasm/fib.wasm

-173 Bytes
Binary file not shown.

Examples/wasm/fib.wat

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)