File: basic_test.py

package info (click to toggle)
basis-universal 2.0.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 216,436 kB
  • sloc: cpp: 163,224; ansic: 51,368; python: 2,824; javascript: 2,637; lisp: 1,026; sh: 161; makefile: 17
file content (58 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download
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
import wasmtime
import ctypes

# --- Engine ---
engine = wasmtime.Engine()

# --- Store ---
store = wasmtime.Store(engine)

# --- WASI config ---
wasi = wasmtime.WasiConfig()
wasi.argv = ["basisu_module_st"]
wasi.inherit_stdout()   # <-- tell WASI to use the host stdout
wasi.inherit_stderr()
store.set_wasi(wasi)

# --- Load module ---
module = wasmtime.Module.from_file(engine, "basisu_py/wasm/basisu_module_st.wasm")

# --- Linker + WASI ---
linker = wasmtime.Linker(engine)
linker.define_wasi()

# --- Instantiate ---
instance = linker.instantiate(store, module)
print("Single-threaded WASM instantiated OK")

# --- Exports ---
exports = instance.exports(store)

get_version = exports["bu_get_version"]
alloc       = exports["bu_alloc"]
free        = exports["bu_free"]
memory      = exports["memory"]

# --- Version ---
version = get_version(store)
print("Version =", version)

# --- Alloc ---
ptr = alloc(store, 64)
print("Allocated ptr =", ptr)

# --- Access WASM memory properly ---
data_len = memory.data_len(store)
raw_ptr  = memory.data_ptr(store)            # ctypes pointer
addr     = ctypes.addressof(raw_ptr.contents)  # convert to integer pointer

# Create a byte array view into WASM memory
buf = (ctypes.c_ubyte * data_len).from_address(addr)

# Write TEST at allocated ptr
buf[ptr : ptr + 4] = b"TEST"
print("Wrote TEST into WASM memory.")

# --- Free ---
free(store, ptr)
print("Memory free OK.")