Coverage for wasmtime/_ffi.py: 77%
79 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1from ctypes import *
2from pathlib import Path
3import ctypes
4import sys
5import platform
6import typing
8if sys.maxsize <= 2**32:
9 raise RuntimeError("wasmtime only works on 64-bit platforms right now")
11if sys.platform == 'linux':
12 libname = '_libwasmtime.so'
13elif sys.platform == 'win32':
14 libname = '_wasmtime.dll'
15elif sys.platform == 'darwin':
16 libname = '_libwasmtime.dylib'
17else:
18 raise RuntimeError("unsupported platform `{}` for wasmtime".format(sys.platform))
20machine = platform.machine()
21if machine == 'AMD64':
22 machine = 'x86_64'
23if machine == 'arm64' or machine == 'ARM64':
24 machine = 'aarch64'
25if machine != 'x86_64' and machine != 'aarch64':
26 raise RuntimeError("unsupported architecture for wasmtime: {}".format(machine))
28filename = Path(__file__).parent / (sys.platform + '-' + machine) / libname
29if not filename.exists():
30 raise RuntimeError("precompiled wasmtime binary not found at `{}`".format(filename))
31dll = cdll.LoadLibrary(str(filename))
33WASM_I32 = c_uint8(0)
34WASM_I64 = c_uint8(1)
35WASM_F32 = c_uint8(2)
36WASM_F64 = c_uint8(3)
37WASM_ANYREF = c_uint8(128)
38WASM_FUNCREF = c_uint8(129)
39# WASM_V128 = c_uint8(4)
41WASMTIME_I32 = c_uint8(0)
42WASMTIME_I64 = c_uint8(1)
43WASMTIME_F32 = c_uint8(2)
44WASMTIME_F64 = c_uint8(3)
45WASMTIME_V128 = c_uint8(4)
46WASMTIME_FUNCREF = c_uint8(5)
47WASMTIME_EXTERNREF = c_uint8(6)
49WASM_CONST = c_uint8(0)
50WASM_VAR = c_uint8(1)
52WASMTIME_EXTERN_FUNC = c_uint8(0)
53WASMTIME_EXTERN_GLOBAL = c_uint8(1)
54WASMTIME_EXTERN_TABLE = c_uint8(2)
55WASMTIME_EXTERN_MEMORY = c_uint8(3)
56WASMTIME_EXTERN_SHAREDMEMORY = c_uint8(4)
57WASMTIME_EXTERN_INSTANCE = c_uint8(4)
58WASMTIME_EXTERN_MODULE = c_uint8(5)
60WASMTIME_FUNCREF_NULL = (1 << 64) - 1
63class wasm_ref_t(Structure):
64 pass
67class wasm_val_union(Union):
68 _fields_ = [
69 ("i32", c_int32),
70 ("i64", c_int64),
71 ("f32", c_float),
72 ("f64", c_double),
73 ("ref", POINTER(wasm_ref_t)),
74 ]
76 i32: int
77 i64: int
78 f32: float
79 f64: float
80 ref: "typing.Union[ctypes._Pointer[wasm_ref_t], None]"
83class wasm_val_t(Structure):
84 _fields_ = [("kind", c_uint8), ("of", wasm_val_union)]
86 kind: int
87 of: wasm_val_union
90from ._bindings import * # noqa
93def to_bytes(vec: wasm_byte_vec_t) -> bytearray:
94 ty = c_uint8 * vec.size
95 return bytearray(ty.from_address(addressof(vec.data.contents)))
98def to_str(vec: wasm_byte_vec_t) -> str:
99 return to_bytes(vec).decode("utf-8")
102def to_str_raw(ptr: "ctypes._Pointer", size: int) -> str:
103 return string_at(ptr, size).decode("utf-8")
106def str_to_name(s: str, trailing_nul: bool = False) -> wasm_byte_vec_t:
107 if not isinstance(s, str):
108 raise TypeError("expected a string")
109 s_bytes = s.encode('utf8')
110 buf = cast(create_string_buffer(s_bytes), POINTER(c_uint8))
111 if trailing_nul:
112 extra = 1
113 else:
114 extra = 0
115 return wasm_byte_vec_t(len(s_bytes) + extra, buf)