|
1 | 1 | from contextlib import contextmanager
|
2 | 2 | from ctypes import POINTER, byref, CFUNCTYPE, c_void_p, cast
|
3 | 3 | import ctypes
|
4 |
| -from wasmtime import Store, FuncType, Val, IntoVal, Trap, WasmtimeError, ValType |
| 4 | +from wasmtime import Store, FuncType, Val, IntoVal, Trap, WasmtimeError |
5 | 5 | from . import _ffi as ffi
|
6 | 6 | from ._extern import wrap_extern
|
7 | 7 | from typing import Callable, Optional, Generic, TypeVar, List, Union, Tuple, cast as cast_type, Sequence
|
8 | 8 | from ._exportable import AsExtern
|
9 | 9 | from ._store import Storelike
|
10 |
| -from ._bindings import wasmtime_val_raw_t, wasm_valtype_kind, wasmtime_val_t, wasmtime_externref_t, wasmtime_func_t |
11 |
| -from ._value import _unintern |
12 |
| -from ._ffi import ( |
13 |
| - WASMTIME_I32, |
14 |
| - WASMTIME_I64, |
15 |
| - WASMTIME_F32, |
16 |
| - WASMTIME_F64, |
17 |
| - WASMTIME_V128, |
18 |
| - WASMTIME_FUNCREF, |
19 |
| - WASMTIME_EXTERNREF, |
20 |
| - WASM_ANYREF, |
21 |
| - WASM_FUNCREF, |
22 |
| - wasmtime_externref_data, |
23 |
| -) |
24 |
| - |
| 10 | +from ._bindings import wasmtime_val_raw_t |
| 11 | +from ._value import get_valtype_attr, val_getter, val_setter |
25 | 12 |
|
26 | 13 | T = TypeVar('T')
|
27 | 14 | FUNCTIONS: "Slab[Tuple]"
|
28 | 15 | LAST_EXCEPTION: Optional[Exception] = None
|
29 | 16 |
|
30 |
| -val_id2attr = { |
31 |
| - WASMTIME_I32.value: 'i32', |
32 |
| - WASMTIME_I64.value: 'i64', |
33 |
| - WASMTIME_F32.value: 'f32', |
34 |
| - WASMTIME_F64.value: 'f64', |
35 |
| - WASMTIME_V128.value: 'v128', |
36 |
| - WASMTIME_FUNCREF.value: 'funcref', |
37 |
| - WASMTIME_EXTERNREF.value: 'externref', |
38 |
| - WASM_FUNCREF.value: 'funcref', |
39 |
| - WASM_ANYREF.value: 'externref', |
40 |
| -} |
41 |
| - |
42 |
| -def get_valtype_attr(ty: ValType): |
43 |
| - return val_id2attr[wasm_valtype_kind(ty._ptr)] |
44 |
| - |
45 |
| -from struct import Struct |
46 |
| - |
47 |
| -def val_getter(store_id, val_raw, attr): |
48 |
| - val = getattr(val_raw, attr) |
49 |
| - |
50 |
| - if attr=='externref': |
51 |
| - ptr = ctypes.POINTER(wasmtime_externref_t) |
52 |
| - if not val: return None |
53 |
| - ffi = ptr.from_address(val) |
54 |
| - if not ffi: return |
55 |
| - extern_id = wasmtime_externref_data(ffi) |
56 |
| - ret = _unintern(extern_id) |
57 |
| - return ret |
58 |
| - elif attr=='funcref': |
59 |
| - if val==0: return None |
60 |
| - f=wasmtime_func_t() |
61 |
| - f.store_id=store_id |
62 |
| - f.index=val |
63 |
| - ret=Func._from_raw(f) |
64 |
| - return ret |
65 |
| - return val |
66 |
| - |
67 |
| -def val_setter(dst, attr, val): |
68 |
| - if attr=='externref': |
69 |
| - if isinstance(val, Val) and val._raw.kind==WASMTIME_EXTERNREF.value: |
70 |
| - if val._raw.of.externref: |
71 |
| - extern_id = wasmtime_externref_data(val._raw.of.externref) |
72 |
| - casted = ctypes.addressof(val._raw.of.externref) |
73 |
| - else: |
74 |
| - v = Val.externref(val) |
75 |
| - casted = ctypes.addressof(v._raw.of.externref) |
76 |
| - elif attr=='funcref': |
77 |
| - if isinstance(val, Val) and val._raw.kind==WASMTIME_FUNCREF.value: |
78 |
| - casted = val._raw.of.funcref.index |
79 |
| - else: raise RuntimeError("foo") |
80 |
| - elif isinstance(val, Func): |
81 |
| - # TODO: handle null_funcref |
82 |
| - # TODO: validate same val._func.store_id |
83 |
| - casted = val._func.index |
84 |
| - else: |
85 |
| - if isinstance(val, Val): |
86 |
| - casted = getattr(val._raw.of, attr) |
87 |
| - else: |
88 |
| - casted = val |
89 |
| - setattr(dst, attr, casted) |
90 | 17 |
|
91 | 18 | class Func:
|
92 | 19 | _func: ffi.wasmtime_func_t
|
@@ -147,8 +74,7 @@ def _extract_return(self, vals_raw: ctypes.Array[wasmtime_val_raw_t]) -> Union[I
|
147 | 74 | if self._results_n==0:
|
148 | 75 | return None
|
149 | 76 | if self._results_n==1:
|
150 |
| - ret = val_getter(self._func.store_id, vals_raw[0], self._results_str0) |
151 |
| - return ret |
| 77 | + return val_getter(self._func.store_id, vals_raw[0], self._results_str0) |
152 | 78 | # we can use tuple construct, but I'm using list for compatability
|
153 | 79 | return [val_getter(self._func.store_id, val_raw, ret_str) for val_raw, ret_str in zip(vals_raw, self._results_str)]
|
154 | 80 |
|
|
0 commit comments