wasmtime-py icon indicating copy to clipboard operation
wasmtime-py copied to clipboard

Accessing Store globals

Open Jflick58 opened this issue 2 years ago • 3 comments

Hello, I’ve built a wasm-powered replacement for the exec() function here and wasmtime has been invaluable!

That being said, I had a quick question - once I set a global value, how do I access the globals that I have set? Is it only through function execution?

Sample code:

            # Check if the value is of a supported type
            if isinstance(value, (int, float)):
                # Determine the Wasmtime type based on the Python type
                if isinstance(value, int):
                    value_type = ValType.i32()
                elif isinstance(value, float):
                    value_type = ValType.f64()

                # Create a mutable global instance with the Python value
                global_type = GlobalType(value_type, mutable=True)
                global_var = Global(store, global_type, value)

The data function of the Store class returns None for me. Would appreciate some guidance, thanks!

Jflick58 avatar May 30 '23 05:05 Jflick58

Could you expand a bit on what your goal is and what you're using globals for? Globals in a store can't be enumerated so you'd need to store your own list of them somewhere, for example on the host in Python. The data member of a Store is set on creation of the Store (an optional argument) and the data accessor retrieves it, but nothing more. What you can do, however, is place a list of globals onto the object returned from data

alexcrichton avatar May 31 '23 14:05 alexcrichton

Thanks for the response. Basically, I'm trying to use wasmtime and the VmWare labs Wasm Python interpreter to create an isolated version of exec I'm trying to figure out support for globals to replicate the ergonomics of the exec function. It looks like I can set them in an environment, but I was trying to figure out a way to confirm their existence and ability to be accessed for my unit tests.

Jflick58 avatar Jun 03 '23 21:06 Jflick58

Ah ok, in that case I think the "globals" you're referring to are perhaps Python globals? If so they have no relation to a WebAssembly global or the wasmtime.Global object, they're entirely separate. Enumerating Python globals as it's running in wasm would have to be a feature of the interpreter you're using

alexcrichton avatar Jun 13 '23 00:06 alexcrichton

I'm giong to close this as it's old and didn't get more context

alexcrichton avatar Nov 05 '25 22:11 alexcrichton