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 Mar 28, 2023
1 parent d0913e8 commit d383a05
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion wasmtime/_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
WASMTIME_V128,
WASMTIME_FUNCREF,
WASMTIME_EXTERNREF,
WASM_ANYREF,
WASM_FUNCREF,
)


Expand All @@ -31,11 +33,26 @@
WASMTIME_V128.value: 'v128',
WASMTIME_FUNCREF.value: 'funcref',
WASMTIME_EXTERNREF.value: 'externref',
WASM_FUNCREF.value: 'funcref',
WASM_ANYREF.value: 'externref',
}

def get_valtype_attr(ty: ValType):
return val_id2attr[wasm_valtype_kind(ty._ptr)]

def val_setter(dst, attr, val):
if attr=='externref':
# TODO: handle None
v = Val.externref(val)
casted = ctypes.addressof(v._raw.of.externref)
elif isinstance(val, Func):
# TODO: handle null_funcref
# TODO: validate same val._func.store_id
casted = val._func.index
else:
casted = val
setattr(dst, attr, casted)

class Func:
_func: ffi.wasmtime_func_t
_ty: FuncType
Expand Down Expand Up @@ -88,7 +105,7 @@ def type(self, store: Storelike) -> FuncType:
def _create_raw_vals(self, *params: IntoVal) -> ctypes.Array[wasmtime_val_raw_t]:
raw = self._vals_raw_type()
for i, param_str in enumerate(self._params_str):
setattr(raw[i], param_str, params[i])
val_setter(raw[i], param_str, params[i])
return raw

def _extract_return(self, vals_raw: ctypes.Array[wasmtime_val_raw_t]) -> Union[IntoVal, Sequence[IntoVal], None]:
Expand Down

0 comments on commit d383a05

Please sign in to comment.