File: Docs.md

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (126 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download
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``