Skip to content

Commit

Permalink
Making SharedMemory inherit from the Manager type
Browse files Browse the repository at this point in the history
  • Loading branch information
atilag committed Oct 24, 2024
1 parent afc98bc commit 4440fe9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion wasmtime/_extern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 9 additions & 12 deletions wasmtime/_sharedmemory.py
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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]":
"""
Expand All @@ -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)

0 comments on commit 4440fe9

Please sign in to comment.