Skip to content

Commit d383a05

Browse files
FIXES #137: make calling wasm from python 7x faster
1 parent d0913e8 commit d383a05

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

wasmtime/_func.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
WASMTIME_V128,
1717
WASMTIME_FUNCREF,
1818
WASMTIME_EXTERNREF,
19+
WASM_ANYREF,
20+
WASM_FUNCREF,
1921
)
2022

2123

@@ -31,11 +33,26 @@
3133
WASMTIME_V128.value: 'v128',
3234
WASMTIME_FUNCREF.value: 'funcref',
3335
WASMTIME_EXTERNREF.value: 'externref',
36+
WASM_FUNCREF.value: 'funcref',
37+
WASM_ANYREF.value: 'externref',
3438
}
3539

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

43+
def val_setter(dst, attr, val):
44+
if attr=='externref':
45+
# TODO: handle None
46+
v = Val.externref(val)
47+
casted = ctypes.addressof(v._raw.of.externref)
48+
elif isinstance(val, Func):
49+
# TODO: handle null_funcref
50+
# TODO: validate same val._func.store_id
51+
casted = val._func.index
52+
else:
53+
casted = val
54+
setattr(dst, attr, casted)
55+
3956
class Func:
4057
_func: ffi.wasmtime_func_t
4158
_ty: FuncType
@@ -88,7 +105,7 @@ def type(self, store: Storelike) -> FuncType:
88105
def _create_raw_vals(self, *params: IntoVal) -> ctypes.Array[wasmtime_val_raw_t]:
89106
raw = self._vals_raw_type()
90107
for i, param_str in enumerate(self._params_str):
91-
setattr(raw[i], param_str, params[i])
108+
val_setter(raw[i], param_str, params[i])
92109
return raw
93110

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

0 commit comments

Comments
 (0)