0
# WebAssembly Types
1
2
Complete type system mapping WebAssembly value types, function signatures, memory layouts, table definitions, and import/export declarations to Python. Provides comprehensive type validation, conversion utilities, and metadata access for WebAssembly components.
3
4
## Capabilities
5
6
### Value Types
7
8
WebAssembly value type system representing the fundamental data types supported by WebAssembly virtual machine, including numeric types, vector types, and reference types.
9
10
```python { .api }
11
class ValType:
12
# Numeric types
13
I32: 'ValType' # 32-bit integer
14
I64: 'ValType' # 64-bit integer
15
F32: 'ValType' # 32-bit floating point
16
F64: 'ValType' # 64-bit floating point
17
18
# Vector type
19
V128: 'ValType' # 128-bit SIMD vector
20
21
# Reference types
22
FUNCREF: 'ValType' # Function reference
23
EXTERNREF: 'ValType' # External reference
24
25
def __eq__(self, other) -> bool: ...
26
def __str__(self) -> str: ...
27
```
28
29
### Value Wrappers
30
31
WebAssembly value wrapper providing type-safe conversion between Python values and WebAssembly runtime values, with automatic marshalling and unmarshalling support.
32
33
```python { .api }
34
class Val:
35
@staticmethod
36
def i32(val: int) -> 'Val':
37
"""
38
Create a 32-bit integer value.
39
40
Parameters:
41
- val: Python integer (must fit in 32-bit signed range)
42
43
Returns:
44
WebAssembly i32 value
45
"""
46
47
@staticmethod
48
def i64(val: int) -> 'Val':
49
"""
50
Create a 64-bit integer value.
51
52
Parameters:
53
- val: Python integer (must fit in 64-bit signed range)
54
55
Returns:
56
WebAssembly i64 value
57
"""
58
59
@staticmethod
60
def f32(val: float) -> 'Val':
61
"""
62
Create a 32-bit floating point value.
63
64
Parameters:
65
- val: Python float
66
67
Returns:
68
WebAssembly f32 value
69
"""
70
71
@staticmethod
72
def f64(val: float) -> 'Val':
73
"""
74
Create a 64-bit floating point value.
75
76
Parameters:
77
- val: Python float
78
79
Returns:
80
WebAssembly f64 value
81
"""
82
83
@staticmethod
84
def funcref(val) -> 'Val':
85
"""
86
Create a function reference value.
87
88
Parameters:
89
- val: Function object or None
90
91
Returns:
92
WebAssembly funcref value
93
"""
94
95
@staticmethod
96
def externref(val) -> 'Val':
97
"""
98
Create an external reference value.
99
100
Parameters:
101
- val: Any Python object or None
102
103
Returns:
104
WebAssembly externref value
105
"""
106
107
@property
108
def value(self) -> Union[int, float, None]:
109
"""
110
Get the raw Python value.
111
112
Returns:
113
The underlying Python value
114
"""
115
116
@property
117
def type(self) -> ValType:
118
"""
119
Get the WebAssembly value type.
120
121
Returns:
122
The ValType of this value
123
"""
124
```
125
126
### Function Types
127
128
Function signature type defining parameter and result types for WebAssembly functions, enabling type checking and validation during function calls and imports.
129
130
```python { .api }
131
class FuncType:
132
def __init__(self, params: List[ValType], results: List[ValType]):
133
"""
134
Create a function type with parameter and result types.
135
136
Parameters:
137
- params: List of parameter value types
138
- results: List of result value types
139
"""
140
141
@property
142
def params(self) -> List[ValType]:
143
"""
144
Get the parameter types.
145
146
Returns:
147
List of parameter value types
148
"""
149
150
@property
151
def results(self) -> List[ValType]:
152
"""
153
Get the result types.
154
155
Returns:
156
List of result value types
157
"""
158
159
def __eq__(self, other) -> bool: ...
160
def __str__(self) -> str: ...
161
```
162
163
### Memory Types
164
165
Memory layout specification defining size limits and growth constraints for WebAssembly linear memory, supporting both bounded and unbounded memory configurations.
166
167
```python { .api }
168
class MemoryType:
169
def __init__(self, limits: Limits):
170
"""
171
Create a memory type with size limits.
172
173
Parameters:
174
- limits: Memory size limits specification
175
"""
176
177
@property
178
def limits(self) -> Limits:
179
"""
180
Get the memory size limits.
181
182
Returns:
183
Memory size limits
184
"""
185
186
def __eq__(self, other) -> bool: ...
187
def __str__(self) -> str: ...
188
```
189
190
### Table Types
191
192
Table type specification defining element type and size constraints for WebAssembly tables, supporting function references and external references.
193
194
```python { .api }
195
class TableType:
196
def __init__(self, element: ValType, limits: Limits):
197
"""
198
Create a table type with element type and size limits.
199
200
Parameters:
201
- element: Type of elements stored in the table
202
- limits: Table size limits specification
203
"""
204
205
@property
206
def element(self) -> ValType:
207
"""
208
Get the table element type.
209
210
Returns:
211
Element value type
212
"""
213
214
@property
215
def limits(self) -> Limits:
216
"""
217
Get the table size limits.
218
219
Returns:
220
Table size limits
221
"""
222
223
def __eq__(self, other) -> bool: ...
224
def __str__(self) -> str: ...
225
```
226
227
### Global Types
228
229
Global variable type specification defining value type and mutability constraints for WebAssembly global variables, supporting both mutable and immutable globals.
230
231
```python { .api }
232
class GlobalType:
233
def __init__(self, content: ValType, mutability: bool):
234
"""
235
Create a global type with value type and mutability.
236
237
Parameters:
238
- content: Value type of the global variable
239
- mutability: Whether the global is mutable
240
"""
241
242
@property
243
def content(self) -> ValType:
244
"""
245
Get the global value type.
246
247
Returns:
248
Global variable value type
249
"""
250
251
@property
252
def mutability(self) -> bool:
253
"""
254
Get the global mutability.
255
256
Returns:
257
True if mutable, False if immutable
258
"""
259
260
def __eq__(self, other) -> bool: ...
261
def __str__(self) -> str: ...
262
```
263
264
### Size Limits
265
266
Size constraint specification for memory and table types, defining minimum size requirements and optional maximum size limits with growth boundaries.
267
268
```python { .api }
269
class Limits:
270
def __init__(self, min: int, max: Optional[int] = None):
271
"""
272
Create size limits with minimum and optional maximum.
273
274
Parameters:
275
- min: Minimum size (in pages for memory, elements for tables)
276
- max: Optional maximum size, None for unbounded
277
"""
278
279
@property
280
def min(self) -> int:
281
"""
282
Get the minimum size.
283
284
Returns:
285
Minimum size constraint
286
"""
287
288
@property
289
def max(self) -> Optional[int]:
290
"""
291
Get the maximum size.
292
293
Returns:
294
Maximum size constraint, or None if unbounded
295
"""
296
297
def __eq__(self, other) -> bool: ...
298
def __str__(self) -> str: ...
299
```
300
301
### Import/Export Types
302
303
Type declarations for WebAssembly module imports and exports, providing metadata about module interface requirements and capabilities for linking and instantiation.
304
305
```python { .api }
306
class ImportType:
307
@property
308
def module(self) -> str:
309
"""
310
Get the import module name.
311
312
Returns:
313
Module name string
314
"""
315
316
@property
317
def name(self) -> str:
318
"""
319
Get the import item name.
320
321
Returns:
322
Import name string
323
"""
324
325
@property
326
def type(self) -> Union[FuncType, MemoryType, TableType, GlobalType]:
327
"""
328
Get the import type.
329
330
Returns:
331
The type of the imported item
332
"""
333
334
def __str__(self) -> str: ...
335
336
class ExportType:
337
@property
338
def name(self) -> str:
339
"""
340
Get the export name.
341
342
Returns:
343
Export name string
344
"""
345
346
@property
347
def type(self) -> Union[FuncType, MemoryType, TableType, GlobalType]:
348
"""
349
Get the export type.
350
351
Returns:
352
The type of the exported item
353
"""
354
355
def __str__(self) -> str: ...
356
```
357
358
## Usage Examples
359
360
### Working with Value Types
361
362
```python
363
import wasmtime
364
365
# Create different value types
366
i32_val = wasmtime.Val.i32(42)
367
i64_val = wasmtime.Val.i64(1234567890123)
368
f32_val = wasmtime.Val.f32(3.14159)
369
f64_val = wasmtime.Val.f64(2.71828182845904)
370
371
# Check types and values
372
print(f"i32 value: {i32_val.value}, type: {i32_val.type}")
373
print(f"f64 value: {f64_val.value}, type: {f64_val.type}")
374
375
# Create reference values
376
func_ref = wasmtime.Val.funcref(None) # null function reference
377
extern_ref = wasmtime.Val.externref({"key": "value"}) # Python object reference
378
```
379
380
### Function Type Definition
381
382
```python
383
import wasmtime
384
385
# Define function type: (i32, i32) -> i32
386
add_type = wasmtime.FuncType(
387
[wasmtime.ValType.I32, wasmtime.ValType.I32],
388
[wasmtime.ValType.I32]
389
)
390
391
# Define function type: (f64) -> (f64, i32)
392
multi_result_type = wasmtime.FuncType(
393
[wasmtime.ValType.F64],
394
[wasmtime.ValType.F64, wasmtime.ValType.I32]
395
)
396
397
# Inspect function types
398
print(f"Add function params: {add_type.params}")
399
print(f"Add function results: {add_type.results}")
400
print(f"Multi-result params: {len(multi_result_type.params)}")
401
print(f"Multi-result results: {len(multi_result_type.results)}")
402
```
403
404
### Memory and Table Type Specification
405
406
```python
407
import wasmtime
408
409
# Create memory type: 1-10 pages (64KB - 640KB)
410
memory_limits = wasmtime.Limits(1, 10)
411
memory_type = wasmtime.MemoryType(memory_limits)
412
413
# Create unbounded memory type: minimum 2 pages
414
unbounded_limits = wasmtime.Limits(2) # No maximum
415
unbounded_memory_type = wasmtime.MemoryType(unbounded_limits)
416
417
# Create table type for function references: 0-100 elements
418
table_limits = wasmtime.Limits(0, 100)
419
table_type = wasmtime.TableType(wasmtime.ValType.FUNCREF, table_limits)
420
421
# Create table type for external references: minimum 5 elements
422
extern_table_limits = wasmtime.Limits(5)
423
extern_table_type = wasmtime.TableType(wasmtime.ValType.EXTERNREF, extern_table_limits)
424
425
print(f"Memory limits: {memory_type.limits.min}-{memory_type.limits.max} pages")
426
print(f"Table element type: {table_type.element}")
427
```
428
429
### Global Variable Types
430
431
```python
432
import wasmtime
433
434
# Create mutable i32 global type
435
mutable_global_type = wasmtime.GlobalType(wasmtime.ValType.I32, True)
436
437
# Create immutable f64 global type
438
immutable_global_type = wasmtime.GlobalType(wasmtime.ValType.F64, False)
439
440
print(f"Mutable global: {mutable_global_type.content}, mutable: {mutable_global_type.mutability}")
441
print(f"Immutable global: {immutable_global_type.content}, mutable: {immutable_global_type.mutability}")
442
```
443
444
### Module Interface Inspection
445
446
```python
447
import wasmtime
448
449
engine = wasmtime.Engine()
450
module = wasmtime.Module(engine, wasm_bytes)
451
452
# Inspect imports
453
print("Module imports:")
454
for import_item in module.imports:
455
print(f" {import_item.module}.{import_item.name}: {import_item.type}")
456
457
# Inspect exports
458
print("Module exports:")
459
for export_item in module.exports:
460
print(f" {export_item.name}: {export_item.type}")
461
462
# Check export type
463
if isinstance(export_item.type, wasmtime.FuncType):
464
func_type = export_item.type
465
print(f" Function: {len(func_type.params)} params, {len(func_type.results)} results")
466
elif isinstance(export_item.type, wasmtime.MemoryType):
467
mem_type = export_item.type
468
print(f" Memory: {mem_type.limits.min}-{mem_type.limits.max} pages")
469
```