Coverage for wasmtime/_tag.py: 96%

28 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2026-05-07 14:30 +0000

1import ctypes 

2from . import _ffi as ffi 

3from wasmtime import WasmtimeError 

4from typing import TYPE_CHECKING 

5from ._store import Storelike 

6from ._types import TagType 

7 

8 

9class Tag: 

10 """ 

11 Represents a WebAssembly tag, used to identify exception types. 

12 

13 Tags are associated with a store and describe the payload signature 

14 of exceptions that can be thrown and caught. 

15 """ 

16 

17 _tag: ffi.wasmtime_tag_t 

18 

19 def __init__(self, store: Storelike, ty: TagType) -> None: 

20 """ 

21 Creates a new host-defined tag with the given tag type. 

22 

23 Raises `WasmtimeError` if the tag cannot be created. 

24 """ 

25 if not isinstance(ty, TagType): 

26 raise TypeError("expected a TagType") 

27 tag = ffi.wasmtime_tag_t() 

28 error = ffi.wasmtime_tag_new(store._context(), ty.ptr(), ctypes.byref(tag)) 

29 if error: 

30 raise WasmtimeError._from_ptr(error) 

31 self._tag = tag 

32 

33 @classmethod 

34 def _from_raw(cls, tag: ffi.wasmtime_tag_t) -> "Tag": 

35 obj = cls.__new__(cls) 

36 obj._tag = tag 

37 return obj 

38 

39 def type(self, store: Storelike) -> TagType: 

40 """ 

41 Returns the type of this tag. 

42 """ 

43 ptr = ffi.wasmtime_tag_type(store._context(), ctypes.byref(self._tag)) 

44 return TagType._from_ptr(ptr) 

45 

46 def eq(self, store: Storelike, other: "Tag") -> bool: 

47 """ 

48 Tests whether two tags are identical (same definition). 

49 """ 

50 return bool(ffi.wasmtime_tag_eq(store._context(), 

51 ctypes.byref(self._tag), 

52 ctypes.byref(other._tag))) 

53 

54 def _as_extern(self) -> ffi.wasmtime_extern_t: 

55 union = ffi.wasmtime_extern_union(tag=self._tag) 

56 return ffi.wasmtime_extern_t(ffi.WASMTIME_EXTERN_TAG, union)