Skip to content

global variable initialization isn't allow with throw in swift6 compiler - adds boilerplate main() around the examples showing use of WasmKit #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions Examples/Sources/Factorial/Factorial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import WasmKit
import WAT
import Foundation

// Convert a WAT file to a Wasm binary, then parse it.
let module = try parseWasm(
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat"))
)
@main
struct Example {
static func main() throws {
// Convert a WAT file to a Wasm binary, then parse it.
let module = try parseWasm(
bytes: try wat2wasm(String(contentsOfFile: "wasm/factorial.wat"))
)

// Create a module instance from the parsed module.
let runtime = Runtime()
let instance = try runtime.instantiate(module: module)
let input: UInt64 = 5
// Invoke the exported function "fac" with a single argument.
let result = try runtime.invoke(instance, function: "fac", with: [.i64(input)])
print("fac(\(input)) = \(result[0].i64)")
// Create a module instance from the parsed module.
let runtime = Runtime()
let instance = try runtime.instantiate(module: module)
let input: UInt64 = 5
// Invoke the exported function "fac" with a single argument.
let result = try runtime.invoke(instance, function: "fac", with: [.i64(input)])
print("fac(\(input)) = \(result[0].i64)")
}
}
53 changes: 29 additions & 24 deletions Examples/Sources/PrintAdd/PrintAdd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
import WasmKit
import WAT

// Convert a WAT file to a Wasm binary, then parse it.
let module = try parseWasm(
bytes: try wat2wasm(
"""
(module
(import "printer" "print_i32" (func $print_i32 (param i32)))
(func (export "print_add") (param $x i32) (param $y i32)
(call $print_i32 (i32.add (local.get $x) (local.get $y)))
)
@main
struct Example {
static func main() throws {
// Convert a WAT file to a Wasm binary, then parse it.
let module = try parseWasm(
bytes: try wat2wasm(
"""
(module
(import "printer" "print_i32" (func $print_i32 (param i32)))
(func (export "print_add") (param $x i32) (param $y i32)
(call $print_i32 (i32.add (local.get $x) (local.get $y)))
)
)
"""
)
)
"""
)
)

// Define a host function that prints an i32 value.
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
// This function is called from "print_add" in the WebAssembly module.
print(args[0])
return []
// Define a host function that prints an i32 value.
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
// This function is called from "print_add" in the WebAssembly module.
print(args[0])
return []
}
// Create a runtime importing the host function.
let runtime = Runtime(hostModules: [
"printer": HostModule(functions: ["print_i32": hostPrint])
])
let instance = try runtime.instantiate(module: module)
// Invoke the exported function "print_add"
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])
}
}
// Create a runtime importing the host function.
let runtime = Runtime(hostModules: [
"printer": HostModule(functions: ["print_i32": hostPrint])
])
let instance = try runtime.instantiate(module: module)
// Invoke the exported function "print_add"
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])
27 changes: 16 additions & 11 deletions Examples/Sources/WASI-Hello/Hello.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import WasmKitWASI
import WAT
import Foundation

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

// Create a WASI instance forwarding to the host environment.
let wasi = try WASIBridgeToHost()
// Create a runtime with WASI host modules.
let runtime = Runtime(hostModules: wasi.hostModules)
let instance = try runtime.instantiate(module: module)
// Create a WASI instance forwarding to the host environment.
let wasi = try WASIBridgeToHost()
// Create a runtime with WASI host modules.
let runtime = Runtime(hostModules: wasi.hostModules)
let instance = try runtime.instantiate(module: module)

// Start the WASI command-line application.
let exitCode = try wasi.start(instance, runtime: runtime)
// Exit the Swift program with the WASI exit code.
exit(Int32(exitCode))
// Start the WASI command-line application.
let exitCode = try wasi.start(instance, runtime: runtime)
// Exit the Swift program with the WASI exit code.
exit(Int32(exitCode))
}
}