Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create InstantiatedModule type #170

Closed
codefromthecrypt opened this issue Jan 29, 2022 · 3 comments
Closed

Create InstantiatedModule type #170

codefromthecrypt opened this issue Jan 29, 2022 · 3 comments

Comments

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Jan 29, 2022

There's a lifecycle requirement that is tricky to prove here:

The start function is intended for initializing the state of a module. The module and its exports are not accessible before this initialization has completed.

https://www.w3.org/TR/wasm-core-1/#start-function%E2%91%A0

Right now, Store includes the instantiated module state internally.

	err = store.Instantiate(mod, "test")
        store.CallFunction("test", "fac-iter", in)

Due to this, to satisfy the above things, runtime checks like this have to happen internally.

	m, ok := s.ModuleInstances[moduleName]
	if !ok {
		return nil, nil, fmt.Errorf("module '%s' not instantiated", moduleName)
	}

This are subject to racing which makes them hard to test also. Finally, it burdens the user a little as they have a way to invoke something when it can be in the wrong state. Ex.

	// forgot to call store.Instantiate, or some other goroutine isn't done that.
        store.CallFunction("test", "fac-iter", in) // bombs

I think a cleaner design would be to make an InstantiatedModule type and move features that require start being invoked first, to be on that type. That features are only on the correct type, people wouldn't be tempted to do something at the wrong time or when something isn't available

Ex.

	store.CallFunction("test", "fac-iter", in) // compile error!
	imod, err = store.Instantiate(mod, "test")
        imod.CallFunction("fac-iter", in) // compile error!
        factIter, ok = imod.GetFunction("fac-iter") // if ok, definitely that function exists
        factIter.invoke(ctx, in) // naming this "call" is also fine. whichever you can use it as often as you like
@codefromthecrypt
Copy link
Contributor Author

If we do this I also think we should think hard about whether adding and replacing functions on an instantiated module is something to support or not. Safety-wise it feels we shouldn't keep doing this, rather allow someone to change the imports/exports etc only up to .Instantiate(), then after that they are fixed.

@codefromthecrypt
Copy link
Contributor Author

from @mathetake

let's add store.GetModuleInstance as well or return *ModuleInstance on store.Instantiate to line with moduleInstance.GetExport API 😄

@codefromthecrypt
Copy link
Contributor Author

this is done via api.Module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant