Skip to content

Commit

Permalink
FIXES bytecodealliance#137: make calling wasm from python 7x faster
Browse files Browse the repository at this point in the history
  • Loading branch information
muayyad-alsadi committed Apr 4, 2023
1 parent 8bc29e6 commit 37b6067
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = --doctest-modules --flake8 --mypy
addopts = --doctest-modules
2 changes: 1 addition & 1 deletion wasmtime/_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def wasm_valtype_new(arg0: Any) -> ctypes._Pointer:
_wasm_valtype_kind = dll.wasm_valtype_kind
_wasm_valtype_kind.restype = wasm_valkind_t
_wasm_valtype_kind.argtypes = [POINTER(wasm_valtype_t)]
def wasm_valtype_kind(arg0: Any) -> wasm_valkind_t:
def wasm_valtype_kind(arg0: Any) -> int:
return _wasm_valtype_kind(arg0) # type: ignore

class wasm_functype_t(Structure):
Expand Down
6 changes: 3 additions & 3 deletions wasmtime/_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Func:
_params_str: list[str]
_results_str: list[str]
_results_str0: str
_vals_raw_type: ctypes.Array[wasmtime_val_raw_t]
_vals_raw_type: type[ctypes.Array[wasmtime_val_raw_t]]

def __init__(self, store: Storelike, ty: FuncType, func: Callable, access_caller: bool = False):
"""
Expand Down Expand Up @@ -78,7 +78,7 @@ def _extract_return(self, vals_raw: ctypes.Array[wasmtime_val_raw_t]) -> Union[I
# we can use tuple construct, but I'm using list for compatability
return [val_getter(self._func.store_id, val_raw, ret_str) for val_raw, ret_str in zip(vals_raw, self._results_str)]

def _init_call(self, ty: FuncType):
def _init_call(self, ty: FuncType) -> None:
"""init signature properties used by call"""
self._ty = ty
ty_params = ty.params
Expand All @@ -87,7 +87,7 @@ def _init_call(self, ty: FuncType):
results_n = len(ty_results)
self._params_str = [get_valtype_attr(i) for i in ty_params]
self._results_str = [get_valtype_attr(i) for i in ty_results]
self._results_str0 = get_valtype_attr(ty_results[0]) if results_n else None
self._results_str0 = get_valtype_attr(ty_results[0]) if results_n else 'i32'
self._params_n = params_n
self._results_n = results_n
n = max(params_n, results_n)
Expand Down
2 changes: 1 addition & 1 deletion wasmtime/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __str__(self) -> str:
return 'anyref'
if kind == ffi.WASM_FUNCREF.value:
return 'funcref'
return 'ValType(%d)' % kind.value
return 'ValType(%d)' % kind

def __del__(self) -> None:
if not hasattr(self, '_owner') or not hasattr(self, '_ptr'):
Expand Down
18 changes: 13 additions & 5 deletions wasmtime/_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ def val_getter(store_id: int, val_raw: wasmtime_val_raw_t, attr: str) -> typing.
return val


def val_setter(dst: wasmtime_val_raw_t, attr: str, val: "IntoVal"):
def val_setter(dst: wasmtime_val_raw_t, attr: str, val: "IntoVal") -> None:
casted: typing.Union[Any, int, float, None, wasmtime.Func]
if attr == 'externref':
if isinstance(val, Val) and val._raw.kind == WASMTIME_EXTERNREF.value:
if isinstance(val, Val) and val._raw and val._raw.kind == WASMTIME_EXTERNREF.value:
casted = ctypes.addressof(val._raw.of.externref)
else:
casted = ctypes.addressof(Val.externref(val)._raw.of.externref)
ex = Val.externref(val)._raw
if ex:
casted = ctypes.addressof(ex.of.externref)
else:
casted = 0
elif attr == 'funcref':
if isinstance(val, Val) and val._raw.kind == WASMTIME_FUNCREF.value:
if isinstance(val, Val) and val._raw and val._raw.kind == WASMTIME_FUNCREF.value:
casted = val._raw.of.funcref.index
elif isinstance(val, wasmtime.Func):
# TODO: validate same val._func.store_id
Expand All @@ -79,7 +84,10 @@ def val_setter(dst: wasmtime_val_raw_t, attr: str, val: "IntoVal"):
raise RuntimeError("foo")
else:
if isinstance(val, Val):
casted = getattr(val._raw.of, attr)
if val._raw:
casted = getattr(val._raw.of, attr)
else:
casted = 0
else:
casted = val
setattr(dst, attr, casted)
Expand Down

0 comments on commit 37b6067

Please sign in to comment.