Coverage for wasmtime/component/_resources.py: 96%
69 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
1from .. import _ffi as ffi, WasmtimeError, Managed, Storelike, WasmtimeError
2from ctypes import POINTER, byref
3import ctypes
4from ._resource_type import ResourceType
5from ._enter import enter_wasm
8class ResourceAny(Managed["ctypes._Pointer[ffi.wasmtime_component_resource_any_t]"]):
9 def __init__(self) -> None:
10 raise WasmtimeError("Cannot directly construct a `ResourceAny`")
12 def _delete(self, ptr: "ctypes._Pointer[ffi.wasmtime_component_resource_any_t]") -> None:
13 ffi.wasmtime_component_resource_any_delete(ptr)
15 @classmethod
16 def _from_ptr(cls, ptr: "ctypes._Pointer[ffi.wasmtime_component_resource_any_t]") -> "ResourceAny":
17 if not isinstance(ptr, POINTER(ffi.wasmtime_component_resource_any_t)):
18 raise TypeError("wrong pointer type")
19 ty: "ResourceAny" = cls.__new__(cls)
20 ty._set_ptr(ptr)
21 return ty
23 @property
24 def type(self) -> ResourceType:
25 """
26 Returns the `ResourceType` of this `ResourceAny`.
27 """
28 ptr = ffi.wasmtime_component_resource_any_type(self.ptr())
29 return ResourceType._from_ptr(ptr)
31 @property
32 def owned(self) -> bool:
33 """
34 Returns whether this `ResourceAny` is an `own` resource or a `borrow`
35 resource.
36 """
37 return ffi.wasmtime_component_resource_any_owned(self.ptr())
39 def drop(self, store: Storelike) -> None:
40 """
41 Runs the WebAssembly-defined destructor, if any, for this resource.
43 This is required to be called to clean up state information in the
44 store about this resource. This may execute WebAssembly code if the
45 resource is a guest-owned resource with a defined destructor.
46 """
47 def run() -> 'ctypes._Pointer[ffi.wasmtime_error_t]':
48 return ffi.wasmtime_component_resource_any_drop(store._context(), self.ptr())
49 enter_wasm(run)
51 def to_host(self, store: Storelike) -> "ResourceHost":
52 """
53 Attempts to downcast this `ResourceAny` to a `ResourceHost`.
55 This will raise a `WasmtimeError` if this `ResourceAny` is not
56 actually a host-defined resource.
57 """
58 ptr = POINTER(ffi.wasmtime_component_resource_host_t)()
59 error = ffi.wasmtime_component_resource_any_to_host(store._context(), self.ptr(), byref(ptr))
60 if error:
61 raise WasmtimeError._from_ptr(error)
62 return ResourceHost._from_ptr(ptr)
65class ResourceHost(Managed["ctypes._Pointer[ffi.wasmtime_component_resource_host_t]"]):
66 def __init__(self) -> None:
67 raise WasmtimeError("Cannot directly construct a `ResourceHost`")
69 def _delete(self, ptr: "ctypes._Pointer[ffi.wasmtime_component_resource_host_t]") -> None:
70 ffi.wasmtime_component_resource_host_delete(ptr)
72 @classmethod
73 def _from_ptr(cls, ptr: "ctypes._Pointer[ffi.wasmtime_component_resource_host_t]") -> "ResourceHost":
74 if not isinstance(ptr, POINTER(ffi.wasmtime_component_resource_host_t)):
75 raise TypeError("wrong pointer type")
76 ty: "ResourceHost" = cls.__new__(cls)
77 ty._set_ptr(ptr)
78 return ty
80 @classmethod
81 def own(cls, rep: int, ty: int) -> "ResourceHost":
82 ptr = ffi.wasmtime_component_resource_host_new(True, rep, ty)
83 return cls._from_ptr(ptr)
85 @classmethod
86 def borrow(cls, rep: int, ty: int) -> "ResourceHost":
87 ptr = ffi.wasmtime_component_resource_host_new(False, rep, ty)
88 return cls._from_ptr(ptr)
90 @property
91 def rep(self) -> int:
92 """
93 Returns the integer representation associated with this host resource.
94 """
95 return ffi.wasmtime_component_resource_host_rep(self.ptr())
97 @property
98 def type(self) -> int:
99 """
100 Returns the integer type identifier associated with this host resource.
101 """
102 return ffi.wasmtime_component_resource_host_type(self.ptr())
104 @property
105 def owned(self) -> bool:
106 """
107 Returns whether this `ResourceHost` is an `own` resource or a `borrow`
108 resource.
109 """
110 return ffi.wasmtime_component_resource_host_owned(self.ptr())
112 def to_any(self, store: Storelike) -> ResourceAny:
113 """
114 Upcasts this `ResourceHost` to a `ResourceAny`.
115 """
116 ptr = POINTER(ffi.wasmtime_component_resource_any_t)()
117 error = ffi.wasmtime_component_resource_host_to_any(store._context(), self.ptr(), byref(ptr))
118 if error:
119 raise WasmtimeError._from_ptr(error)
120 return ResourceAny._from_ptr(ptr)