From 4440fe9eb94b9e78555448c862f35c34c4d83615 Mon Sep 17 00:00:00 2001 From: Juan Gomez Mosquera Date: Wed, 23 Oct 2024 21:13:30 -0400 Subject: [PATCH] Making SharedMemory inherit from the Manager type --- wasmtime/_extern.py | 2 +- wasmtime/_sharedmemory.py | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/wasmtime/_extern.py b/wasmtime/_extern.py index c3f526f..f11b47f 100644 --- a/wasmtime/_extern.py +++ b/wasmtime/_extern.py @@ -16,7 +16,7 @@ def wrap_extern(ptr: ffi.wasmtime_extern_t) -> AsExtern: if ptr.kind == ffi.WASMTIME_EXTERN_MEMORY.value: return Memory._from_raw(ptr.of.memory) if ptr.kind == ffi.WASMTIME_EXTERN_SHAREDMEMORY.value: - return Memory._from_raw(ptr.of.sharedmemory) + return SharedMemory._from_raw(ptr.of.sharedmemory) if ptr.kind == ffi.WASMTIME_EXTERN_INSTANCE.value: return Instance._from_raw(ptr.of.instance) if ptr.kind == ffi.WASMTIME_EXTERN_MODULE.value: diff --git a/wasmtime/_sharedmemory.py b/wasmtime/_sharedmemory.py index d86c2fe..f04b772 100644 --- a/wasmtime/_sharedmemory.py +++ b/wasmtime/_sharedmemory.py @@ -1,15 +1,12 @@ from . import _ffi as ffi from ctypes import * import ctypes -import typing -from wasmtime import MemoryType, WasmtimeError, Engine +from wasmtime import MemoryType, WasmtimeError, Engine, Managed from ._store import Storelike -class SharedMemory: - _sharedmemory: ffi.wasmtime_sharedmemory_t - +class SharedMemory(Managed["ctypes._Pointer[ffi.wasmtime_sharedmemory_t]"]): def __init__(self, engine: Engine, ty: MemoryType): """ Creates a new shared memory in `store` with the given `ty` @@ -20,12 +17,12 @@ def __init__(self, engine: Engine, ty: MemoryType): error = ffi.wasmtime_sharedmemory_new(engine.ptr(), ty.ptr(), byref(ptr)) if error: raise WasmtimeError._from_ptr(error) - self._sharedmemory = sharedmemory + self._set_ptr(sharedmemory) @classmethod def _from_raw(cls, sharedmemory: ffi.wasmtime_sharedmemory_t) -> "SharedMemory": ty: "SharedMemory" = cls.__new__(cls) - ty._sharedmemory = sharedmemory + ty._set_ptr(sharedmemory) return ty def type(self) -> MemoryType: @@ -44,7 +41,7 @@ def grow(self, delta: int) -> int: if delta < 0: raise WasmtimeError("cannot grow by negative amount") prev = ffi.c_uint64(0) - error = ffi.wasmtime_sharedmemory_grow(byref(self._sharedmemory), delta, byref(prev)) + error = ffi.wasmtime_sharedmemory_grow(byref(self.ptr()), delta, byref(prev)) if error: raise WasmtimeError._from_ptr(error) return prev.value @@ -54,7 +51,7 @@ def size(self) -> int: Returns the size, in WebAssembly pages, of this shared memory. """ - return ffi.wasmtime_sharedmemory_size(byref(self._sharedmemory)) + return ffi.wasmtime_sharedmemory_size(byref(self.ptr())) def data_ptr(self) -> "ctypes._Pointer[c_ubyte]": """ @@ -63,15 +60,15 @@ def data_ptr(self) -> "ctypes._Pointer[c_ubyte]": Remember that all accesses to wasm shared memory should be bounds-checked against the `data_len` method. """ - return ffi.wasmtime_sharedmemory_data(byref(self._sharedmemory)) + return ffi.wasmtime_sharedmemory_data(byref(self.ptr())) def data_len(self) -> int: """ Returns the raw byte length of this memory. """ - return ffi.wasmtime_sharedmemory_data_size(byref(self._sharedmemory)) + return ffi.wasmtime_sharedmemory_data_size(byref(self.ptr())) def _as_extern(self) -> ffi.wasmtime_extern_t: - union = ffi.wasmtime_extern_union(sharedmemory=pointer(self._sharedmemory)) + union = ffi.wasmtime_extern_union(sharedmemory=pointer(self.ptr())) return ffi.wasmtime_extern_t(ffi.WASMTIME_EXTERN_SHAREDMEMORY, union)