1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
# ``WasmKit``
A WebAssembly runtime written in Swift.
## Overview
**WasmKit** is a standalone and embeddable WebAssembly runtime implementation written in Swift.
## Quick start
This example shows how to instantiate a WebAssembly module, interact with the host process, and invoke a WebAssembly function.
```swift
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)))
)
)
"""
)
)
// Create engine and store
let engine = Engine()
let store = Store(engine: engine)
// Instantiate a parsed module with importing a host function
let instance = try module.instantiate(
store: store,
// Import a host function that prints an i32 value.
imports: [
"printer": [
"print_i32": Function(store: store, parameters: [.i32]) { _, args in
// This function is called from "print_add" in the WebAssembly module.
print(args[0])
return []
}
]
]
)
// Invoke the exported function "print_add"
let printAdd = instance.exports[function: "print_add"]!
try printAdd([.i32(42), .i32(3)])
```
See [examples](https://github.com/swiftwasm/WasmKit/tree/main/Examples) for executable examples.
## WASI Example
This example shows how to run WASI application on WasmKit.
```swift
import WasmKit
import WasmKitWASI
import WAT
import Foundation
// 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 engine and store
let engine = Engine()
let store = Store(engine: engine)
// Instantiate a parsed module importing WASI
var imports = Imports()
wasi.link(to: &imports, store: store)
let instance = try module.instantiate(store: store, imports: imports)
// Start the WASI command-line application.
let exitCode = try wasi.start(instance)
// Exit the Swift program with the WASI exit code.
exit(Int32(exitCode))
```
See [examples](https://github.com/swiftwasm/WasmKit/tree/main/Examples) for executable examples.
## Topics
### Basic Concepts
- ``Engine``
- ``Store``
- ``Module``
- ``Instance``
- ``Function``
### Binary Parser
- ``parseWasm(bytes:features:)``
- ``parseWasm(filePath:features:)``
### Other WebAssembly Entities
- ``Global``
- ``Memory``
- ``Table``
### Extending Runtime
- ``Imports``
- ``Caller``
- ``GuestMemory``
- ``UnsafeGuestPointer``
- ``UnsafeGuestRawPointer``
- ``UnsafeGuestBufferPointer``
- ``GuestPointee``
- ``GuestPrimitivePointee``
### Component Model
- ``CanonicalLifting``
- ``CanonicalLowering``
- ``CanonicalOptions``
- ``CanonicalCallContext``
- ``ComponentError``
|