-
Notifications
You must be signed in to change notification settings - Fork 52
/
_value.py
289 lines (252 loc) · 9.61 KB
/
_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from ._error import WasmtimeError
from ._ffi import *
from ._types import ValType
import ctypes
import typing
if typing.TYPE_CHECKING:
from ._store import Storelike
@ctypes.CFUNCTYPE(None, c_void_p)
def _externref_finalizer(extern_id: int) -> None:
Val._id_to_ref_count[extern_id] -= 1
if Val._id_to_ref_count[extern_id] == 0:
del Val._id_to_ref_count[extern_id]
del Val._id_to_extern[extern_id]
def _intern(obj: typing.Any) -> c_void_p:
extern_id = id(obj)
Val._id_to_ref_count.setdefault(extern_id, 0)
Val._id_to_ref_count[extern_id] += 1
Val._id_to_extern[extern_id] = obj
return ctypes.c_void_p(extern_id)
def _unintern(val: int) -> typing.Any:
return Val._id_to_extern.get(val)
class Val:
# We can't let the extern values we wrap `externref`s around be GC'd, so we
# pin them in `_id_to_extern`. Additionally, we might make multiple
# `externref`s to the same extern value, so we count how many references
# we've created in `_id_to_ref_count`, and only remove a value's entry from
# `_id_to_extern` once the ref count is zero.
_id_to_extern: typing.Dict[int, typing.Any] = {}
_id_to_ref_count: typing.Dict[int, int] = {}
_kind: wasmtime_valkind_t
_val: typing.Any
@classmethod
def i32(cls, val: int) -> "Val":
"""
Create a new 32-bit integer value
"""
if not isinstance(val, int):
raise TypeError("expected an integer")
val = wasmtime_valunion_t(i32=val).i32
return Val(WASMTIME_I32, val)
@classmethod
def i64(cls, val: int) -> "Val":
"""
Create a new 64-bit integer value
"""
if not isinstance(val, int):
raise TypeError("expected an integer")
val = wasmtime_valunion_t(i64=val).i64
return Val(WASMTIME_I64, val)
@classmethod
def f32(cls, val: float) -> "Val":
"""
Create a new 32-bit float value
"""
if not isinstance(val, float):
raise TypeError("expected a float")
val = wasmtime_valunion_t(f32=val).f32
return Val(WASMTIME_F32, val)
@classmethod
def f64(cls, val: float) -> "Val":
"""
Create a new 64-bit float value
"""
if not isinstance(val, float):
raise TypeError("expected a float")
val = wasmtime_valunion_t(f64=val).f64
return Val(WASMTIME_F64, val)
@classmethod
def externref(cls, extern: typing.Optional[typing.Any]) -> "Val":
return Val(WASMTIME_EXTERNREF, extern)
@classmethod
def funcref(cls, f: "typing.Optional[wasmtime.Func]") -> "Val":
return Val(WASMTIME_FUNCREF, f)
@classmethod
def ref_null(cls, ty: ValType) -> "Val":
"""
Create a null reference value of the given type.
Raise an exception if `ty` is not a reference type.
"""
if ty == ValType.externref():
return Val.externref(None)
if ty == ValType.funcref():
return Val.funcref(None)
raise WasmtimeError("Invalid reference type for `ref_null`: %s" % ty)
def __init__(self, kind: wasmtime_valkind_t, val: typing.Any):
self._kind = kind
self._val = val
def __eq__(self, rhs: typing.Any) -> typing.Any:
if isinstance(rhs, Val):
return self._kind == rhs._kind and self._val == rhs._val
return self._val == rhs
@classmethod
def _convert_to_raw(cls, store: 'Storelike', ty: ValType, val: Any) -> wasmtime_val_t:
if isinstance(val, Val):
if ty != val.type:
raise TypeError("wrong type of `Val` provided")
return val._new_raw(store)
if ty == ValType.externref():
print('convert to externref', val);
return Val.externref(val)._new_raw(store)
elif isinstance(val, int):
if ty == ValType.i32():
return Val.i32(val)._new_raw(store)
if ty == ValType.i64():
return Val.i64(val)._new_raw(store)
elif isinstance(val, float):
if ty == ValType.f32():
return Val.f32(val)._new_raw(store)
if ty == ValType.f64():
return Val.f64(val)._new_raw(store)
elif isinstance(val, wasmtime.Func):
return Val.funcref(val)._new_raw(store)
elif val is None:
if ty == ValType.externref():
return Val.externref(None)._new_raw(store)
if ty == ValType.funcref():
return Val.funcref(None)._new_raw(store)
raise TypeError("don't know how to convert %r to %s" % (val, ty))
def _new_raw(self, store: 'Storelike') -> wasmtime_val_t:
ret = wasmtime_val_t(kind = self._kind)
if ret.kind == WASMTIME_I32.value:
ret.of.i32 = self._val
elif ret.kind == WASMTIME_I64.value:
ret.of.i64 = self._val
elif ret.kind == WASMTIME_F32.value:
ret.of.f32 = self._val
elif ret.kind == WASMTIME_F64.value:
ret.of.f64 = self._val
elif ret.kind == WASMTIME_EXTERNREF.value:
if self._val is not None:
extern_id = _intern(self._val)
if not wasmtime_externref_new(store._context(), extern_id, _externref_finalizer,
byref(ret.of.externref)):
raise WasmtimeError("failed to create an externref value")
else:
ret.of.externref.store_id = 0
elif ret.kind == WASMTIME_FUNCREF.value:
if self._val is not None:
ret.of.funcref = self._val._func
else:
raise RuntimeError("unknown kind")
return ret
@classmethod
def _from_raw(cls, store: 'Storelike', raw: wasmtime_val_t, owned: bool = True) -> 'Val':
val: typing.Any = None
if raw.kind == WASMTIME_I32.value:
val = raw.of.i32
elif raw.kind == WASMTIME_I64.value:
val = raw.of.i64
elif raw.kind == WASMTIME_F32.value:
val = raw.of.f32
elif raw.kind == WASMTIME_F64.value:
val = raw.of.f64
elif raw.kind == WASMTIME_EXTERNREF.value:
if raw.of.externref:
extern_id = wasmtime_externref_data(store._context(), raw.of.externref)
val = _unintern(extern_id)
elif raw.kind == WASMTIME_FUNCREF.value:
if raw.of.funcref.store_id != 0:
val = wasmtime.Func._from_raw(raw.of.funcref)
else:
raise WasmtimeError("Unkown `wasmtime_valkind_t`: {}".format(raw.kind))
if owned:
wasmtime_val_unroot(store._context(), byref(raw))
return Val(raw.kind, val)
@property
def value(self) -> typing.Any:
"""
Get the the underlying value as a python value
"""
return self._val
def as_i32(self) -> typing.Optional[int]:
"""
Get the 32-bit integer value of this value, or `None` if it's not an i32
"""
if self._kind == WASMTIME_I32:
assert(isinstance(self._val, int))
return self._val
else:
return None
def as_i64(self) -> typing.Optional[int]:
"""
Get the 64-bit integer value of this value, or `None` if it's not an i64
"""
if self._kind == WASMTIME_I64:
assert(isinstance(self._val, int))
return self._val
else:
return None
def as_f32(self) -> typing.Optional[float]:
"""
Get the 32-bit float value of this value, or `None` if it's not an f32
"""
if self._kind == WASMTIME_F32:
assert(isinstance(self._val, float))
return self._val
else:
return None
def as_f64(self) -> typing.Optional[float]:
"""
Get the 64-bit float value of this value, or `None` if it's not an f64
"""
if self._kind == WASMTIME_F64:
assert(isinstance(self._val, float))
return self._val
else:
return None
def as_externref(self) -> typing.Optional[typing.Any]:
"""
Get the extern data referenced by this `externref` value, or `None` if
it's not an `externref`.
"""
if self._kind == WASMTIME_EXTERNREF:
return self._val
else:
return None
def as_funcref(self) -> typing.Optional["wasmtime.Func"]:
"""
Get the function that this `funcref` value is referencing, or `None` if
this is not a `funcref` value, or is a null reference.
"""
if self._kind == WASMTIME_FUNCREF:
assert(isinstance(self._val, wasmtime.Func))
return self._val
else:
return None
@property
def type(self) -> ValType:
"""
Returns the `ValType` corresponding to this `Val`
"""
kind = self._kind
if kind == WASMTIME_I32:
return ValType.i32()
elif kind == WASMTIME_I64:
return ValType.i64()
elif kind == WASMTIME_F32:
return ValType.f32()
elif kind == WASMTIME_F64:
return ValType.f64()
elif kind == WASMTIME_V128:
raise Exception("unimplemented v128 type")
elif kind == WASMTIME_EXTERNREF:
return ValType.externref()
elif kind == WASMTIME_FUNCREF:
return ValType.funcref()
else:
raise Exception("unknown kind %d" % kind.value)
# This needs to be imported so that mypy understands `wasmtime.Func` in typings,
# but it also has to come after `Val`'s definition to deal with circular module
# dependency issues.
import wasmtime # noqa: E402