From 0a6da34c9d43789ef1f8bcc466df3e1b018706ba Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 14 Jan 2021 09:05:43 -0800 Subject: [PATCH] Update for recent C API changes Updates this binding for bytecodealliance/wasmtime#2579 --- bindgen.py | 5 ++++- wasmtime/_bindings.py | 24 +++++++++++----------- wasmtime/_func.py | 48 +++++++++++++++++++++---------------------- wasmtime/_instance.py | 4 ++-- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/bindgen.py b/bindgen.py index 804a8d2f..65d45234 100644 --- a/bindgen.py +++ b/bindgen.py @@ -93,9 +93,12 @@ def visit_FuncDecl(self, node): argname = param.name if not argname or argname == "import" or argname == "global": argname = "arg{}".format(i) + tyname = type_name(param.type) + if i == 0 and tyname == "None": + continue argpairs.append("{}: Any".format(argname)) argnames.append(argname) - argtypes.append(type_name(param.type)) + argtypes.append(tyname) retty = type_name(node.type, ptr, typing=True) self.ret += "\n" diff --git a/wasmtime/_bindings.py b/wasmtime/_bindings.py index a39e3b67..e464652a 100644 --- a/wasmtime/_bindings.py +++ b/wasmtime/_bindings.py @@ -1114,9 +1114,9 @@ def wasm_func_as_ref(arg0: Any) -> pointer: def wasm_func_as_ref_const(arg0: Any) -> pointer: return _wasm_func_as_ref_const(arg0) # type: ignore -wasm_func_callback_t = CFUNCTYPE(c_size_t, POINTER(wasm_val_t), POINTER(wasm_val_t)) +wasm_func_callback_t = CFUNCTYPE(c_size_t, POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t)) -wasm_func_callback_with_env_t = CFUNCTYPE(c_size_t, c_void_p, POINTER(wasm_val_t), POINTER(wasm_val_t)) +wasm_func_callback_with_env_t = CFUNCTYPE(c_size_t, c_void_p, POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t)) _wasm_func_new = dll.wasm_func_new _wasm_func_new.restype = POINTER(wasm_func_t) @@ -1150,7 +1150,7 @@ def wasm_func_result_arity(arg0: Any) -> int: _wasm_func_call = dll.wasm_func_call _wasm_func_call.restype = POINTER(wasm_trap_t) -_wasm_func_call.argtypes = [POINTER(wasm_func_t), POINTER(wasm_val_t), POINTER(wasm_val_t)] +_wasm_func_call.argtypes = [POINTER(wasm_func_t), POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t)] def wasm_func_call(arg0: Any, args: Any, results: Any) -> pointer: return _wasm_func_call(arg0, args, results) # type: ignore @@ -1631,7 +1631,7 @@ def wasm_instance_as_ref_const(arg0: Any) -> pointer: _wasm_instance_new = dll.wasm_instance_new _wasm_instance_new.restype = POINTER(wasm_instance_t) -_wasm_instance_new.argtypes = [POINTER(wasm_store_t), POINTER(wasm_module_t), POINTER(POINTER(wasm_extern_t)), POINTER(POINTER(wasm_trap_t))] +_wasm_instance_new.argtypes = [POINTER(wasm_store_t), POINTER(wasm_module_t), POINTER(wasm_extern_vec_t), POINTER(POINTER(wasm_trap_t))] def wasm_instance_new(arg0: Any, arg1: Any, imports: Any, arg3: Any) -> pointer: return _wasm_instance_new(arg0, arg1, imports, arg3) # type: ignore @@ -1944,9 +1944,9 @@ def wasmtime_linker_get_one_by_name(linker: Any, module: Any, name: Any, item: A class wasmtime_caller_t(Structure): pass -wasmtime_func_callback_t = CFUNCTYPE(c_size_t, POINTER(wasmtime_caller_t), POINTER(wasm_val_t), POINTER(wasm_val_t)) +wasmtime_func_callback_t = CFUNCTYPE(c_size_t, POINTER(wasmtime_caller_t), POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t)) -wasmtime_func_callback_with_env_t = CFUNCTYPE(c_size_t, POINTER(wasmtime_caller_t), c_void_p, POINTER(wasm_val_t), POINTER(wasm_val_t)) +wasmtime_func_callback_with_env_t = CFUNCTYPE(c_size_t, POINTER(wasmtime_caller_t), c_void_p, POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t)) _wasmtime_func_new = dll.wasmtime_func_new _wasmtime_func_new.restype = POINTER(wasm_func_t) @@ -2019,9 +2019,9 @@ def wasmtime_frame_module_name(arg0: Any) -> pointer: _wasmtime_func_call = dll.wasmtime_func_call _wasmtime_func_call.restype = POINTER(wasmtime_error_t) -_wasmtime_func_call.argtypes = [POINTER(wasm_func_t), POINTER(wasm_val_t), c_size_t, POINTER(wasm_val_t), c_size_t, POINTER(POINTER(wasm_trap_t))] -def wasmtime_func_call(func: Any, args: Any, num_args: Any, results: Any, num_results: Any, trap: Any) -> pointer: - return _wasmtime_func_call(func, args, num_args, results, num_results, trap) # type: ignore +_wasmtime_func_call.argtypes = [POINTER(wasm_func_t), POINTER(wasm_val_vec_t), POINTER(wasm_val_vec_t), POINTER(POINTER(wasm_trap_t))] +def wasmtime_func_call(func: Any, args: Any, results: Any, trap: Any) -> pointer: + return _wasmtime_func_call(func, args, results, trap) # type: ignore _wasmtime_global_new = dll.wasmtime_global_new _wasmtime_global_new.restype = POINTER(wasmtime_error_t) @@ -2037,9 +2037,9 @@ def wasmtime_global_set(arg0: Any, val: Any) -> pointer: _wasmtime_instance_new = dll.wasmtime_instance_new _wasmtime_instance_new.restype = POINTER(wasmtime_error_t) -_wasmtime_instance_new.argtypes = [POINTER(wasm_store_t), POINTER(wasm_module_t), POINTER(POINTER(wasm_extern_t)), c_size_t, POINTER(POINTER(wasm_instance_t)), POINTER(POINTER(wasm_trap_t))] -def wasmtime_instance_new(store: Any, module: Any, imports: Any, num_imports: Any, instance: Any, trap: Any) -> pointer: - return _wasmtime_instance_new(store, module, imports, num_imports, instance, trap) # type: ignore +_wasmtime_instance_new.argtypes = [POINTER(wasm_store_t), POINTER(wasm_module_t), POINTER(wasm_extern_vec_t), POINTER(POINTER(wasm_instance_t)), POINTER(POINTER(wasm_trap_t))] +def wasmtime_instance_new(store: Any, module: Any, imports: Any, instance: Any, trap: Any) -> pointer: + return _wasmtime_instance_new(store, module, imports, instance, trap) # type: ignore _wasmtime_module_new = dll.wasmtime_module_new _wasmtime_module_new.restype = POINTER(wasmtime_error_t) diff --git a/wasmtime/_func.py b/wasmtime/_func.py index 9947a7a9..3e5d149b 100644 --- a/wasmtime/_func.py +++ b/wasmtime/_func.py @@ -26,7 +26,7 @@ def __init__(self, store: Store, ty: FuncType, func: Callable, access_caller: bo raise TypeError("expected a Store") if not isinstance(ty, FuncType): raise TypeError("expected a FuncType") - idx = FUNCTIONS.allocate((func, ty.params, ty.results, store)) + idx = FUNCTIONS.allocate((func, ty.results, store)) if access_caller: ptr = ffi.wasmtime_func_new_with_env( store._ptr, @@ -102,17 +102,17 @@ def __call__(self, *params: IntoVal) -> Union[IntoVal, Sequence[IntoVal], None]: params_ptr = (ffi.wasm_val_t * len(params))() for i, val in enumerate(param_vals): params_ptr[i] = val._unwrap_raw() + params_arg = ffi.wasm_val_vec_t(len(params), params_ptr) result_tys = ty.results results_ptr = (ffi.wasm_val_t * len(result_tys))() + results_arg = ffi.wasm_val_vec_t(len(result_tys), results_ptr) trap = POINTER(ffi.wasm_trap_t)() error = ffi.wasmtime_func_call( self._ptr, - params_ptr, - len(params), - results_ptr, - len(result_tys), + byref(params_arg), + byref(results_arg), byref(trap)) if error: raise WasmtimeError._from_ptr(error) @@ -188,49 +188,49 @@ def extract_val(val: Val) -> IntoVal: @ffi.wasm_func_callback_with_env_t # type: ignore -def trampoline(idx, params_ptr, results_ptr): # type: ignore - return invoke(idx, params_ptr, results_ptr, []) +def trampoline(idx, params, results): # type: ignore + return invoke(idx, params.contents, results.contents, []) @ffi.wasmtime_func_callback_with_env_t # type: ignore -def trampoline_with_caller(caller, idx, params_ptr, results_ptr): # type: ignore +def trampoline_with_caller(caller, idx, params, results): # type: ignore caller = Caller(caller) try: - return invoke(idx, params_ptr, results_ptr, [caller]) + return invoke(idx, params.contents, results.contents, [caller]) finally: delattr(caller, '_ptr') -def invoke(idx, params_ptr, results_ptr, params): # type: ignore - func, param_tys, result_tys, store = FUNCTIONS.get(idx or 0) +def invoke(idx, params, results, pyparams): # type: ignore + func, result_tys, store = FUNCTIONS.get(idx or 0) try: - for i in range(0, len(param_tys)): - params.append(Val._value(params_ptr[i])) - results = func(*params) - if len(result_tys) == 0: - if results is not None: + for i in range(0, params.size): + pyparams.append(Val._value(params.data[i])) + pyresults = func(*pyparams) + if results.size == 0: + if pyresults is not None: raise WasmtimeError( "callback produced results when it shouldn't") - elif len(result_tys) == 1: - if isinstance(results, Val): + elif results.size == 1: + if isinstance(pyresults, Val): # Because we are taking the inner value with `_into_raw`, we # need to ensure that we have a unique `Val`. - val = results._clone() + val = pyresults._clone() else: - val = Val._convert(result_tys[0], results) - results_ptr[0] = val._into_raw() + val = Val._convert(result_tys[0], pyresults) + results.data[0] = val._into_raw() else: - if len(results) != len(result_tys): + if len(pyresults) != results.size: raise WasmtimeError("callback produced wrong number of results") - for i, result in enumerate(results): + for i, result in enumerate(pyresults): # Because we are taking the inner value with `_into_raw`, we # need to ensure that we have a unique `Val`. if isinstance(result, Val): val = result._clone() else: val = Val._convert(result_tys[i], result) - results_ptr[i] = val._into_raw() + results.data[i] = val._into_raw() except Exception: exc_type, exc_value, exc_traceback = sys.exc_info() fmt = traceback.format_exception(exc_type, exc_value, exc_traceback) diff --git a/wasmtime/_instance.py b/wasmtime/_instance.py index 3e41caab..05cfc2e9 100644 --- a/wasmtime/_instance.py +++ b/wasmtime/_instance.py @@ -33,14 +33,14 @@ def __init__(self, store: Store, module: Module, imports: Sequence[AsExtern]): imports_ptr = (POINTER(ffi.wasm_extern_t) * len(imports))() for i, val in enumerate(imports): imports_ptr[i] = get_extern_ptr(val) + imports_arg = ffi.wasm_extern_vec_t(len(imports), imports_ptr) instance = POINTER(ffi.wasm_instance_t)() trap = POINTER(ffi.wasm_trap_t)() error = ffi.wasmtime_instance_new( store._ptr, module._ptr, - imports_ptr, - len(imports), + byref(imports_arg), byref(instance), byref(trap)) if error: