From 0ec00a9e0755aab9d42f31fb6eedaebc39c44c41 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Mon, 27 Feb 2023 14:21:23 -0500 Subject: [PATCH] Demonstrate preopen of tmpdir in linking example --- examples/linking.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/examples/linking.py b/examples/linking.py index 4c24dbf8..7bdea057 100644 --- a/examples/linking.py +++ b/examples/linking.py @@ -2,6 +2,8 @@ from wasmtime import Engine, Store, Module, Linker, WasiConfig +import tempfile + engine = Engine() # Load and compile our two modules @@ -13,18 +15,20 @@ linker = Linker(engine) linker.define_wasi() -# Create a `Store` to hold instances, and configure wasi state -store = Store(engine) -wasi = WasiConfig() -wasi.inherit_stdout() -store.set_wasi(wasi) - -# Instantiate our first module which only uses WASI, then register that -# instance with the linker since the next linking will use it. -linking2 = linker.instantiate(store, linking2) -linker.define_instance(store, "linking2", linking2) - -# And with that we can perform the final link and the execute the module. -linking1 = linker.instantiate(store, linking1) -run = linking1.exports(store)["run"] -run(store) +with tempfile.TemporaryDirectory() as tmpdirname: + # Create a `Store` to hold instances, and configure wasi state + store = Store(engine) + wasi = WasiConfig() + wasi.inherit_stdout() + wasi.preopen_dir(tmpdirname, tmpdirname) + store.set_wasi(wasi) + + # Instantiate our first module which only uses WASI, then register that + # instance with the linker since the next linking will use it. + linking2 = linker.instantiate(store, linking2) + linker.define_instance(store, "linking2", linking2) + + # And with that we can perform the final link and the execute the module. + linking1 = linker.instantiate(store, linking1) + run = linking1.exports(store)["run"] + run(store)